1 package org.opensrf.net.xmpp;
4 import java.net.Socket;
8 * Represents a single XMPP session. Sessions are responsible for writing to
9 * the stream and for managing a stream reader.
11 public class XMPPSession {
13 /** Initial jabber message */
14 public static final String JABBER_CONNECT =
15 "<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>";
17 /** Basic auth message */
18 public static final String JABBER_BASIC_AUTH =
19 "<iq id='123' type='set'><query xmlns='jabber:iq:auth'>" +
20 "<username>%s</username><password>%s</password><resource>%s</resource></query></iq>";
26 /** jabber username */
27 private String username;
28 /** jabber password */
29 private String password;
30 /** jabber resource */
31 private String resource;
33 /** XMPP stream reader */
35 /** Fprint-capable socket writer */
37 /** Raw socket output stream */
38 OutputStream outStream;
40 /** The process-wide session. All communication occurs
41 * accross this single connection */
42 private static XMPPSession globalSession;
46 * Creates a new session.
47 * @param host The jabber domain
48 * @param port The jabber port
50 public XMPPSession( String host, int port ) {
56 * Returns the global, process-wide session
58 public static XMPPSession getGlobalSession() {
63 * Sets the global, process-wide section
65 public static void setGlobalSession(XMPPSession ses) {
70 /** true if this session is connected to the server */
71 public boolean connected() {
74 reader.getXMPPStreamState() ==
75 XMPPReader.XMPPStreamState.CONNECTED);
80 * Connects to the network.
81 * @param username The jabber username
82 * @param password The jabber password
83 * @param resource The Jabber resource
85 public void connect(String username, String password, String resource) throws XMPPException {
87 this.username = username;
88 this.password = password;
89 this.resource = resource;
94 /* open the socket and associated streams */
95 socket = new Socket(host, port);
97 /** the session maintains control over the output stream */
98 outStream = socket.getOutputStream();
99 writer = new PrintWriter(outStream, true);
101 /** pass the input stream to the reader */
102 reader = new XMPPReader(socket.getInputStream());
104 } catch(IOException ioe) {
106 XMPPException("unable to communicate with host " + host + " on port " + port);
109 /* build the reader thread */
110 Thread thread = new Thread(reader);
111 thread.setDaemon(true);
114 /* send the initial jabber message */
116 reader.waitCoreEvent(10000);
117 if( reader.getXMPPStreamState() != XMPPReader.XMPPStreamState.CONNECT_RECV )
118 throw new XMPPException("unable to connect to jabber server");
120 /* send the basic auth message */
121 sendBasicAuth(); /* XXX add support for other auth mechanisms */
122 reader.waitCoreEvent(10000);
124 throw new XMPPException("Authentication failed");
127 /** Sends the initial jabber message */
128 private void sendConnect() {
129 writer.printf(JABBER_CONNECT, host);
130 reader.setXMPPStreamState(XMPPReader.XMPPStreamState.CONNECT_SENT);
133 /** Send the basic auth message */
134 private void sendBasicAuth() {
135 writer.printf(JABBER_BASIC_AUTH, username, password, resource);
136 reader.setXMPPStreamState(XMPPReader.XMPPStreamState.AUTH_SENT);
141 * Sends an XMPPMessage.
142 * @param msg The message to send.
144 public synchronized void send(XMPPMessage msg) throws XMPPException {
147 String xml = msg.toXML();
148 outStream.write(xml.getBytes());
149 } catch (Exception e) {
150 throw new XMPPException(e.toString());
156 * @throws XMPPException if we are no longer connected.
158 private void checkConnected() throws XMPPException {
160 throw new XMPPException("Disconnected stream");
165 * Receives messages from the network.
166 * @param timeout Maximum number of milliseconds to wait for a message to arrive.
167 * If timeout is negative, this method will wait indefinitely.
168 * If timeout is 0, this method will not block at all, but will return a
169 * message if there is already a message available.
171 public XMPPMessage recv(long timeout) throws XMPPException {
177 while(true) { /* wait indefinitely for a message to arrive */
178 reader.waitCoreEvent(timeout);
179 msg = reader.popMessageQueue();
180 if( msg != null ) return msg;
186 while(timeout >= 0) { /* wait at most 'timeout' milleseconds for a message to arrive */
187 timeout -= reader.waitCoreEvent(timeout);
188 msg = reader.popMessageQueue();
189 if( msg != null ) return msg;