@@ -90,25 +90,56 @@ type Application struct {
9090 playedItems map [string ]PlayedItem
9191 cacheDisabled bool
9292 cache * storage.Storage
93+
94+ // Number of connection retries to try before returning
95+ // and error.
96+ connectionRetries int
97+ }
98+
99+ type ApplicationOption func (* Application )
100+
101+ func WithIface (iface * net.Interface ) ApplicationOption {
102+ return func (a * Application ) {
103+ a .iface = iface
104+ }
93105}
94106
95- func NewApplication (iface * net.Interface , debug , cacheDisabled bool ) * Application {
96- // TODO(vishen): make cast.Connection an interface, most likely will just need
97- // the Send method
98- // Channel to receive messages from the cast connecttion. 5 is a randomly
99- // chosen number.
107+ func WithDebug (debug bool ) ApplicationOption {
108+ return func (a * Application ) {
109+ a .debug = debug
110+ a .conn .SetDebug (debug )
111+ }
112+ }
113+
114+ func WithCacheDisabled (cacheDisabled bool ) ApplicationOption {
115+ return func (a * Application ) {
116+ a .cacheDisabled = cacheDisabled
117+ }
118+ }
119+
120+ func WithConnectionRetries (connectionRetries int ) ApplicationOption {
121+ return func (a * Application ) {
122+ a .connectionRetries = connectionRetries
123+ }
124+ }
125+
126+ func NewApplication (opts ... ApplicationOption ) * Application {
100127 recvMsgChan := make (chan * pb.CastMessage , 5 )
101128 a := & Application {
102- recvMsgChan : recvMsgChan ,
103- resultChanMap : map [int ]chan * pb.CastMessage {},
104- messageChan : make (chan * pb.CastMessage ),
105- conn : cast .NewConnection (recvMsgChan , debug ),
106- debug : debug ,
107- cacheDisabled : cacheDisabled ,
108- playedItems : map [string ]PlayedItem {},
109- cache : storage .NewStorage (),
110- iface : iface ,
129+ recvMsgChan : recvMsgChan ,
130+ resultChanMap : map [int ]chan * pb.CastMessage {},
131+ messageChan : make (chan * pb.CastMessage ),
132+ conn : cast .NewConnection (recvMsgChan ),
133+ playedItems : map [string ]PlayedItem {},
134+ cache : storage .NewStorage (),
135+ connectionRetries : 5 ,
136+ }
137+
138+ // Apply options
139+ for _ , o := range opts {
140+ o (a )
111141 }
142+
112143 // Kick off the listener for asynchronous messages received from the
113144 // cast connection.
114145 go a .recvMessages ()
@@ -119,6 +150,7 @@ func NewApplication(iface *net.Interface, debug, cacheDisabled bool) *Applicatio
119150
120151func (a * Application ) Application () * cast.Application { return a .application }
121152func (a * Application ) Media () * cast.Media { return a .media }
153+ func (a * Application ) Volume () * cast.Volume { return a .volumeReceiver }
122154
123155func (a * Application ) AddMessageFunc (f CastMessageFunc ) {
124156 a .messageMu .Lock ()
@@ -236,7 +268,10 @@ func (a *Application) Update() error {
236268 var err error
237269 // Simple retry. We need this for when the device isn't currently
238270 // available, but it is likely that it will come up soon.
239- for i := 0 ; i < 5 ; i ++ {
271+ // TODO: This seems to happen when changing media on the cast device,
272+ // not sure how to fix but there might be some way of knowing from the
273+ // payload?
274+ for i := 0 ; i < a .connectionRetries ; i ++ {
240275 recvStatus , err = a .getReceiverStatus ()
241276 if err == nil {
242277 break
0 commit comments