]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Session.java
added multi-threaded client support to the opensrf network/xmpp layer
[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 import java.util.Arrays;
7
8 public abstract class Session {
9
10     /** Represents the different connection states for a session */
11     public enum ConnectState {
12         DISCONNECTED,
13         CONNECTING,
14         CONNECTED
15     };
16
17     /** local cache of existing sessions */
18     private static Map<String, Session> 
19         sessionCache = new HashMap<String, Session>();
20
21     /** the current connection state */
22     private ConnectState connectState;
23
24     /** The address of the remote party we are communicating with */
25     private String remoteNode;
26
27     /** 
28      * The thread is used to link messages to a given session. 
29      * In other words, each session has a unique thread, and all messages 
30      * in that session will carry this thread around as an indicator.
31      */
32     private String thread;
33
34     public Session() {
35         connectState = ConnectState.DISCONNECTED;
36     }
37     
38     /**
39      * Sends a Message to our remoteNode.
40      */
41     public void send(Message omsg) throws SessionException {
42
43         /** construct the XMPP message */
44         XMPPMessage xmsg = new XMPPMessage();
45         xmsg.setTo(remoteNode);
46         xmsg.setThread(thread);
47         xmsg.setBody(new JSONWriter(Arrays.asList(new Message[] {omsg})).write());
48
49         try {
50             XMPPSession.getThreadSession().send(xmsg);
51         } catch(XMPPException e) {
52             connectState = ConnectState.DISCONNECTED;
53             throw new SessionException("Error sending message to " + remoteNode, e);
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) throws SessionException, MethodException {
63         try {
64             Stack.processXMPPMessage(
65                 XMPPSession.getThreadSession().recv(millis));
66         } catch(XMPPException e) {
67             throw new SessionException("Error waiting for message", e);
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     /**
88      * Puts this session into session cache.
89      */
90     protected void cacheSession() {
91         sessionCache.put(thread, this);
92     }
93
94     /**
95      * Sets the remote address
96      * @param nodeName The name of the remote node.
97      */
98     public void setRemoteNode(String nodeName) {
99         remoteNode = nodeName;
100     }
101     /**
102      * @return The remote node
103      */
104     public String getRemoteNode() {
105         return remoteNode;
106     }
107
108
109     /**
110      * Get thread.
111      * @return thread as String.
112      */
113     public String getThread() {
114         return thread;
115     }
116     
117     /**
118      * Set thread.
119      * @param thread the value to set.
120      */
121     public void setThread(String thread) {
122         this.thread = thread;
123     }
124     
125     /**
126      * Get connectState.
127      * @return connectState as ConnectState.
128      */
129     public ConnectState getConnectState() {
130         return connectState;
131     }
132     
133     /**
134      * Set connectState.
135      * @param connectState the value to set.
136      */
137     public void setConnectState(ConnectState connectState) {
138         this.connectState = connectState;
139     }
140 }