2 import org.opensrf.util.JSONWriter;
3 import org.opensrf.net.xmpp.*;
5 import java.util.HashMap;
7 public abstract class Session {
9 /** Represents the different connection states for a session */
10 public enum ConnectState {
16 /** local cache of existing sessions */
17 private static Map<String, Session>
18 sessionCache = new HashMap<String, Session>();
20 /** the current connection state */
21 private ConnectState connectState;
23 /** The (jabber) address of the remote party we are communicating with */
24 private String remoteNode;
27 * The thread is used to link messages to a given session.
28 * In other words, each session has a unique thread, and all messages
29 * in that session will carry this thread around as an indicator.
31 protected String thread;
34 connectState = ConnectState.DISCONNECTED;
38 * Sends a Message to our remoteNode.
40 public void send(Message omsg) throws XMPPException {
42 /** construct the XMPP message */
43 XMPPMessage xmsg = new XMPPMessage();
44 xmsg.setTo(remoteNode);
45 xmsg.setThread(thread);
46 xmsg.setBody(new JSONWriter(omsg).write());
47 XMPPSession ses = XMPPSession.getGlobalSession();
50 XMPPSession.getGlobalSession().send(xmsg);
51 } catch(XMPPException e) {
52 /* XXX log.. what else? */
53 connectState = ConnectState.DISCONNECTED;
58 * Waits for a message to arrive over the network and passes
59 * all received messages to the stack for processing
60 * @param millis The number of milliseconds to wait for a message to arrive
62 public static void waitForMessage(long millis) {
64 Stack.processXMPPMessage(
65 XMPPSession.getGlobalSession().recv(millis));
66 } catch(XMPPException e) {
67 /* XXX log.. what else? */
72 * Removes this session from the session cache.
74 public void cleanup() {
75 sessionCache.remove(thread);
79 * Searches for the cached session with the given thread.
80 * @param thread The session thread.
81 * @return The found session or null.
83 public static Session findCachedSession(String thread) {
84 return sessionCache.get(thread);
87 protected void cacheSession() {
88 sessionCache.put(thread, this);
91 public void setRemoteNode(String nodeName) {
92 remoteNode = nodeName;
94 public String getRemoteNode() {