]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Session.java
2eb9aa9853c430fa612e46fa6910fe6af02fd9b0
[OpenSRF.git] / src / java / org / opensrf / Session.java
1 package org.opensrf;
2 import org.opensrf.util.JSONWriter;
3 import org.opensrf.net.xmpp.*;
4 import java.util.Map;
5 import java.util.HashMap;
6
7 public abstract class Session {
8
9     /** Represents the different connection states for a session */
10     public enum ConnectState {
11         DISCONNECTED,
12         CONNECTING,
13         CONNECTED
14     };
15
16     /** local cache of existing sessions */
17     private static Map<String, Session> 
18         sessionCache = new HashMap<String, Session>();
19
20     /** the current connection state */
21     private ConnectState connectState;
22
23     /** The (jabber) address of the remote party we are communicating with */
24     private String remoteNode;
25
26     /** 
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.
30      */
31     protected String thread;
32
33     public Session() {
34         connectState = ConnectState.DISCONNECTED;
35     }
36     
37     /**
38      * Sends a Message to our remoteNode.
39      */
40     public void send(Message omsg) throws XMPPException {
41
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();
48
49         try {
50             XMPPSession.getGlobalSession().send(xmsg);
51         } catch(XMPPException e) {
52             /* XXX log.. what else? */
53             connectState = ConnectState.DISCONNECTED;
54         }
55     }
56
57     /**
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
61      */
62     public static void waitForMessage(long millis) {
63         try {
64             Stack.processXMPPMessage(
65                 XMPPSession.getGlobalSession().recv(millis));
66         } catch(XMPPException e) {
67             /* XXX log.. what else? */
68         }
69     }
70
71     /**
72      * Removes this session from the session cache.
73      */
74     public void cleanup() {
75         sessionCache.remove(thread);
76     }
77
78     /**
79      * Searches for the cached session with the given thread.
80      * @param thread The session thread.
81      * @return The found session or null.
82      */
83     public static Session findCachedSession(String thread) {
84         return sessionCache.get(thread);
85     }
86
87     protected void cacheSession() {
88         sessionCache.put(thread, this);
89     }
90
91     public void setRemoteNode(String nodeName) {
92         remoteNode = nodeName;
93     }
94     public String getRemoteNode() {
95         return remoteNode;
96     }
97 }