httpSender now implemented as a NetSender client, rather than a generic HTTP client.

This commit is contained in:
Alan Noble 2018-06-15 16:42:29 +09:30
parent 3f59d353c7
commit 5ba5327f33
1 changed files with 17 additions and 23 deletions

View File

@ -32,14 +32,12 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strconv"
"time"
"bitbucket.org/ausocean/av/rtmp"
"bitbucket.org/ausocean/iot/pi/netsender"
"bitbucket.org/ausocean/utils/ring"
)
@ -103,10 +101,9 @@ func (s *fileSender) close() error {
return s.file.Close()
}
// httpSender implements loadSender for an HTTP destination.
// httpSender implements loadSender for posting HTTP to NetReceiver
type httpSender struct {
client http.Client
url string
client *netsender.Sender
log func(lvl, msg string)
@ -115,10 +112,11 @@ type httpSender struct {
chunk *ring.Chunk
}
func newHttpSender(url string, timeout time.Duration, log func(lvl, msg string)) *httpSender {
func newHttpSender(_ string, _ time.Duration, log func(lvl, msg string)) *httpSender {
var client netsender.Sender
client.Init(nil, nil, nil)
return &httpSender{
client: http.Client{Timeout: timeout},
url: url,
client: &client,
log: log,
}
}
@ -129,27 +127,23 @@ func (s *httpSender) load(c *ring.Chunk) error {
_, err := s.chunk.WriteTo(buf)
s.buf = buf.Bytes()
if err != nil {
return fmt.Errorf("fileSender: %v", err)
return fmt.Errorf("httpSender: %v", err)
}
return nil
}
func (s *httpSender) send() error {
url := s.url + strconv.Itoa(len(s.buf))
s.log(Debug, fmt.Sprintf("Posting %s (%d bytes)\n", url, len(s.buf)))
resp, err := s.client.Post(url, "video/mp2t", bytes.NewReader(s.buf))
pins := netsender.MakePins("V0")
pins[0].Value = len(s.buf)
var payload netsender.Payload
payload.Name = "V0"
payload.Data = s.buf
payload.MimeType = "video/mp2t"
_, _, err := s.client.Send(netsender.RequestPoll, pins, &payload)
if err != nil {
return fmt.Errorf("Error posting to %s: %s", url, err)
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
s.log(Debug, fmt.Sprintf("%s\n", body))
} else {
s.log(Error, err.Error())
}
return err
return nil
}
func (s *httpSender) release() {