]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/net/xmpp/XMPPSession.java
implemented enough of the stack/session logic to make stateless requests and receive...
[OpenSRF.git] / src / java / org / opensrf / net / xmpp / XMPPSession.java
1 package org.opensrf.net.xmpp;
2
3 import java.io.*;
4 import java.net.Socket;
5
6
7 /**
8  * Represents a single XMPP session.  Sessions are responsible for writing to
9  * the stream and for managing a stream reader.
10  */
11 public class XMPPSession {
12
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'>";
16
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>";
21
22     /** jabber domain */
23     private String host;
24     /** jabber port */
25     private int port;
26     /** jabber username */
27     private String username;
28     /** jabber password */
29     private String password;
30     /** jabber resource */
31     private String resource;
32
33     /** XMPP stream reader */
34     XMPPReader reader;
35     /** Fprint-capable socket writer */
36     PrintWriter writer;
37     /** Raw socket output stream */
38     OutputStream outStream;
39
40     /** The process-wide session.  All communication occurs
41      * accross this single connection */
42     private static XMPPSession globalSession;
43
44
45     /**
46      * Creates a new session.
47      * @param host The jabber domain
48      * @param port The jabber port
49      */
50     public XMPPSession( String host, int port ) {
51         this.host = host;
52         this.port = port;
53     }
54
55     public static XMPPSession getGlobalSession() {
56         return globalSession;
57     }
58
59     public static void setGlobalSession(XMPPSession ses) {
60         globalSession = ses;
61     }
62
63
64     /** true if this session is connected to the server */
65     public boolean connected() {
66         return (
67             reader != null && 
68             reader.getXMPPStreamState() == 
69                 XMPPReader.XMPPStreamState.CONNECTED);
70     }
71
72
73     /**
74      * Connects to the network.
75      * @param username The jabber username
76      * @param password The jabber password
77      * @param resource The Jabber resource
78      */
79     public void connect(String username, String password, String resource) throws XMPPException {
80
81         this.username = username;
82         this.password = password;
83         this.resource = resource;
84
85         Socket socket;
86
87         try { 
88             /* open the socket and associated streams */
89             socket = new Socket(host, port);
90
91             /** the session maintains control over the output stream */
92             outStream = socket.getOutputStream();
93             writer = new PrintWriter(outStream, true);
94
95             /** pass the input stream to the reader */
96             reader = new XMPPReader(socket.getInputStream());
97
98         } catch(IOException ioe) {
99             throw new 
100                 XMPPException("unable to communicate with host " + host + " on port " + port);
101         }
102
103         /* build the reader thread */
104         Thread thread = new Thread(reader);
105         thread.setDaemon(true);
106         thread.start();
107
108         /* send the initial jabber message */
109         sendConnect();
110         reader.waitCoreEvent(10000);
111         if( reader.getXMPPStreamState() != XMPPReader.XMPPStreamState.CONNECT_RECV ) 
112             throw new XMPPException("unable to connect to jabber server");
113
114         /* send the basic auth message */
115         sendBasicAuth(); /* XXX add support for other auth mechanisms */
116         reader.waitCoreEvent(10000);
117         if(!connected())
118             throw new XMPPException("Authentication failed");
119     }
120
121     /** Sends the initial jabber message */
122     private void sendConnect() {
123         writer.printf(JABBER_CONNECT, host);
124         reader.setXMPPStreamState(XMPPReader.XMPPStreamState.CONNECT_SENT);
125     }
126
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);
131     }
132
133
134     /**
135      * Sends an XMPPMessage.
136      * @param msg The message to send.
137      */
138     public synchronized void send(XMPPMessage msg) throws XMPPException {
139         checkConnected();
140         try {
141             String xml = msg.toXML();
142             outStream.write(xml.getBytes()); 
143         } catch (Exception e) {
144             throw new XMPPException(e.toString());
145         }
146     }
147
148
149     /**
150      * @throws XMPPException if we are no longer connected.
151      */
152     private void checkConnected() throws XMPPException {
153         if(!connected())
154             throw new XMPPException("Disconnected stream");
155     }
156
157
158     /**
159      * Receives messages from the network.  
160      * @param timeout Maximum number of milliseconds to wait for a message to arrive.
161      * If timeout is negative, this method will wait indefinitely.
162      * If timeout is 0, this method will not block at all, but will return a 
163      * message if there is already a message available.
164      */
165     public XMPPMessage recv(long timeout) throws XMPPException {
166
167         XMPPMessage msg;
168
169         if(timeout < 0) {
170
171             while(true) { /* wait indefinitely for a message to arrive */
172                 reader.waitCoreEvent(timeout);
173                 msg = reader.popMessageQueue();
174                 if( msg != null ) return msg;
175                 checkConnected();
176             }
177
178         } else {
179
180             while(timeout >= 0) { /* wait at most 'timeout' milleseconds for a message to arrive */
181                 timeout -= reader.waitCoreEvent(timeout);
182                 msg = reader.popMessageQueue();
183                 if( msg != null ) return msg;
184                 checkConnected();
185             }
186         }
187
188         return null;
189     }
190 }
191