diff --git a/device.go b/device.go index 6a5bcd7..929a525 100644 --- a/device.go +++ b/device.go @@ -1,11 +1,11 @@ package gocast import ( - "log" "net" "strconv" "sync" + "github.com/sirupsen/logrus" "github.com/stampzilla/gocast/api" "github.com/stampzilla/gocast/events" "github.com/stampzilla/gocast/handlers" @@ -50,12 +50,15 @@ func NewDevice() *Device { func (d *Device) SetName(name string) { d.name = name } + func (d *Device) SetUuid(uuid string) { d.uuid = uuid } + func (d *Device) SetIp(ip net.IP) { d.ip = ip } + func (d *Device) SetPort(port int) { d.port = port } @@ -63,12 +66,15 @@ func (d *Device) SetPort(port int) { func (d *Device) Name() string { return d.name } + func (d *Device) Uuid() string { return d.uuid } + func (d *Device) Ip() net.IP { return d.ip } + func (d *Device) Port() int { return d.port } @@ -98,8 +104,7 @@ func (d *Device) Subscribe(urn, destinationId string, handler Handler) { handler.RegisterDispatch(d.Dispatch) handler.Connect() - log.Println("Subscribing to ", urn, " --- ", destinationId) - + logrus.Debug("Subscribing to ", urn, " --- ", destinationId) } func (d *Device) UnsubscribeByUrn(urn string) { @@ -117,6 +122,7 @@ func (d *Device) UnsubscribeByUrn(urn string) { delete(d.subscriptions, sub) } } + func (d *Device) UnsubscribeByUrnAndDestinationId(urn, destinationId string) { subs := []string{} d.RLock() diff --git a/device_connection.go b/device_connection.go index 64a1afa..5273a13 100644 --- a/device_connection.go +++ b/device_connection.go @@ -4,11 +4,11 @@ import ( "crypto/tls" "encoding/json" "fmt" - "log" "time" "github.com/davecgh/go-spew/spew" "github.com/gogo/protobuf/proto" + "github.com/sirupsen/logrus" "github.com/stampzilla/gocast/api" "github.com/stampzilla/gocast/events" "github.com/stampzilla/gocast/responses" @@ -17,29 +17,25 @@ import ( func (d *Device) reader() { for { packet, err := d.wrapper.Read() - if err != nil { - log.Println("Error reading from chromecast error:", err, "Packet:", packet) + logrus.Errorf("Error reading from chromecast error: %s Packet: %#v", err, packet) d.Disconnect() - //d.reconnect <- struct{}{} return } message := &api.CastMessage{} err = proto.Unmarshal(*packet, message) if err != nil { - log.Fatalf("Failed to unmarshal CastMessage: %s", err) + logrus.Errorf("Failed to unmarshal CastMessage: %s", err) continue } - //spew.Dump("Message!", message) - headers := &responses.Headers{} err = json.Unmarshal([]byte(*message.PayloadUtf8), headers) if err != nil { - log.Fatalf("Failed to unmarshal message: %s", err) + logrus.Errorf("Failed to unmarshal message: %s", err) continue } @@ -53,8 +49,8 @@ func (d *Device) reader() { d.RUnlock() if !catched { - log.Println("LOST MESSAGE:") - spew.Dump(message) + logrus.Debug("LOST MESSAGE:") + logrus.Debug(spew.Sdump(message)) } } } @@ -64,31 +60,38 @@ func (d *Device) Connected() bool { defer d.RUnlock() return d.connected } + func (d *Device) Connect() error { go d.reconnector() return d.connect() } + func (d *Device) Reconnect() { select { case d.reconnect <- struct{}{}: default: } } + func (d *Device) reconnector() { for { select { case <-d.reconnect: - log.Println("Reconnect signal received") + logrus.Info("Reconnect signal received") time.Sleep(time.Second * 2) - d.connect() + err := d.connect() + if err != nil { + logrus.Error(err) + } } } } + func (d *Device) connect() error { - log.Printf("connecting to %s:%d ...", d.ip, d.port) + logrus.Infof("connecting to %s:%d ...", d.ip, d.port) if d.conn != nil { - return fmt.Errorf("Already connected to: %s (%s:%d)", d.Name(), d.Ip().String(), d.Port()) + return fmt.Errorf("already connected to: %s (%s:%d)", d.Name(), d.Ip().String(), d.Port()) } var err error @@ -97,16 +100,14 @@ func (d *Device) connect() error { }) if err != nil { - //d.reconnect <- struct{}{} - return fmt.Errorf("Failed to connect to Chromecast. Error:%s", err) + return fmt.Errorf("failed to connect to Chromecast. Error:%s", err) } d.Lock() d.connected = true d.Unlock() - event := events.Connected{} - d.Dispatch(event) + d.Dispatch(events.Connected{}) d.wrapper = NewPacketStream(d.conn) go d.reader() @@ -139,7 +140,6 @@ func (d *Device) Disconnect() { func (d *Device) Send(urn, sourceId, destinationId string, payload responses.Payload) error { payloadJson, err := json.Marshal(payload) if err != nil { - fmt.Println("Failed to json.Marshal: ", err) return err } payloadString := string(payloadJson) @@ -157,16 +157,15 @@ func (d *Device) Send(urn, sourceId, destinationId string, payload responses.Pay data, err := proto.Marshal(message) if err != nil { - fmt.Println("Failed to proto.Marshal: ", err) return err } if *message.Namespace != "urn:x-cast:com.google.cast.tp.heartbeat" { - log.Println("Writing:", spew.Sdump(message)) + logrus.Debug("Writing:", spew.Sdump(message)) } if d.conn == nil { - return fmt.Errorf("We are disconnected, cannot send!") + return fmt.Errorf("we are disconnected, cannot send!") } _, err = d.wrapper.Write(data) diff --git a/discovery/service.go b/discovery/service.go index 5db8f74..5a8e284 100644 --- a/discovery/service.go +++ b/discovery/service.go @@ -33,9 +33,8 @@ func NewService() *Service { } func (d *Service) Periodic(interval time.Duration) error { - if d.stopPeriodic != nil { - return fmt.Errorf("Periodic discovery is allready running") + return fmt.Errorf("Periodic discovery is already running") } mdns.Query(&mdns.QueryParam{ @@ -82,7 +81,7 @@ func (d *Service) Found() chan *gocast.Device { func (d *Service) listner() { for entry := range d.entriesCh { - //fmt.Printf("Got new entry: %#v\n", entry) + // fmt.Printf("Got new entry: %#v\n", entry) name := strings.Split(entry.Name, "._googlecast") diff --git a/discovery/service_test.go b/discovery/service_test.go index e9b9176..05d6804 100644 --- a/discovery/service_test.go +++ b/discovery/service_test.go @@ -7,20 +7,17 @@ import ( ) func TestDecodeDnsEntry(t *testing.T) { - source := `Stamp\.\.\ \195\132r\ En\ Liten\ Fisk` result := decodeDnsEntry(source) assert.Equal(t, result, "Stamp.. Är En Liten Fisk") - +} } func TestDecodeTxtRecord(t *testing.T) { - source := `id=87cf98a003f1f1dbd2efe6d19055a617|ve=04|md=Chromecast|ic=/setup/icon.png|fn=Chromecast PO|ca=5|st=0|bs=FA8FCA7EE8A9|rs=` result := decodeTxtRecord(source) assert.Equal(t, result["id"], "87cf98a003f1f1dbd2efe6d19055a617") - } diff --git a/example/main.go b/example/main.go index 6a6c27d..c72d489 100644 --- a/example/main.go +++ b/example/main.go @@ -27,11 +27,11 @@ func main() { func discoveryListner(discovery *discovery.Service) { for device := range discovery.Found() { - fmt.Printf("New device discoverd: %#v \n", device) + fmt.Printf("New device discovered: %#v \n", device) - //plexHandler := NewPlexHandler() - //device.Subscribe("urn:x-cast:plex", plexHandler) - //device.Subscribe("urn:x-cast:com.google.cast.media", mediaHandler) + // plexHandler := NewPlexHandler() + // device.Subscribe("urn:x-cast:plex", plexHandler) + // device.Subscribe("urn:x-cast:com.google.cast.media", mediaHandler) device.OnEvent(func(event events.Event) { switch data := event.(type) { @@ -46,8 +46,8 @@ func discoveryListner(discovery *discovery.Service) { fmt.Println(device.Name(), "- App started:", data.DisplayName, "(", data.AppID, ")") case events.AppStopped: fmt.Println(device.Name(), "- App stopped:", data.DisplayName, "(", data.AppID, ")") - //gocast.MediaEvent: - //plexEvent: + // gocast.MediaEvent: + // plexEvent: default: fmt.Printf("unexpected event %T: %#v\n", data, data) } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..44d86e6 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module github.com/stampzilla/gocast + +go 1.15 + +require ( + github.com/davecgh/go-spew v1.1.1 + github.com/gogo/protobuf v1.3.1 + github.com/micro/mdns v0.3.0 + github.com/sirupsen/logrus v1.7.0 + github.com/stretchr/testify v1.6.1 + golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3d90095 --- /dev/null +++ b/go.sum @@ -0,0 +1,33 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= +github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/micro/mdns v0.3.0 h1:bYycYe+98AXR3s8Nq5qvt6C573uFTDPIYzJemWON0QE= +github.com/micro/mdns v0.3.0/go.mod h1:KJ0dW7KmicXU2BV++qkLlmHYcVv7/hHnbtguSWt9Aoc= +github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM= +github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664 h1:YbZJ76lQ1BqNhVe7dKTSB67wDrc2VPRR75IyGyyPDX8= +golang.org/x/crypto v0.0.0-20190130090550-b01c7a725664/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3 h1:ulvT7fqt0yHWzpJwI57MezWnYDVpCAYBVuYst/L+fAY= +golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190129075346-302c3dd5f1cc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handlers/basehandler.go b/handlers/basehandler.go index bd5aacb..6237945 100644 --- a/handlers/basehandler.go +++ b/handlers/basehandler.go @@ -23,6 +23,7 @@ func (r *baseHandler) Dispatch(e events.Event) { func (r *baseHandler) RegisterSend(send func(responses.Payload) error) { r.send = send } + func (r *baseHandler) Send(p responses.Payload) error { return r.send(p) } @@ -30,6 +31,7 @@ func (r *baseHandler) Send(p responses.Payload) error { func (r *baseHandler) RegisterRequest(send func(responses.Payload) (*api.CastMessage, error)) { r.request = send } + func (r *baseHandler) Request(p responses.Payload) (*api.CastMessage, error) { return r.request(p) } diff --git a/handlers/connection.go b/handlers/connection.go index 15a5ca9..d3d33f5 100644 --- a/handlers/connection.go +++ b/handlers/connection.go @@ -1,8 +1,7 @@ package handlers import ( - "fmt" - + "github.com/sirupsen/logrus" "github.com/stampzilla/gocast/responses" ) @@ -19,5 +18,5 @@ func (c *Connection) Disconnect() { } func (c *Connection) Unmarshal(message string) { - fmt.Println("Connection received: ", message) + logrus.Info("Connection received: ", message) } diff --git a/handlers/media.go b/handlers/media.go index 208159d..4f8958c 100644 --- a/handlers/media.go +++ b/handlers/media.go @@ -3,8 +3,8 @@ package handlers import ( "encoding/json" "fmt" - "log" + "github.com/sirupsen/logrus" "github.com/stampzilla/gocast/events" "github.com/stampzilla/gocast/responses" ) @@ -16,34 +16,32 @@ type Media struct { } func (m *Media) Connect() { + logrus.Info("Connecting to media") // Request a new status update - log.Println("Connecting to media") m.Send(&responses.Headers{Type: "GET_STATUS"}) } func (m *Media) Disconnect() { - //r.knownApplications = make(map[string]responses.ApplicationSession, 0) + // r.knownApplications = make(map[string]responses.ApplicationSession, 0) m.currentStatus = nil } func (m *Media) Unmarshal(message string) { - log.Println("Media received: ", message) + logrus.Debug("Media received: ", message) response := &responses.MediaStatusResponse{} err := json.Unmarshal([]byte(message), response) - if err != nil { - fmt.Printf("Failed to unmarshal status message:%s - %s\n", err, message) + logrus.Errorf("Failed to unmarshal status message:%s - %s\n", err, message) return } - //log.Println("MEDIA SESSION ID: ", response.MediaSessionID) if len(response.Status) > 0 { m.currentStatus = response.Status[0] m.Dispatch(events.Media{m.currentStatus}) } - } + func (m *Media) Play() { if m.currentStatus != nil { m.Request(&responses.MediaCommand{commandMediaPlay, m.currentStatus.MediaSessionID}) @@ -70,11 +68,13 @@ func (m *Media) Seek(currentTime int) { var getMediaStatus = responses.Headers{Type: "GET_STATUS"} -var commandMediaPlay = responses.Headers{Type: "PLAY"} -var commandMediaPause = responses.Headers{Type: "PAUSE"} -var commandMediaStop = responses.Headers{Type: "STOP"} -var commandMediaLoad = responses.Headers{Type: "LOAD"} -var commandMediaSeek = responses.Headers{Type: "SEEK"} +var ( + commandMediaPlay = responses.Headers{Type: "PLAY"} + commandMediaPause = responses.Headers{Type: "PAUSE"} + commandMediaStop = responses.Headers{Type: "STOP"} + commandMediaLoad = responses.Headers{Type: "LOAD"} + commandMediaSeek = responses.Headers{Type: "SEEK"} +) func (c *Media) LoadMedia(media responses.MediaItem, currentTime int, autoplay bool, customData interface{}) error { _, err := c.Request(&responses.LoadMediaCommand{ diff --git a/handlers/receiver.go b/handlers/receiver.go index ea19878..d798f96 100644 --- a/handlers/receiver.go +++ b/handlers/receiver.go @@ -3,8 +3,8 @@ package handlers import ( "encoding/json" "fmt" - "log" + "github.com/sirupsen/logrus" "github.com/stampzilla/gocast/api" "github.com/stampzilla/gocast/events" "github.com/stampzilla/gocast/responses" @@ -23,28 +23,27 @@ func (r *Receiver) Connect() { } func (r *Receiver) Disconnect() { - r.knownApplications = make(map[string]responses.ApplicationSession, 0) + r.knownApplications = make(map[string]responses.ApplicationSession) } func (r *Receiver) Unmarshal(message string) { - log.Println("Receiver received: ", message) + logrus.Debug("Receiver received: ", message) response := &responses.ReceiverResponse{} err := json.Unmarshal([]byte(message), response) - if err != nil { - log.Printf("Failed to unmarshal status message:%s - %s\n", err, message) + logrus.Errorf("Failed to unmarshal status message:%s - %s\n", err, message) return } - if response.Type != responses.TypeStatus { //Probably an error like: {"reason":"CANCELLED","requestId":2,"type":"LAUNCH_ERROR"} - log.Println("Type RECEIVER_STATUS expected. Skipping.") + if response.Type != responses.TypeStatus { // Probably an error like: {"reason":"CANCELLED","requestId":2,"type":"LAUNCH_ERROR"} + logrus.Debugf("Type RECEIVER_STATUS expected got: %s", response.Type) return } - prev := make(map[string]responses.ApplicationSession, 0) + prev := make(map[string]responses.ApplicationSession) if r.knownApplications == nil { - r.knownApplications = make(map[string]responses.ApplicationSession, 0) + r.knownApplications = make(map[string]responses.ApplicationSession) } // Make a copy of known applications @@ -53,7 +52,7 @@ func (r *Receiver) Unmarshal(message string) { } for _, app := range response.Status.Applications { - // App allready running + // App already running if _, ok := prev[app.AppID]; ok { // Remove it from the list of previous known apps delete(prev, app.AppID) @@ -64,10 +63,6 @@ func (r *Receiver) Unmarshal(message string) { r.knownApplications[app.AppID] = *app r.Dispatch(events.AppStarted{app}) - //AppID: app.AppID, - //DisplayName: app.DisplayName, - //TransportId: app.TransportId, - //}) } // Loop thru all stopped apps @@ -75,10 +70,6 @@ func (r *Receiver) Unmarshal(message string) { delete(r.knownApplications, key) r.Dispatch(events.AppStopped{&app}) - //AppID: app.AppID, - //DisplayName: app.DisplayName, - //TransportId: app.TransportId, - //}) } r.Dispatch(events.ReceiverStatus{ @@ -86,6 +77,7 @@ func (r *Receiver) Unmarshal(message string) { }) r.status = response.Status } + func (r *Receiver) GetSessionByAppId(appId string) *responses.ApplicationSession { for _, app := range r.knownApplications { if app.AppID == appId { @@ -100,10 +92,9 @@ type LaunchRequest struct { AppId string `json:"appId"` } -var ErrAppAlreadyLaunched = fmt.Errorf("App already launched") +var ErrAppAlreadyLaunched = fmt.Errorf("app already launched") func (r *Receiver) LaunchApp(appId string) error { - //already launched? if app := r.GetSessionByAppId(appId); app != nil { return ErrAppAlreadyLaunched } @@ -115,7 +106,7 @@ func (r *Receiver) LaunchApp(appId string) error { return err } -//TODO maybe do 0-100 instead of 0.0 to 1.0? +// TODO maybe do 0-100 instead of 0.0 to 1.0? func (r *Receiver) SetVolume(volume float64) (*api.CastMessage, error) { return r.Request(&responses.ReceiverStatus{ Headers: responses.Headers{Type: "SET_VOLUME"}, diff --git a/packetstream.go b/packetstream.go index 9e33fb9..15fbd77 100644 --- a/packetstream.go +++ b/packetstream.go @@ -4,6 +4,8 @@ import ( "encoding/binary" "fmt" "io" + + "github.com/sirupsen/logrus" ) type packetStream struct { @@ -31,27 +33,26 @@ func (w *packetStream) readPackets() { go func() { for { - err := binary.Read(w.stream, binary.BigEndian, &length) if err != nil { - fmt.Printf("Failed binary.Read packet: %s", err) + logrus.Errorf("Failed binary.Read packet: %s", err) w.packets <- packetContainer{err: err, payload: nil} return } - //TODO make sure this goroutine is killed on disconnect + // TODO make sure this goroutine is killed on disconnect if length > 0 { packet := make([]byte, length) i, err := w.stream.Read(packet) if err != nil { - fmt.Printf("Failed to read packet: %s", err) + logrus.Errorf("Failed to read packet: %s", err) continue } if i != int(length) { - fmt.Printf("Invalid packet size. Wanted: %d Read: %d", length, i) + logrus.Errorf("Invalid packet size. Wanted: %d Read: %d", length, i) continue } @@ -60,7 +61,6 @@ func (w *packetStream) readPackets() { err: nil, } } - } }() } @@ -74,9 +74,7 @@ func (w *packetStream) Read() (*[]byte, error) { } func (w *packetStream) Write(data []byte) (int, error) { - err := binary.Write(w.stream, binary.BigEndian, uint32(len(data))) - if err != nil { err = fmt.Errorf("Failed to write packet length %d. error:%s\n", len(data), err) return 0, err diff --git a/responses/headers.go b/responses/headers.go index 92955a8..9c1f50d 100644 --- a/responses/headers.go +++ b/responses/headers.go @@ -1,14 +1,16 @@ package responses -const TypePing = "PING" -const TypePong = "PONG" -const TypeStatus = "RECEIVER_STATUS" -const TypeAppAvailability = "GET_APP_AVAILABILITY" -const TypeInvalid = "INVALID_REQUEST" -const TypeMediaStatus = "MEDIA_STATUS" -const TypeClose = "CLOSE" -const TypeLoadFailed = "LOAD_FAILED" -const TypeLaunchError = "LAUNCH_ERROR" +const ( + TypePing = "PING" + TypePong = "PONG" + TypeStatus = "RECEIVER_STATUS" + TypeAppAvailability = "GET_APP_AVAILABILITY" + TypeInvalid = "INVALID_REQUEST" + TypeMediaStatus = "MEDIA_STATUS" + TypeClose = "CLOSE" + TypeLoadFailed = "LOAD_FAILED" + TypeLaunchError = "LAUNCH_ERROR" +) type Headers struct { Type string `json:"type"` diff --git a/subscription.go b/subscription.go index 7a8be28..6a429ca 100644 --- a/subscription.go +++ b/subscription.go @@ -33,7 +33,7 @@ func (s *Subscription) Send(payload responses.Payload) error { return s.Device.Send(s.Urn, s.SourceId, s.DestinationId, payload) } -// Request works like send, but waits for resposne to requestId before returning +// Request works like send, but waits for resposne to requestId before returning. func (s *Subscription) Request(payload responses.Payload) (*api.CastMessage, error) { requestId := int(atomic.AddInt64(&s.requestId, 1)) payload.SetRequestId(requestId) @@ -41,7 +41,7 @@ func (s *Subscription) Request(payload responses.Payload) (*api.CastMessage, err response := make(chan *api.CastMessage) s.inFlight[requestId] = response - //err := s.Send(payload) + // err := s.Send(payload) err := s.Device.Send(s.Urn, s.SourceId, s.DestinationId, payload) if err != nil { delete(s.inFlight, requestId) @@ -66,7 +66,7 @@ func (s *Subscription) Receive(message *api.CastMessage, headers *responses.Head s.Handler.Unmarshal(message.GetPayloadUtf8()) - //if this is a request we must send the response back to the pending request + // if this is a request we must send the response back to the pending request if headers.RequestId != nil && *headers.RequestId != 0 { if listener, ok := s.inFlight[*headers.RequestId]; ok { listener <- message