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 ) {
55 public static XMPPSession getGlobalSession() {
59 public static void setGlobalSession(XMPPSession ses) {
64 /** true if this session is connected to the server */
65 public boolean connected() {
68 reader.getXMPPStreamState() ==
69 XMPPReader.XMPPStreamState.CONNECTED);
74 * Connects to the network.
75 * @param username The jabber username
76 * @param password The jabber password
77 * @param resource The Jabber resource
79 public void connect(String username, String password, String resource) throws XMPPException {
81 this.username = username;
82 this.password = password;
83 this.resource = resource;
88 /* open the socket and associated streams */
89 socket = new Socket(host, port);
91 /** the session maintains control over the output stream */
92 outStream = socket.getOutputStream();
93 writer = new PrintWriter(outStream, true);
95 /** pass the input stream to the reader */
96 reader = new XMPPReader(socket.getInputStream());
98 } catch(IOException ioe) {
100 XMPPException("unable to communicate with host " + host + " on port " + port);
103 /* build the reader thread */
104 Thread thread = new Thread(reader);
105 thread.setDaemon(true);
108 /* send the initial jabber message */
110 reader.waitCoreEvent(10000);
111 if( reader.getXMPPStreamState() != XMPPReader.XMPPStreamState.CONNECT_RECV )
112 throw new XMPPException("unable to connect to jabber server");
114 /* send the basic auth message */
115 sendBasicAuth(); /* XXX add support for other auth mechanisms */
116 reader.waitCoreEvent(10000);
118 throw new XMPPException("Authentication failed");
121 /** Sends the initial jabber message */
122 private void sendConnect() {
123 writer.printf(JABBER_CONNECT, host);
124 reader.setXMPPStreamState(XMPPReader.XMPPStreamState.CONNECT_SENT);
127 /** Send the basic auth message */
128 private void sendBasicAuth() {
129 writer.printf(JABBER_BASIC_AUTH, username, password, resource);
130 reader.setXMPPStreamState(XMPPReader.XMPPStreamState.AUTH_SENT);
135 * Sends an XMPPMessage.
136 * @param msg The message to send.
138 public synchronized void send(XMPPMessage msg) throws XMPPException {
141 outStream.write(msg.toXML().getBytes());
142 } catch (Exception e) {
143 throw new XMPPException(e.toString());
149 * @throws XMPPException if we are no longer connected.
151 private void checkConnected() throws XMPPException {
153 throw new XMPPException("Disconnected stream");
158 * Receives messages from the network.
159 * @param timeout Maximum number of milliseconds to wait for a message to arrive.
160 * If timeout is negative, this method will wait indefinitely.
161 * If timeout is 0, this method will not block at all, but will return a
162 * message if there is already a message available.
164 public XMPPMessage recv(long timeout) throws XMPPException {
170 while(true) { /* wait indefinitely for a message to arrive */
171 reader.waitCoreEvent(timeout);
172 msg = reader.popMessageQueue();
173 if( msg != null ) return msg;
179 while(timeout >= 0) { /* wait at most 'timeout' milleseconds for a message to arrive */
180 timeout -= reader.waitCoreEvent(timeout);
181 msg = reader.popMessageQueue();
182 if( msg != null ) return msg;