KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > plugin > TalkPluginAppClient


1 package com.quikj.application.web.talk.plugin;
2
3 import com.quikj.server.framework.*;
4 import com.quikj.server.web.*;
5 import com.quikj.application.web.talk.messaging.*;
6
7 import java.util.*;
8
9
10 public class TalkPluginAppClient implements PluginAppClientInterface
11 {
12     public TalkPluginAppClient()
13     {
14     }
15     
16     public boolean newConnection(String JavaDoc host, HTTPEndPoint parent)
17     {
18         this.host = host;
19         this.parent = parent;
20         return true;
21     }
22     
23     public boolean requestReceived(int request_id,
24     String JavaDoc content_type,
25     String JavaDoc body)
26     {
27         //System.out.println (Thread.currentThread().getName() + " RX\n" + body + "---");
28
// do some error checking
29
if (checkRequestMessage(content_type, body) == false)
30         {
31             return true;
32         }
33         
34         // next, parse the message
35
TalkMessageParser parser = null;
36         try
37         {
38             parser = new TalkMessageParser(XMLBuilder.Instance().getDocumentBuilder());
39         }
40         catch (AceMessageException ex)
41         {
42             // print error message
43
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
44             Thread.currentThread().getName()
45             + "- TalkPluginAppClient.requestReceived() -- Could not obtain XML parser: "
46             + ex.getMessage());
47             
48             return true;
49         }
50         
51         if (parser.parse(body, true) == false)
52         {
53             // print error message
54
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
55             Thread.currentThread().getName()
56             + "- TalkPluginAppClient.requestReceived() -- Could not parse the received message: "
57             + parser.getErrorMessage());
58             
59             System.out.println (body + "\n");
60             
61             return true;
62         }
63         
64         TalkMessageInterface message = parser.getMessage();
65         
66         if ((message instanceof RegistrationRequestMessage) == true)
67         {
68             return processRegistrationRequestMessage(request_id,
69             (RegistrationRequestMessage)message);
70         }
71         else if ((message instanceof SetupRequestMessage) == true)
72         {
73             return processSetupRequestMessage(request_id, (SetupRequestMessage)message);
74         }
75         else if ((message instanceof RTPMessage) == true)
76         {
77             return processRTPMessage((RTPMessage)message);
78         }
79         else if ((message instanceof DisconnectMessage) == true)
80         {
81             return processDisconnectMessage((DisconnectMessage)message);
82         }
83         else if ((message instanceof UserToUserMessage) == true)
84         {
85             return processUserToUserRequestMessage(request_id,
86             (UserToUserMessage)message);
87         }
88         else
89         {
90             // unknown type of message, send it to the service controller
91
MessageEvent me = new MessageEvent(
92             MessageEvent.CLIENT_REQUEST_MESSAGE,
93             parent,
94             message,
95             null,
96             request_id);
97             if (ServiceController.Instance().sendMessage(me) == false)
98             {
99                 // print error message
100
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
101                 Thread.currentThread().getName()
102                 + "- TalkPluginAppClient.requestReceived() -- Error sending client request message to the service controller");
103                 return false;
104             }
105             else
106             {
107                 return true;
108             }
109         }
110     }
111     
112     public boolean responseReceived(int request_id,
113     int status,
114     String JavaDoc reason,
115     String JavaDoc content_type,
116     String JavaDoc body)
117     {
118         // do some error checking
119
if (checkResponseMessage(content_type, body) == false)
120         {
121             return false;
122         }
123         
124         // parse the message
125
TalkMessageParser parser = null;
126         
127         try
128         {
129             parser = new TalkMessageParser(XMLBuilder.Instance().getDocumentBuilder());
130         }
131         catch (AceMessageException ex)
132         {
133             // print error message
134
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
135             Thread.currentThread().getName()
136             + "- TalkPluginAppClient.responseReceived() -- Could not obtain XML parser: "
137             + ex.getMessage());
138             return false;
139         }
140         
141         if (parser.parse(body, false) == false)
142         {
143             // print error message
144
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
145             Thread.currentThread().getName()
146             + "- TalkPluginAppClient.responseReceived() -- Could not parse the received message: "
147             + parser.getErrorMessage());
148             return false;
149         }
150         
151         TalkMessageInterface message = parser.getMessage();
152         if ((message instanceof SetupResponseMessage) == true)
153         {
154             return processSetupResponseMessage(status, reason,
155             (SetupResponseMessage)message);
156         }
157         else if ((message instanceof UserToUserMessage) == true)
158         {
159             return processUserToUserResponseMessage(status, reason,
160             (UserToUserMessage)message);
161         }
162         else
163         {
164             if (ServiceController.Instance().sendMessage(new MessageEvent(
165             MessageEvent.CLIENT_RESPONSE_MESSAGE,
166             parent,
167             status,
168             reason,
169             message,
170             null))
171             == false)
172             {
173                 // print error message
174
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
175                 Thread.currentThread().getName()
176                 + "- TalkPluginAppClient.responseReceived() -- Error sending client response message to the service controller");
177                 return false;
178             }
179             else
180             {
181                 return true;
182             }
183         }
184     }
185     
186     public boolean eventReceived(AceMessageInterface event)
187     {
188         if ((event instanceof MessageEvent) == true)
189         {
190             MessageEvent event_rcvd = (MessageEvent)event;
191             
192             switch (event_rcvd.getEventType())
193             {
194                 case MessageEvent.REGISTRATION_RESPONSE:
195                     return processRegistrationResponseEvent(event_rcvd);
196                     
197                 case MessageEvent.SETUP_RESPONSE:
198                     return processSetupResponseEvent(event_rcvd);
199                     
200                 case MessageEvent.SETUP_REQUEST:
201                     return processSetupRequestEvent(event_rcvd);
202                     
203                 case MessageEvent.DISCONNECT_MESSAGE:
204                     return processDisconnectEvent(event_rcvd);
205                     
206                 case MessageEvent.RTP_MESSAGE:
207                     return processRTPEvent(event_rcvd);
208                     
209                 default:
210                     // unexpected message event, send it to the client
211

212                     if (event_rcvd.isRequest() == true) // request message
213
{
214                         if (parent.sendRequestMessageToClient(event_rcvd.getRequestId(),
215                         "text/xml",
216                         event_rcvd.getMessage().format(),
217                         true) == false)
218                         {
219                             // print error message
220
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
221                             Thread.currentThread().getName()
222                             + "- TalkPluginAppClient.eventReceived() -- Error sending server request message to the endpoint");
223                             return false;
224                         }
225                     }
226                     else // response message
227
{
228                         if (parent.sendResponseMessageToClient(event_rcvd.getRequestId(),
229                         event_rcvd.getResponseStatus(),
230                         event_rcvd.getReason(),
231                         "text/xml",
232                         event_rcvd.getMessage().format(),
233                         true) == false)
234                         {
235                             // print an error message
236
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
237                             Thread.currentThread().getName()
238                             + "- TalkPluginAppClient.eventReceived() -- Error sending server response message to the endpoint");
239                             return false;
240                         }
241                     }
242                     return true;
243             }
244         }
245         else if ((event instanceof ActionEvent) == true)
246         {
247             return processActionEvent((ActionEvent)event);
248         }
249         else if ((event instanceof InformationEvent) == true)
250         {
251             return processInformationEvent((InformationEvent)event);
252         }
253         else if ((event instanceof DropEndpointEvent) == true)
254         {
255             return processDropEndpointEvent((DropEndpointEvent)event);
256         }
257         else
258         {
259             // unexpected event
260

261             // print error message
262
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
263             Thread.currentThread().getName()
264             + "- TalkPluginAppClient.eventReceived() -- Unknown type of event received: "
265             + event.messageType());
266             return false;
267         }
268     }
269     
270     public void connectionClosed()
271     {
272         // equivalent to receiving a disconnect for all sessions
273

274         Enumeration sessions = callList.elements();
275         
276         while (sessions.hasMoreElements() == true)
277         {
278             SessionInfo session_info = (SessionInfo)sessions.nextElement();
279             
280             DisconnectMessage message = new DisconnectMessage();
281             message.setSessionId(session_info.getSessionId());
282             DisconnectReasonElement disc_element = new DisconnectReasonElement();
283             disc_element.setReasonCode(0);
284             disc_element.setReasonText(java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language", ServiceController.getLocale(parent.getParam("language"))).getString("end_point_disconnected"));
285             message.setDisconnectReason(disc_element);
286             
287             if (ServiceController.Instance().sendMessage(new MessageEvent(MessageEvent.DISCONNECT_MESSAGE,
288             parent,
289             message,
290             null)) == false)
291             {
292                 // print error message
293
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
294                 Thread.currentThread().getName()
295                 + "- TalkPluginAppClient.connectionClosed() -- Error sending disconnect message to the service controller");
296             }
297         }
298         
299         callList.clear();
300         
301         if (registered == true)
302         {
303             if (unregistrationComplete == false)
304             {
305                 if (ServiceController.Instance().sendMessage(new UnregistrationEvent(registeredUserName)) == false)
306                 {
307                     // print error message
308
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
309                     Thread.currentThread().getName()
310                     + "- TalkPluginAppClient.connectionClosed() -- Error sending unregistration message to the service controller");
311                 }
312             }
313         }
314     }
315     
316     private boolean checkResponseMessage(String JavaDoc content_type, String JavaDoc body)
317     {
318         if (body != null)
319         {
320             if (content_type.equalsIgnoreCase("text/xml") == false)
321             {
322                 // print error message
323
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
324                 Thread.currentThread().getName()
325                 + "- TalkPluginAppClient.checkResponseMessage() -- Content type of a response message is not text/xml");
326                 return false;
327             }
328         }
329         return true;
330     }
331         
332     private boolean checkRequestMessage(String JavaDoc content_type, String JavaDoc body)
333     {
334         if (body == null)
335         {
336             // print error message
337
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
338             Thread.currentThread().getName()
339             + "- TalkPluginAppClient.checkRequestMessage() -- Request message does not have a body");
340             return false;
341         }
342         
343         if (body.length() <= 0)
344         {
345             // print error message
346
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
347             Thread.currentThread().getName()
348             + "- TalkPluginAppClient.checkRequestMessage() -- Request message does not has a zero-length body");
349             return false;
350         }
351         
352         if (content_type.equalsIgnoreCase("text/xml") == false)
353         {
354             // print error message
355
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
356             Thread.currentThread().getName()
357             + "- TalkPluginAppClient.checkRequestMessage() -- Content type of a response message is not text/xml");
358             return false;
359         }
360         return true;
361     }
362     
363     private boolean processUserToUserResponseMessage(int status, String JavaDoc reason,
364     UserToUserMessage message)
365     {
366         String JavaDoc name = message.getEndPointName();
367         if (name == null)
368         {
369             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
370             Thread.currentThread().getName()
371             + "- TalkPluginAppClient.processUserToUserResponseMessage() -- user to user message does not contain the end-point name parameter");
372             
373             return false;
374         }
375         
376         EndPointInterface endpoint = EndPointList.Instance().findRegisteredEndPoint(name);
377         if (endpoint == null)
378         {
379             // print error message
380
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
381             Thread.currentThread().getName()
382             + "- TalkPluginAppClient.processRegistrationResponseMessage() -- Endpoint does not exist - coluld not send response event");
383             return true;
384         }
385         
386         if (endpoint.sendEvent(new MessageEvent(
387         MessageEvent.CLIENT_RESPONSE_MESSAGE,
388         parent,
389         status,
390         reason,
391         message,
392         null))
393         == false)
394         {
395             // print error message
396
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
397             Thread.currentThread().getName()
398             + "- TalkPluginAppClient.processUserToUserResponseMessage() -- Error sending user-to-user response message to the endpoint");
399             return false;
400         }
401         
402         return true;
403     }
404     
405     private boolean processUserToUserRequestMessage(int request_id, UserToUserMessage message)
406     {
407         String JavaDoc name = message.getEndPointName();
408         if (name == null)
409         {
410             AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
411             Thread.currentThread().getName()
412             + "- TalkPluginAppClient.processUserToUserRequestMessage() -- user to user message does not contain the end-point name parameter");
413             
414             return false;
415         }
416         
417         EndPointInterface endpoint = EndPointList.Instance().findRegisteredEndPoint(name);
418         if (endpoint == null)
419         {
420             // send a response to the client saying that the end-point is not available
421
if (parent.sendResponseMessageToClient(request_id,
422             AceHTTPMessage.SERVICE_UNAVAILABLE,
423             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language",
424             ServiceController.getLocale(parent.getParam("language"))).getString("The_end_point_is_not_available"),
425             "text/xml",
426             null,
427             true) == false)
428             {
429                 // print error message
430
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
431                 Thread.currentThread().getName()
432                 + "- TalkPluginAppClient.processRegistrationRequestMessage() -- Error sending user-to-user response message to the endpoint");
433                 return false;
434             }
435             return true;
436         }
437         
438         MessageEvent me = new MessageEvent(
439         MessageEvent.CLIENT_REQUEST_MESSAGE,
440         parent,
441         message,
442         null,
443         request_id);
444         if (endpoint.sendEvent(me) == false)
445         {
446             // print error message
447
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
448             Thread.currentThread().getName()
449             + "- TalkPluginAppClient.processUserToUserRequestMessage() -- Error sending client request message to endpoint "
450             + name);
451             
452             // send a response to the client saying that the end-point is not available
453
if (parent.sendResponseMessageToClient(request_id,
454             AceHTTPMessage.SERVICE_UNAVAILABLE,
455             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language",
456             ServiceController.getLocale(parent.getParam("language"))).getString("The_end_point_is_not_available"),
457             "text/xml",
458             null,
459             true) == false)
460             {
461                 // print error message
462
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
463                 Thread.currentThread().getName()
464                 + "- TalkPluginAppClient.processRegistrationRequestMessage() -- Error sending user-to-user response message to the endpoint");
465                 return false;
466             }
467             
468             return true;
469         }
470         
471         return true;
472     }
473     
474     private boolean processRegistrationRequestMessage(int request_id,
475     RegistrationRequestMessage message)
476     {
477         AceNetworkAccess access = TalkPluginApp.Instance().getAccessInfo();
478         if (access != null) // specified in the config file
479
{
480             if (access.match(host) == false)
481             {
482                 // print error message
483
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
484                 Thread.currentThread().getName()
485                 + "- TalkPluginAppClient.processRegistrationRequestMessage() -- Unauthorized registered user access from host "
486                 + host);
487                 
488                 if (parent.sendResponseMessageToClient(request_id,
489                 AceHTTPMessage.FORBIDDEN,
490                 java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language", ServiceController.getLocale(parent.getParam("language"))).getString("You_are_not_allowed_to_login_from_this_location"),
491                 "text/xml",
492                 null,
493                 true) == false)
494                 {
495                     // print error message
496
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
497                     Thread.currentThread().getName()
498                     + "- TalkPluginAppClient.processRegistrationRequestMessage() -- Error sending registration response message to the endpoint");
499                 }
500                 
501                 return false; // no point continuing
502
}
503         }
504         
505         if (registered == false) // not registered
506
{
507             registeredUserName = message.getUserName();
508             
509             // send the message to the ServiceController
510
if (ServiceController.Instance().sendMessage(new MessageEvent(MessageEvent.REGISTRATION_REQUEST,
511             parent,
512             message,
513             null))
514             == false)
515             {
516                 // print error message
517
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
518                 Thread.currentThread().getName()
519                 + "- TalkPluginAppClient.processRegistrationRequestMessage() -- Error sending registration request message to the service controller");
520                 return false;
521             }
522             registrationRequestId = request_id;
523         }
524         else // already registered
525
{
526             // print error message
527
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
528             Thread.currentThread().getName()
529             + "- TalkPluginAppClient.processRegistrationRequestMessage() -- A registration message is received for a client that is already registered");
530             
531             if (parent.sendResponseMessageToClient(request_id,
532             AceHTTPMessage.FORBIDDEN,
533             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language", ServiceController.getLocale(parent.getParam("language"))).getString("Already_registered"),
534             "text/xml",
535             null,
536             true) == false)
537             {
538                 // print error message
539
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
540                 Thread.currentThread().getName()
541                 + "- TalkPluginAppClient.processRegistrationRequestMessage() -- Error sending registration response message to the endpoint");
542                 return false;
543             }
544             return true;
545         }
546         return true;
547     }
548     
549     private boolean processRegistrationResponseEvent(MessageEvent event)
550     {
551         if (registered == true) // if already registered
552
{
553             // something must be wrong
554

555             // print error message
556
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
557             Thread.currentThread().getName()
558             + "- TalkPluginAppClient.processRegistrationResponseEvent() -- A registration response event is received for a client that is already registered");
559             return false;
560         }
561         
562         
563         String JavaDoc resp_message_s = null;
564         RegistrationResponseMessage resp_message = (RegistrationResponseMessage)event.getMessage();
565         if (resp_message != null)
566         {
567             resp_message_s = resp_message.format();
568         }
569         
570         // send the response to the client
571
if (parent.sendResponseMessageToClient(registrationRequestId,
572         event.getResponseStatus(),
573         event.getReason(),
574         "text/xml",
575         resp_message_s,
576         true) == false)
577         {
578             // print an error message
579
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
580             Thread.currentThread().getName()
581             + "- TalkPluginAppClient.processRegistrationResponseEvent() -- Error sending registration response message to the endpoint");
582             return false;
583         }
584         
585         if (event.getResponseStatus() == AceHTTPMessage.OK)
586         {
587             registered = true;
588             
589             selfInfo = resp_message.getCallPartyInfo(); // save the information
590

591             if (resp_message != null)
592             {
593                 resp_message_s = resp_message.format();
594             }
595         }
596         else
597         {
598             return false;
599         }
600         
601         return true;
602     }
603     
604     private boolean processSetupRequestMessage(int request_id, SetupRequestMessage message)
605     {
606         // System.out.println(Thread.currentThread().getName()
607
// + " In processSetupRequestMessage");
608

609         if ((registered == false) && (callList.size() > 0))
610         {
611             // unregistered users get one call
612

613             // send a response
614
parent.sendResponseMessageToClient(request_id,
615             AceHTTPMessage.FORBIDDEN,
616             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language", ServiceController.getLocale(parent.getParam("language"))).getString("Only_one_call_allowed_for_unregistered_user"),
617             "text/xml",
618             null,
619             true);
620             return false;
621         }
622         
623         // get a unique session id
624
long session_id = ServiceController.Instance().getNewSessionId();
625         message.setSessionId(session_id); // save the session id
626

627         // send an ACK response
628
SetupResponseMessage rsp = new SetupResponseMessage();
629         rsp.setSessionId(session_id);
630         if (parent.sendResponseMessageToClient(request_id,
631         SetupResponseMessage.ACK,
632         "Acknowledgement",
633         "text/xml",
634         rsp.format(),
635         true) == false)
636         {
637             // print error message
638
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
639             Thread.currentThread().getName()
640             + "- TalkPluginAppClient.processSetupRequestMessage() -- Error sending message to the endpoint");
641             return false;
642         }
643         
644         // send the message to the service controller
645
if (ServiceController.Instance().sendMessage(new MessageEvent(MessageEvent.SETUP_REQUEST,
646         parent,
647         message,
648         null))
649         == false)
650         {
651             // print error message
652
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
653             Thread.currentThread().getName()
654             + "- TalkPluginAppClient.processSetupRequestMessage() -- Error sending message to the service controller");
655             
656             parent.sendResponseMessageToClient(request_id,
657             AceHTTPMessage.SERVICE_UNAVAILABLE,
658             java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.plugin.language", ServiceController.getLocale(parent.getParam("language"))).getString("Unable_to_notify_the_service_controller"),
659             "text/xml",
660             null,
661             true);
662             return false;
663         }
664         
665         
666         // if the end point is not registered. save the information about the caller
667
if (registered == false)
668         {
669             selfInfo = message.getCallingNameElement().getCallParty();
670         }
671         else
672         {
673             // for registered user, save the information about the caller only
674
// if the service controller did not provide such information during
675
// the registration.
676
if (selfInfo != null)
677             {
678                 selfInfo = message.getCallingNameElement().getCallParty();
679             }
680         }
681         
682         // create a session info
683
SessionInfo session = new SessionInfo(session_id, parent);
684         session.setRequestId(request_id);
685         
686         // and add it to the list of sessions
687
addToCallList(session_id, session);
688         
689         lastSession = session;
690         return true;
691     }
692     
693     private boolean processSetupResponseEvent(MessageEvent event)
694     {
695         // System.out.println(Thread.currentThread().getName()
696
// + " In processSetupResponseEvent");
697

698         // get the call information from the call list
699
SetupResponseMessage message_to_send = (SetupResponseMessage)event.getMessage();
700         long session_id = message_to_send.getSessionId();
701         SessionInfo session_info = getSessionInfo(session_id);
702         if (session_info == null) // not found
703
{
704             return true; // ignore
705
}
706         
707         if (session_info.isConnected() == true)
708         {
709             // we do not expect any setup response
710

711             // print error message
712
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
713             Thread.currentThread().getName()
714             + "- TalkPluginAppClient.processSetupResponseEvent() -- A setup response event is received for a call that is already in connected state");
715             // and ignore
716
return true;
717         }
718         
719         
720         int status = event.getResponseStatus();
721         EndPointInterface from = event.getFrom();
722         
723         // process the request
724
switch (status)
725         {
726             case SetupResponseMessage.ALERTING:
727                 if (session_info.numEndPoints() <= 1)
728                 {
729                     session_info.addEndPoint(from);
730                 }
731                 
732                 // send the alerting message to the client
733
if (parent.sendResponseMessageToClient(session_info.getRequestId(),
734                 status,
735                 event.getReason(),
736                 "text/xml",
737                 message_to_send.format(),
738                 true) == false)
739                 {
740                     // print an error message
741
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
742                     Thread.currentThread().getName()
743                     + "- TalkPluginAppClient.processSetupResponseEvent() -- Error sending setup response (ALERTING) to the endpoint");
744                     return false;
745                 }
746                 break;
747                 
748             case SetupResponseMessage.PROG:
749                 if (session_info.numEndPoints() <= 1)
750                 {
751                     session_info.addEndPoint(from);
752                 }
753                 
754                 //send the progress message to the client
755
if (parent.sendResponseMessageToClient(session_info.getRequestId(),
756                 status,
757                 event.getReason(),
758                 "text/xml",
759                 message_to_send.format(),
760                 true) == false)
761                 {
762                     // print an error message
763
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
764                     Thread.currentThread().getName()
765                     + "- TalkPluginAppClient.processSetupResponseEvent() -- Error sending setup response (PROG) to the endpoint");
766                     return false;
767                 }
768                 break;
769                 
770             case SetupResponseMessage.CONNECT:
771                 if (session_info.numEndPoints() <= 1)
772                 {
773                     session_info.addEndPoint(from);
774                 }
775                 
776                 // send the connect message to the client
777
if (parent.sendResponseMessageToClient(session_info.getRequestId(),
778                 status,
779                 event.getReason(),
780                 "text/xml",
781                 message_to_send.format(),
782                 true) == false)
783                 {
784                     // print an error message
785
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
786                     Thread.currentThread().getName()
787                     + "- TalkPluginAppClient.processSetupResponseEvent() -- Error sending setup response (CONNECT) to the endpoint");
788                     return false;
789                 }
790                 
791                 session_info.setConnected(true);
792                 break;
793                 
794                 
795             default:
796                 // all other cases, received a response that is a terminating response and not going
797
// to transition to connected state
798

799                 removeFromCallList(session_id);
800                 
801                 // send the message to the client
802
if (parent.sendResponseMessageToClient(session_info.getRequestId(),
803                 status,
804                 event.getReason(),
805                 "text/xml",
806                 message_to_send.format(),
807                 true) == false)
808                 {
809                     // print an error message
810
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
811                     Thread.currentThread().getName()
812                     + "- TalkPluginAppClient.processSetupResponseEvent() -- Error sending setup response ("
813                     + status
814                     + ") to the endpoint");
815                     return false;
816                 }
817                 break;
818         }
819         
820         
821         return true;
822     }
823     
824     private boolean processRTPMessage(RTPMessage message)
825     {
826         // get the call information from the call list
827
long session_id = message.getSessionId();
828         SessionInfo session_info = getSessionInfo(session_id);
829         if (session_info == null) // not found
830
{
831             return true;
832         }
833         
834         if (session_info.isConnected() == false)
835         {
836             // we do not expect any setup response
837

838             // print error message
839
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
840             Thread.currentThread().getName()
841             + "- TalkPluginAppClient.processRTPMessage() -- An RTP message is received for a call that is not connected");
842             // and ignore
843
return true;
844         }
845         
846         // if message processing is required at this end
847
if (message.isParse() == true)
848         {
849             // canned message feature has been removed,
850
// however, this thing may be useful in the future
851
}
852         
853         // propagate the message to the other terminals
854
int num_end_points = session_info.numEndPoints(); // max for num_end_points = 2
855

856         for (int i = 0; i < num_end_points; i++)
857         {
858             EndPointInterface element = session_info.elementAt(i);
859             
860             if (element != parent) // if not self
861
{
862                 if (element.sendEvent(new MessageEvent(MessageEvent.RTP_MESSAGE,
863                 parent,
864                 message,
865                 null)) == false)
866                 {
867                     // print error message
868
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
869                     Thread.currentThread().getName()
870                     + "- TalkPluginAppClient.processRTPMessage() -- Could not send RTP message to the endpoint "
871                     + element);
872                     // and ignore
873
return true;
874                 }
875             }
876         }
877         
878         return true;
879     }
880     
881     
882     private boolean processDisconnectMessage(DisconnectMessage message)
883     {
884         // System.out.println(Thread.currentThread().getName()
885
// + " In processDisconnectMessage");
886

887         // get the call information from the call list
888
long session_id = message.getSessionId();
889         
890         SessionInfo session_info = null;
891         if (session_id == -1) // not specified
892
{
893             session_info = lastSession;
894         }
895         else
896         {
897             session_info = getSessionInfo(session_id);
898         }
899         
900         if (session_info == null) // not found
901
{
902             return true;
903         }
904         
905         // propagate the message to the service controller
906
if (ServiceController.Instance().sendMessage(new MessageEvent(MessageEvent.DISCONNECT_MESSAGE,
907         parent,
908         message,
909         null)) == false)
910         {
911             // print error message
912
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
913             Thread.currentThread().getName()
914             + "- TalkPluginAppClient.processDisconnectMessage() -- Error sending message to the service controller");
915             
916             return false;
917         }
918         
919         // remove the session from the call list
920
removeFromCallList(session_id);
921         
922         if (registered == false)
923         {
924             // close the connection
925
return false;
926         }
927         
928         return true;
929     }
930     
931     private boolean processSetupRequestEvent(MessageEvent event)
932     {
933         // System.out.println(Thread.currentThread().getName()
934
// + " In processSetupRequestEvent");
935
if (registered == false)
936         {
937             // something must be wrong
938

939             // print error message
940
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
941             Thread.currentThread().getName()
942             + "- TalkPluginAppClient.processSetupRequestEvent() -- A setup request event is received for a session that is not registered");
943             return false;
944         }
945         
946         EndPointInterface calling_party = event.getFrom();
947         
948         SetupRequestMessage incoming_message = (SetupRequestMessage)event.getMessage();
949         
950         long session_id = incoming_message.getSessionId();
951         
952         // send an ALTERTING message to the calling party
953
SetupResponseMessage resp = new SetupResponseMessage();
954         resp.setSessionId(session_id);
955         
956         if (selfInfo != null)
957         {
958             CalledNameElement cp_element = new CalledNameElement();
959             cp_element.setCallParty(selfInfo);
960             resp.setCalledParty(cp_element);
961         }
962         
963         // send the message
964
if (calling_party.sendEvent(new MessageEvent(MessageEvent.SETUP_RESPONSE,
965         parent,
966         SetupResponseMessage.ALERTING,
967         "Alterting",
968         resp,
969         null)) == false)
970         {
971             // print error message
972
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
973             Thread.currentThread().getName()
974             + "- TalkPluginAppClient.processSetupRequestEvent() -- Error sending alerting event to the calling party");
975             return false;
976         }
977         
978         // send the message to the client
979
if (parent.sendRequestMessageToClient(0,
980         "text/xml",
981         incoming_message.format(),
982         true) == false)
983         {
984             // print error message
985
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
986             Thread.currentThread().getName()
987             + "- TalkPluginAppClient.processSetupRequestEvent() -- Error sending setup message to the endpoint");
988             
989             return false;
990         }
991         
992         // create a session
993
SessionInfo session = new SessionInfo(session_id, calling_party);
994         session.addEndPoint(parent);
995         
996         // add it to the session list
997
addToCallList(session_id, session);
998         
999         return true;
1000    }
1001    
1002    public boolean processSetupResponseMessage(int status, String JavaDoc reason,
1003    SetupResponseMessage message)
1004    {
1005        // System.out.println(Thread.currentThread().getName()
1006
// + " In processSetupResponseMessage");
1007

1008        long session_id = message.getSessionId();
1009        
1010        SessionInfo session = getSessionInfo(session_id);
1011        
1012        if (session == null)
1013        {
1014            return true;
1015        }
1016        
1017        if (session.isConnected() == true)
1018        {
1019            // we do not expect any setup response
1020

1021            // print error message
1022
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
1023            Thread.currentThread().getName()
1024            + "- TalkPluginAppClient.processSetupResponseMessage() -- A setup response message is received for a session that is already connected");
1025            
1026            // and ignore
1027
return true;
1028        }
1029        
1030        switch (status)
1031        {
1032            case SetupResponseMessage.CONNECT:
1033                session.setConnected(true);
1034                
1035                // propagate the message to the calling party via the service controller
1036
if (ServiceController.Instance().sendMessage(new MessageEvent(MessageEvent.SETUP_RESPONSE,
1037                parent,
1038                status,
1039                reason,
1040                message,
1041                null)) == false)
1042                {
1043                    // print error message
1044
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
1045                    Thread.currentThread().getName()
1046                    + "- TalkPluginAppClient.processSetupResponseMessage() -- Error sending message (CONNECT) to the service controller");
1047                }
1048                break;
1049                
1050            default:
1051                // all other cases, received a response that is a terminating response and not going
1052
// to transition to connected state
1053

1054                removeFromCallList(session_id);
1055                
1056                // propogate the message to the calling party via the service controller
1057
if (ServiceController.Instance().sendMessage(new MessageEvent(MessageEvent.SETUP_RESPONSE,
1058                parent,
1059                status,
1060                reason,
1061                message,
1062                null)) == false)
1063                {
1064                    // print error message
1065
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
1066                    Thread.currentThread().getName()
1067                    + "- TalkPluginAppClient.processSetupResponseMessage() -- Error sending message ("
1068                    + status
1069                    + ") to the service controller");
1070                    
1071                }
1072                break;
1073        }
1074        
1075        
1076        return true;
1077    }
1078    
1079    private boolean processRTPEvent(MessageEvent event)
1080    {
1081        // send the message to the client
1082
if (parent.sendRequestMessageToClient(0,
1083        "text/xml",
1084        ((RTPMessage)(event.getMessage())).format(),
1085        true) == false)
1086        {
1087            // print error message
1088
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
1089            Thread.currentThread().getName()
1090            + "- TalkPluginAppClient.processRTPEvent() -- Error sending RTP message to the endpoint");
1091            return false;
1092        }
1093        
1094        return true;
1095    }
1096    
1097    private boolean processDisconnectEvent(MessageEvent event)
1098    {
1099        // System.out.println(
1100
// Thread.currentThread().getName()
1101
// + " In processDisconnectEvent");
1102

1103        DisconnectMessage message = (DisconnectMessage)event.getMessage();
1104        long session_id = message.getSessionId();
1105        
1106        SessionInfo session = getSessionInfo(session_id);
1107        if (session == null)
1108        {
1109            return true;
1110        }
1111        
1112        // send the disconnect message to the client
1113
if (parent.sendRequestMessageToClient(0,
1114        "text/xml",
1115        message.format(),
1116        true) == false)
1117        {
1118            // print error message
1119
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
1120            Thread.currentThread().getName()
1121            + "- TalkPluginAppClient.processDisconnectEvent() -- Error sending disconnect message to the endpoint");
1122        }
1123        
1124        // remove the session
1125
removeFromCallList(session_id);
1126        
1127        if (registered == false)
1128        {
1129            // if it is not a transfer
1130
if (message.getCalledInfo() == null)
1131            {
1132                // close the connection
1133
return false;
1134            }
1135            // else, retain the connection
1136
}
1137        
1138        return true;
1139    }
1140    
1141    private boolean processActionEvent(ActionEvent event)
1142    {
1143        EndPointActionInterface[] actions = event.getActionList();
1144        
1145        for (int i = 0; i < actions.length; i++)
1146        {
1147            if ((actions[i] instanceof ChangeEndPointAction) == true)
1148            {
1149                ChangeEndPointAction change = (ChangeEndPointAction)actions[i];
1150                long session_id = change.getSessionId();
1151                
1152                SessionInfo session = getSessionInfo(session_id);
1153                if (session == null)
1154                {
1155                    return true; // ignore
1156
}
1157                
1158                int num_ep = session.numEndPoints();
1159                for (int j = 0; j < num_ep; j++)
1160                {
1161                    EndPointInterface ep = session.elementAt(j);
1162                    if (ep != parent)
1163                    {
1164                        session.replaceEndPoint(j, change.getEndPoint());
1165                    }
1166                }
1167            }
1168            else if ((actions[i] instanceof RemoveSessionAction) == true)
1169            {
1170                RemoveSessionAction remove = (RemoveSessionAction)actions[i];
1171                long session_id = remove.getSessionId();
1172                
1173                SessionInfo session = getSessionInfo(session_id);
1174                if (session == null)
1175                {
1176                    return true; // ignore
1177
}
1178                
1179                // remove the session
1180
removeFromCallList(session_id);
1181            }
1182            else if ((actions[i] instanceof ReplaceSessionAction) == true)
1183            {
1184                ReplaceSessionAction replace = (ReplaceSessionAction)actions[i];
1185                long old_session_id = replace.getOldSessionId();
1186                long new_session_id = replace.getNewSessionId();
1187                
1188                SessionInfo new_session_info = getSessionInfo(new_session_id);
1189                SessionInfo old_session_info = getSessionInfo(old_session_id);
1190                
1191                if (new_session_info == null) // if the new session does not exist
1192
{
1193                    if (old_session_info != null)
1194                    {
1195                        // the service controller is telling me to replace the old session id
1196
// with the new one
1197
old_session_info.setSessionId(new_session_id);
1198                        
1199                        changeSessionId(old_session_id, new_session_id);
1200                        
1201                        // send notification to the client
1202
ReplaceSessionMessage message = new ReplaceSessionMessage();
1203                        message.setNewSessionId(new_session_id);
1204                        message.setOldSessionId(old_session_id);
1205                        
1206                        String JavaDoc enc_key = null;
1207                        String JavaDoc key = replace.getNewKey();
1208                        if (key != null)
1209                        {
1210                            enc_key = key;
1211                        }
1212                        
1213                        message.setEncryptedKey(key);
1214                        if (parent.sendRequestMessageToClient(0,
1215                        "text/xml",
1216                        message.format(),
1217                        true) == false)
1218                        {
1219                            // print error message
1220
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
1221                            Thread.currentThread().getName()
1222                            + "- TalkPluginAppClient.processActionEvent() -- Error sending replace session message to the endpoint");
1223                        }
1224                        
1225                    }
1226                    // else, ignore
1227
}
1228                else // new session exists
1229
{
1230                    if (old_session_info != null)
1231                    {
1232                        // the service controller is telling me to get rid of the old session
1233
removeFromCallList(old_session_id);
1234                    }
1235                }
1236            }
1237            // else, ignore
1238
}
1239        return true;
1240    }
1241    
1242    private boolean processInformationEvent(InformationEvent event)
1243    {
1244        if (event.isRequest() == true)
1245        {
1246            EndPointInterface from = event.getFrom();
1247            event.setRequest(false);
1248            event.setInformation(selfInfo);
1249            event.setFrom(parent);
1250            if (from.sendEvent(event) == false)
1251            {
1252                // print error message
1253
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
1254                Thread.currentThread().getName()
1255                + "- TalkPluginAppClient.processInformationEvent() -- Error sending information event to the endpoint");
1256            }
1257        }
1258        // else, ignore
1259

1260        return true;
1261    }
1262    
1263    private void addToCallList(long session_id, SessionInfo session)
1264    {
1265        callList.put(new Long JavaDoc(session_id), session);
1266        
1267        if (registered == true)
1268        {
1269            EndPointList.Instance().setCallCount(parent, callList.size());
1270            
1271            if (EndPointList.Instance().isDnd(parent) == false)
1272            {
1273                ServiceController.Instance().groupNotifyOfCallCountChange(parent);
1274            }
1275        }
1276    }
1277    
1278    private void removeFromCallList(long session_id)
1279    {
1280        callList.remove(new Long JavaDoc(session_id));
1281        
1282        if (registered == true)
1283        {
1284            EndPointList.Instance().setCallCount(parent, callList.size());
1285            if (EndPointList.Instance().isDnd(parent) == false)
1286            {
1287                ServiceController.Instance().groupNotifyOfCallCountChange(parent);
1288            }
1289        }
1290    }
1291    
1292    private SessionInfo getSessionInfo(long session_id)
1293    {
1294        return (SessionInfo)callList.get(new Long JavaDoc(session_id));
1295    }
1296    
1297    private void changeSessionId(long from_session_id, long to_session_id)
1298    {
1299        SessionInfo session = getSessionInfo(from_session_id);
1300        if (session != null)
1301        {
1302            session.setSessionId(to_session_id);
1303            callList.remove(new Long JavaDoc(from_session_id));
1304            callList.put(new Long JavaDoc(to_session_id), session);
1305        }
1306    }
1307    
1308    private boolean processDropEndpointEvent(DropEndpointEvent event)
1309    {
1310        unregistrationComplete = true;
1311        return false;
1312    }
1313    
1314    private boolean registered = false;
1315    private String JavaDoc host;
1316    private HTTPEndPoint parent;
1317    private Hashtable callList = new Hashtable();
1318    private SessionInfo lastSession = null;
1319    private int registrationRequestId = -1;
1320    private String JavaDoc registeredUserName;
1321    private CallPartyElement selfInfo = null;
1322    private boolean unregistrationComplete = false;
1323}
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
Popular Tags