@@ -23,7 +23,6 @@ package main
2323
2424import (
2525 "log"
26- "strings"
2726 "time"
2827)
2928
@@ -34,7 +33,6 @@ const (
3433type ChannellingAPI interface {
3534 OnConnect (Client , * Session )
3635 OnIncoming (ResponseSender , * Session , * DataIncoming )
37- OnDisconnect (* Session )
3836}
3937
4038type channellingAPI struct {
@@ -46,11 +44,9 @@ type channellingAPI struct {
4644 ContactManager
4745 TurnDataCreator
4846 Unicaster
49- Broadcaster
50- buddyImages ImageCache
5147}
5248
53- func NewChannellingAPI (config * Config , roomStatus RoomStatusManager , sessionEncoder SessionEncoder , sessionManager SessionManager , statsCounter StatsCounter , contactManager ContactManager , turnDataCreator TurnDataCreator , unicaster Unicaster , broadcaster Broadcaster , buddyImages ImageCache ) ChannellingAPI {
49+ func NewChannellingAPI (config * Config , roomStatus RoomStatusManager , sessionEncoder SessionEncoder , sessionManager SessionManager , statsCounter StatsCounter , contactManager ContactManager , turnDataCreator TurnDataCreator , unicaster Unicaster ) ChannellingAPI {
5450 return & channellingAPI {
5551 config ,
5652 roomStatus ,
@@ -60,8 +56,6 @@ func NewChannellingAPI(config *Config, roomStatus RoomStatusManager, sessionEnco
6056 contactManager ,
6157 turnDataCreator ,
6258 unicaster ,
63- broadcaster ,
64- buddyImages ,
6559 }
6660}
6761
@@ -77,40 +71,31 @@ func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *D
7771 case "Hello" :
7872 //log.Println("Hello", msg.Hello, c.Index())
7973 // TODO(longsleep): Filter room id and user agent.
80- api .UpdateSession (session , & SessionUpdate {Types : []string {"Ua" }, Ua : msg .Hello .Ua })
81- if session .Hello && session .Roomid != msg .Hello .Id {
82- api .LeaveRoom (session )
83- api .Broadcast (session , session .DataSessionLeft ("soft" ))
84- }
74+ session .Update (& SessionUpdate {Types : []string {"Ua" }, Ua : msg .Hello .Ua })
8575
76+ room , err := session .JoinRoom (msg .Hello .Id , msg .Hello .Credentials , c )
8677 // NOTE(lcooper): Iid filtered for compatibility's sake.
8778 // Evaluate sending unconditionally when supported by all clients.
88- if room , err := api .JoinRoom (msg .Hello .Id , msg .Hello .Credentials , session , c ); err == nil {
89- session .Hello = true
90- session .Roomid = msg .Hello .Id
91- if msg .Iid != "" {
79+ if msg .Iid != "" {
80+ if err == nil {
9281 c .Reply (msg .Iid , & DataWelcome {
9382 Type : "Welcome" ,
9483 Room : room ,
9584 Users : api .RoomUsers (session ),
9685 })
97- }
98- api .Broadcast (session , session .DataSessionJoined ())
99- } else {
100- session .Hello = false
101- if msg .Iid != "" {
86+ } else {
10287 c .Reply (msg .Iid , err )
10388 }
10489 }
10590 case "Offer" :
10691 // TODO(longsleep): Validate offer
107- api .Unicast (session , msg .Offer .To , msg .Offer )
92+ session .Unicast (msg .Offer .To , msg .Offer )
10893 case "Candidate" :
10994 // TODO(longsleep): Validate candidate
110- api .Unicast (session , msg .Candidate .To , msg .Candidate )
95+ session .Unicast (msg .Candidate .To , msg .Candidate )
11196 case "Answer" :
11297 // TODO(longsleep): Validate Answer
113- api .Unicast (session , msg .Answer .To , msg .Answer )
98+ session .Unicast (msg .Answer .To , msg .Answer )
11499 case "Users" :
115100 if session .Hello {
116101 sessions := & DataSessions {Type : "Users" , Users : api .RoomUsers (session )}
@@ -125,27 +110,27 @@ func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *D
125110 if err := api .Authenticate (session , st , "" ); err == nil {
126111 log .Println ("Authentication success" , session .Userid )
127112 api .SendSelf (c , session )
128- api . BroadcastSessionStatus ( session )
113+ session . BroadcastStatus ( )
129114 } else {
130115 log .Println ("Authentication failed" , err , st .Userid , st .Nonce )
131116 }
132117 case "Bye" :
133- api .Unicast (session , msg .Bye .To , msg .Bye )
118+ session .Unicast (msg .Bye .To , msg .Bye )
134119 case "Status" :
135120 //log.Println("Status", msg.Status)
136- api . UpdateSession ( session , & SessionUpdate {Types : []string {"Status" }, Status : msg .Status .Status })
137- api . BroadcastSessionStatus ( session )
121+ session . Update ( & SessionUpdate {Types : []string {"Status" }, Status : msg .Status .Status })
122+ session . BroadcastStatus ( )
138123 case "Chat" :
139124 // TODO(longsleep): Limit sent chat messages per incoming connection.
140125 if ! msg .Chat .Chat .NoEcho {
141- api .Unicast (session , session .Id , msg .Chat )
126+ session .Unicast (session .Id , msg .Chat )
142127 }
143128 msg .Chat .Chat .Time = time .Now ().Format (time .RFC3339 )
144129 if msg .Chat .To == "" {
145130 // TODO(longsleep): Check if chat broadcast is allowed.
146131 if session .Hello {
147132 api .CountBroadcastChat ()
148- api .Broadcast (session , msg .Chat )
133+ session .Broadcast (msg .Chat )
149134 }
150135 } else {
151136 if msg .Chat .Chat .Status != nil && msg .Chat .Chat .Status .ContactRequest != nil {
@@ -159,10 +144,10 @@ func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *D
159144 api .CountUnicastChat ()
160145 }
161146
162- api .Unicast (session , msg .Chat .To , msg .Chat )
147+ session .Unicast (msg .Chat .To , msg .Chat )
163148 if msg .Chat .Chat .Mid != "" {
164149 // Send out delivery confirmation status chat message.
165- api .Unicast (session , session .Id , & DataChat {To : msg .Chat .To , Type : "Chat" , Chat : & DataChatMessage {Mid : msg .Chat .Chat .Mid , Status : & DataChatStatus {State : "sent" }}})
150+ session .Unicast (session .Id , & DataChat {To : msg .Chat .To , Type : "Chat" , Chat : & DataChatMessage {Mid : msg .Chat .Chat .Mid , Status : & DataChatStatus {State : "sent" }}})
166151 }
167152 }
168153 case "Conference" :
@@ -173,7 +158,7 @@ func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *D
173158 // Send conference update to anyone.
174159 for _ , id := range msg .Conference .Conference {
175160 if id != session .Id {
176- api .Unicast (session , id , msg .Conference )
161+ session .Unicast (id , msg .Conference )
177162 }
178163 }
179164 }
@@ -211,7 +196,7 @@ func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *D
211196 }
212197 case "Room" :
213198 if room , err := api .UpdateRoom (session , msg .Room ); err == nil {
214- api .Broadcast (session , room )
199+ session .Broadcast (room )
215200 c .Reply (msg .Iid , room )
216201 } else {
217202 c .Reply (msg .Iid , err )
@@ -221,23 +206,6 @@ func (api *channellingAPI) OnIncoming(c ResponseSender, session *Session, msg *D
221206 }
222207}
223208
224- func (api * channellingAPI ) OnDisconnect (session * Session ) {
225- dsl := session .DataSessionLeft ("hard" )
226- if session .Hello {
227- api .LeaveRoom (session )
228- api .Broadcast (session , dsl )
229- }
230-
231- session .RunForAllSubscribers (func (session * Session ) {
232- log .Println ("Notifying subscriber that we are gone" , session .Id , session .Id )
233- api .Unicast (session , session .Id , dsl )
234- })
235-
236- api .Unicaster .OnDisconnect (session )
237-
238- api .buddyImages .Delete (session .Id )
239- }
240-
241209func (api * channellingAPI ) SendSelf (c Responder , session * Session ) {
242210 token , err := api .EncodeSessionToken (session )
243211 if err != nil {
@@ -259,26 +227,3 @@ func (api *channellingAPI) SendSelf(c Responder, session *Session) {
259227 }
260228 c .Reply ("" , self )
261229}
262-
263- func (api * channellingAPI ) UpdateSession (session * Session , s * SessionUpdate ) uint64 {
264- if s .Status != nil {
265- status , ok := s .Status .(map [string ]interface {})
266- if ok && status ["buddyPicture" ] != nil {
267- pic := status ["buddyPicture" ].(string )
268- if strings .HasPrefix (pic , "data:" ) {
269- imageId := api .buddyImages .Update (session .Id , pic [5 :])
270- if imageId != "" {
271- status ["buddyPicture" ] = "img:" + imageId
272- }
273- }
274- }
275- }
276-
277- return session .Update (s )
278- }
279-
280- func (api * channellingAPI ) BroadcastSessionStatus (session * Session ) {
281- if session .Hello {
282- api .Broadcast (session , session .DataSessionStatus ())
283- }
284- }
0 commit comments