01: package org.getahead.dwrdemo.arac;
02:
03: import java.util.Collection;
04: import java.util.Date;
05: import java.util.Map;
06: import java.util.TreeMap;
07:
08: import org.apache.commons.logging.LogFactory;
09: import org.apache.commons.logging.Log;
10: import org.directwebremoting.ScriptSession;
11: import org.directwebremoting.WebContext;
12: import org.directwebremoting.WebContextFactory;
13: import org.directwebremoting.proxy.ScriptProxy;
14:
15: /**
16: * @author Joe Walker [joe at getahead dot ltd dot uk]
17: */
18: public class Control {
19: /**
20: * @param name The new message text to add
21: */
22: public void pingFromClient(String name) {
23: WebContext wctx = WebContextFactory.get();
24: ScriptSession session = wctx.getScriptSession();
25: String id = session.getId();
26:
27: Client client = clientsBySessionId.get(id);
28: if (client == null) {
29: client = new Client(name, session);
30: clientsBySessionId.put(id, client);
31: clientsByClientId.put(new Integer(client.getId()), client);
32: }
33:
34: client.setName(name);
35: client.setLastPinged(new Date());
36:
37: serverRefresh();
38: }
39:
40: /**
41: * @param id
42: * @param message
43: */
44: public void sendToClient(int id, String message) {
45: Client client = clientsByClientId.get(new Integer(id));
46: client.setLastMessaged(new Date());
47:
48: ScriptSession session = client.getSession();
49: ScriptProxy proxy = new ScriptProxy(session);
50: proxy.addFunctionCall("addMessage", message);
51:
52: serverRefresh();
53: }
54:
55: /**
56: *
57: */
58: public void serverRefresh() {
59: WebContext wctx = WebContextFactory.get();
60: String contextPath = wctx.getHttpServletRequest()
61: .getContextPath();
62:
63: // For all the browsers on the current page:
64: Collection<ScriptSession> sessions = wctx
65: .getScriptSessionsByPage(contextPath
66: + "/arac/server.html");
67: log.debug("Sending refresh to " + sessions.size()
68: + " servers about " + clientsByClientId.size()
69: + " clients.");
70:
71: ScriptProxy proxy = new ScriptProxy(sessions);
72: proxy.addFunctionCall("buildClientTable", clientsByClientId);
73: }
74:
75: /**
76: * The current set of messages
77: */
78: private Map<String, Client> clientsBySessionId = new TreeMap<String, Client>();
79:
80: private Map<Integer, Client> clientsByClientId = new TreeMap<Integer, Client>();
81:
82: /**
83: * The log stream
84: */
85: protected static final Log log = LogFactory.getLog(Control.class);
86: }
|