|
|
|
@ -28,7 +28,7 @@ type ResponseEntity struct { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type RestInterface interface { |
|
|
|
|
Send(id string) |
|
|
|
|
Send(id rune) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type rest struct { |
|
|
|
@ -38,38 +38,41 @@ type rest struct { |
|
|
|
|
mutex sync.Mutex |
|
|
|
|
timer *time.Timer |
|
|
|
|
sb strings.Builder |
|
|
|
|
nospace bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewRest(connectorId uint, url string, timeout time.Duration) RestInterface { |
|
|
|
|
r := rest{connectorId: connectorId, url: url, timeout: timeout} |
|
|
|
|
func NewRest(connectorId uint, url string, timeout time.Duration, nospace bool) RestInterface { |
|
|
|
|
r := rest{connectorId: connectorId, url: url, timeout: timeout, nospace: nospace} |
|
|
|
|
return &r |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *rest) Send(id string) { |
|
|
|
|
r.mutex.Lock() |
|
|
|
|
defer r.mutex.Unlock() |
|
|
|
|
func (c *rest) Send(r rune) { |
|
|
|
|
c.mutex.Lock() |
|
|
|
|
defer c.mutex.Unlock() |
|
|
|
|
|
|
|
|
|
r.sb.WriteString(id) |
|
|
|
|
if !(c.nospace && r == ' ') { |
|
|
|
|
c.sb.WriteRune(r) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if r.timer != nil { |
|
|
|
|
r.timer.Reset(timeout) |
|
|
|
|
if c.timer != nil { |
|
|
|
|
c.timer.Reset(timeout) |
|
|
|
|
} else { |
|
|
|
|
f := func() { |
|
|
|
|
s := r.sb.String() |
|
|
|
|
log.Printf("Send '%s'", s) |
|
|
|
|
go r.send(s) |
|
|
|
|
r.sb.Reset() |
|
|
|
|
s := c.sb.String() |
|
|
|
|
log.Printf("Sent '%s'", s) |
|
|
|
|
go c.send(s) |
|
|
|
|
c.sb.Reset() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
r.timer = time.AfterFunc(timeout, f) |
|
|
|
|
c.timer = time.AfterFunc(timeout, f) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (r *rest) send(id string) { |
|
|
|
|
tag := &MonitorIdTag{IdTag: id, ConnectorId: r.connectorId} |
|
|
|
|
func (c *rest) send(id string) { |
|
|
|
|
tag := &MonitorIdTag{IdTag: id, ConnectorId: c.connectorId} |
|
|
|
|
|
|
|
|
|
//if r.connectorId > 0 {
|
|
|
|
|
// tag.ConnectorId = &r.connectorId
|
|
|
|
|
//if c.connectorId > 0 {
|
|
|
|
|
// tag.ConnectorId = &c.connectorId
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
var b []byte |
|
|
|
@ -82,10 +85,10 @@ func (r *rest) send(id string) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), r.timeout) |
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), c.timeout) |
|
|
|
|
byteReader := bytes.NewReader(b) |
|
|
|
|
|
|
|
|
|
if request, err = http.NewRequestWithContext(ctx, "POST", r.url, byteReader); err != nil { |
|
|
|
|
if request, err = http.NewRequestWithContext(ctx, "POST", c.url, byteReader); err != nil { |
|
|
|
|
log.Printf("Can't build new request: %e\n", err) |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|