]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Session.java
added a lot of documentation
[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         XMPPSession ses = XMPPSession.getGlobalSession();
49
50         try {
51             XMPPSession.getGlobalSession().send(xmsg);
52         } catch(XMPPException e) {
53             connectState = ConnectState.DISCONNECTED;
54             throw new SessionException("Error sending message to " + remoteNode, e);
55         }
56     }
57
58     /**
59      * Waits for a message to arrive over the network and passes
60      * all received messages to the stack for processing
61      * @param millis The number of milliseconds to wait for a message to arrive
62      */
63     public static void waitForMessage(long millis) throws SessionException {
64         try {
65             Stack.processXMPPMessage(
66                 XMPPSession.getGlobalSession().recv(millis));
67         } catch(XMPPException e) {
68             throw new SessionException("Error waiting for message", e);
69         }
70     }
71
72     /**
73      * Removes this session from the session cache.
74      */
75     public void cleanup() {
76         sessionCache.remove(thread);
77     }
78
79     /**
80      * Searches for the cached session with the given thread.
81      * @param thread The session thread.
82      * @return The found session or null.
83      */
84     public static Session findCachedSession(String thread) {
85         return sessionCache.get(thread);
86     }
87
88     /**
89      * Puts this session into session cache.
90      */
91     protected void cacheSession() {
92         sessionCache.put(thread, this);
93     }
94
95     /**
96      * Sets the remote address
97      * @param nodeName The name of the remote node.
98      */
99     public void setRemoteNode(String nodeName) {
100         remoteNode = nodeName;
101     }
102     /**
103      * @return The remote node
104      */
105     public String getRemoteNode() {
106         return remoteNode;
107     }
108
109
110     /**
111      * Get thread.
112      * @return thread as String.
113      */
114     public String getThread() {
115         return thread;
116     }
117     
118     /**
119      * Set thread.
120      * @param thread the value to set.
121      */
122     public void setThread(String thread) {
123         this.thread = thread;
124     }
125     
126     /**
127      * Get connectState.
128      * @return connectState as ConnectState.
129      */
130     public ConnectState getConnectState() {
131         return connectState;
132     }
133     
134     /**
135      * Set connectState.
136      * @param connectState the value to set.
137      */
138     public void setConnectState(ConnectState connectState) {
139         this.connectState = connectState;
140     }
141 }