KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > feature > proactive > server > ProactivePluginAppClient


1 package com.quikj.application.web.talk.feature.proactive.server;
2
3 import java.util.*;
4 import java.io.*;
5 import com.quikj.server.web.*;
6 import com.quikj.server.framework.*;
7
8 public class ProactivePluginAppClient implements PluginAppClientInterface
9 {
10     private static final long NOACTIVITY_TIMER_DURATION = 5 * 60 * 1000L;
11     
12     private String JavaDoc host;
13     private HTTPEndPoint parent;
14     
15     private String JavaDoc identifier = null;
16     private String JavaDoc group = null;
17     private String JavaDoc document = null;
18     private int timerId = -1;
19     
20     public ProactivePluginAppClient()
21     {
22     }
23     
24     public boolean newConnection(String JavaDoc host, HTTPEndPoint parent)
25     {
26         this.host = host;
27         this.parent = parent;
28         
29         try
30         {
31             timerId = AceTimer.Instance().startTimer(NOACTIVITY_TIMER_DURATION, 0L);
32             if (timerId == -1)
33             {
34                 // print error message
35
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
36                 parent.getIdentifier()
37                 + "- ProactivePluginAppClient.newConnection() -- Failed to start Ace Timer");
38             }
39         }
40         catch (IOException ex)
41         {
42             // should not happen
43
}
44         return true;
45     }
46     
47     public boolean requestReceived(int request_id, String JavaDoc content_type, String JavaDoc body)
48     {
49         if (body != null)
50         {
51             parseMessage(body);
52             
53             UnregisteredInfo info = null;
54             if (identifier == null) // first time to the web site
55
{
56                 if (group == null)
57                 {
58                     AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
59                     parent.getIdentifier()
60                     + "- ProactivePluginClient.requestReceived() -- Client sent a message without a group identifier");
61                     return false; // will close the connection
62
}
63                 
64                 info = ProactiveUserData.getInstance().createIdentifier(host, group);
65                 identifier = info.getIdent().getUniqueIdentifier();
66                 // and send it back
67
if (parent.sendRequestMessageToClient(-1, "text/plain",
68                 "Identifier:" + identifier + "\n",
69                 true) == false)
70                 {
71                     // print error message
72
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
73                     parent.getIdentifier()
74                     + "- ProactivePluginClient.requestReceived() -- Error sending message to the client");
75                 }
76             }
77             else // has previously visited the site
78
{
79                 UnregisteredIdentifier ui = new UnregisteredIdentifier(group, identifier);
80                 info = ProactiveUserData.getInstance().get(ui);
81                 if (info == null) // removed by the cleanup process because of session timeout
82
{
83                     info = ProactiveUserData.getInstance().createIdentifier(host, group);
84                     
85                     // and send it back
86
if (parent.sendRequestMessageToClient(-1, "text/plain",
87                     "Identifier:" + info.getIdent().getUniqueIdentifier() + "\n",
88                     true) == false)
89                     {
90                         // print error message
91
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
92                         parent.getIdentifier()
93                         + "- ProactivePluginClient.requestReceived() -- Error sending message to the client");
94                     }
95                 }
96             }
97             
98             ProactiveUserData.getInstance().setNewSessionData(parent, document, info);
99         }
100         
101         return true;
102     }
103     
104     public boolean responseReceived(int request_id, int status, String JavaDoc reason,
105     String JavaDoc content_type, String JavaDoc body)
106     {
107         return true;
108     }
109     
110     public boolean eventReceived(AceMessageInterface event)
111     {
112         if ((event instanceof AceTimerMessage) == true)
113         {
114             timerId = -1;
115             
116             UnregisteredIdentifier ident = new UnregisteredIdentifier(group, identifier);
117             UnregisteredInfo info = ProactiveUserData.getInstance().get(ident);
118             if (info != null)
119             {
120                 ProactiveUserData.getInstance().removeEndpoint(info);
121             }
122             
123             if (parent.closeClientConnection() == false)
124             {
125                 // print error message
126
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
127                 parent.getIdentifier()
128                 + "- ProactivePluginAppClient.eventReceived() -- Could not close connection on inactivity");
129             }
130             
131         }
132         else if ((event instanceof ConversationInitiationMessage) == true)
133         {
134             ConversationInitiationMessage cim = (ConversationInitiationMessage)event;
135             boolean ok = ProactiveUserData.getInstance().lockSession(cim.getGroup(),
136             cim.getSessionId());
137             if (ok == false)
138             {
139                 AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
140                 parent.getIdentifier()
141                 + "- ProactivePluginAppClient.eventReceived() -- Could not lock session: "
142                 + cim.getGroup() + "-" + cim.getSessionId());
143                 return true;
144             }
145             
146             // send a message to the client
147
if (parent.sendRequestMessageToClient(-1, "text/plain",
148             "Initiate-Call:" + cim.getOperator() + "\n",
149             true) == false)
150             {
151                 // print error message
152
AceLogger.Instance().log(AceLogger.ERROR, AceLogger.SYSTEM_LOG,
153                 parent.getIdentifier()
154                 + "- ProactivePluginAppClient.eventReceived() -- Error sending message to the client");
155             }
156         }
157         else
158         {
159             // print error message
160
AceLogger.Instance().log(AceLogger.WARNING, AceLogger.SYSTEM_LOG,
161             parent.getIdentifier()
162             + "- ProactivePluginAppClient.eventReceived() -- An unexpected event is received: "
163             + event.messageType());
164         }
165         return true;
166     }
167     
168     public void connectionClosed()
169     {
170         if (identifier != null)
171         {
172             UnregisteredIdentifier ident = new UnregisteredIdentifier(group, identifier);
173             UnregisteredInfo info = ProactiveUserData.getInstance().get(ident);
174             if (info != null)
175             {
176                 ProactiveUserData.getInstance().removeEndpoint(info);
177             }
178         }
179         
180         // cancel the timer, if needed
181
if (timerId != -1)
182         {
183             try
184             {
185                 AceTimer.Instance().cancelTimer(timerId);
186                 timerId = -1;
187             }
188             catch (IOException ex)
189             {
190                 // should not happen
191
}
192         }
193     }
194     
195     
196     
197     private void parseMessage(String JavaDoc message)
198     {
199         StringTokenizer tokens = new StringTokenizer(message, "\n");
200         int num_lines = tokens.countTokens();
201         
202         for (int i = 0; i < num_lines; i++)
203         {
204             String JavaDoc line = tokens.nextToken().trim();
205             if (line.length() == 0)
206             {
207                 continue;
208             }
209             
210             StringTokenizer t = new StringTokenizer(line, ":");
211             int n = t.countTokens();
212             if (n < 2)
213             {
214                 continue;
215             }
216             
217             String JavaDoc key = t.nextToken().trim();
218             String JavaDoc value = "";
219             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
220             for (int j = 1; j < n; j++)
221             {
222                 buffer.append(t.nextToken());
223             }
224             value = buffer.toString();
225             
226             if ((key.length() == 0) || (value.length() == 0))
227             {
228                 continue;
229             }
230             
231             if (key.equals("Identifier") == true)
232             {
233                 if (value.equals("NA") == false)
234                 {
235                     identifier = value;
236                 }
237             }
238             else if (key.equals("Group") == true)
239             {
240                 group = value;
241             }
242             else if (key.equals("Document") == true)
243             {
244                 document = value;
245             }
246         }
247     }
248 }
249
250
251
252
Popular Tags