]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/transport_session.h
Prepare for #inclusion in C++
[OpenSRF.git] / include / opensrf / transport_session.h
1 // ---------------------------------------------------------------------------------
2 // Manages the Jabber session.  Data is taken from the TCP object and pushed into
3 // a SAX push parser as it arrives.  When key Jabber documetn elements are met, 
4 // logic ensues.
5 // ---------------------------------------------------------------------------------
6 #ifndef TRANSPORT_SESSION_H
7 #define TRANSPORT_SESSION_H
8
9 #include <opensrf/transport_message.h>
10
11 #include <opensrf/utils.h>
12 #include <opensrf/log.h>
13 #include <opensrf/socket_bundle.h>
14
15 #include "sha.h"
16
17 #include <string.h>
18 #include <libxml/globals.h>
19 #include <libxml/xmlerror.h>
20 #include <libxml/parser.h>
21 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
22 #include <libxml/tree.h>
23 #include <libxml/debugXML.h>
24 #include <libxml/xmlmemory.h>
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define CONNECTING_1 1 /* just starting the connection to Jabber */
31 #define CONNECTING_2 2 /* First <stream> packet sent and <stream> packet received from server */
32
33 /* Note. these are growing buffers, so all that's necessary is a sane starting point */
34 #define JABBER_BODY_BUFSIZE             4096
35 #define JABBER_SUBJECT_BUFSIZE  64      
36 #define JABBER_THREAD_BUFSIZE           64      
37 #define JABBER_JID_BUFSIZE                      64      
38 #define JABBER_STATUS_BUFSIZE           16 
39
40 // ---------------------------------------------------------------------------------
41 // Callback for handling the startElement event.  Much of the jabber logic occurs
42 // in this and the characterHandler callbacks.
43 // Here we check for the various top level jabber elements: body, iq, etc.
44 // ---------------------------------------------------------------------------------
45 void startElementHandler( 
46                 void *session, const xmlChar *name, const xmlChar **atts);
47
48 // ---------------------------------------------------------------------------------
49 // Callback for handling the endElement event.  Updates the Jabber state machine
50 // to let us know the element is over.
51 // ---------------------------------------------------------------------------------
52 void endElementHandler( void *session, const xmlChar *name);
53
54 // ---------------------------------------------------------------------------------
55 // This is where we extract XML text content.  In particular, this is useful for
56 // extracting Jabber message bodies.
57 // ---------------------------------------------------------------------------------
58 void characterHandler(
59                 void *session, const xmlChar *ch, int len);
60
61 void  parseWarningHandler( void *session, const char* msg, ... );
62 void  parseErrorHandler( void *session, const char* msg, ... );
63
64 // ---------------------------------------------------------------------------------
65 // Tells the SAX parser which functions will be used as event callbacks
66 // ---------------------------------------------------------------------------------
67 static xmlSAXHandler SAXHandlerStruct = {
68    NULL,                                                        /* internalSubset */
69    NULL,                                                        /* isStandalone */
70    NULL,                                                        /* hasInternalSubset */
71    NULL,                                                        /* hasExternalSubset */
72    NULL,                                                        /* resolveEntity */
73    NULL,                                                        /* getEntity */
74    NULL,                                                        /* entityDecl */
75    NULL,                                                        /* notationDecl */
76    NULL,                                                        /* attributeDecl */
77    NULL,                                                        /* elementDecl */
78    NULL,                                                        /* unparsedEntityDecl */
79    NULL,                                                        /* setDocumentLocator */
80    NULL,                                                        /* startDocument */
81    NULL,                                                        /* endDocument */
82         startElementHandler,            /* startElement */
83         endElementHandler,              /* endElement */
84    NULL,                                                        /* reference */
85         characterHandler,                       /* characters */
86    NULL,                                                        /* ignorableWhitespace */
87    NULL,                                                        /* processingInstruction */
88    NULL,                                                        /* comment */
89    parseWarningHandler,         /* xmlParserWarning */
90    parseErrorHandler,           /* xmlParserError */
91    NULL,                                                        /* xmlParserFatalError : unused */
92    NULL,                                                        /* getParameterEntity */
93    NULL,                                                        /* cdataBlock; */
94    NULL,                                                        /* externalSubset; */
95    1,
96    NULL,
97    NULL,                                                        /* startElementNs */
98    NULL,                                                        /* endElementNs */
99         NULL                                                    /* xmlStructuredErrorFunc */
100 };
101
102 // ---------------------------------------------------------------------------------
103 // Our SAX handler pointer.
104 // ---------------------------------------------------------------------------------
105 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
106
107 // ---------------------------------------------------------------------------------
108 // Jabber state machine.  This is how we know where we are in the Jabber
109 // conversation.
110 // ---------------------------------------------------------------------------------
111 struct jabber_state_machine_struct {
112         int connected;
113         int connecting;
114         int in_message;
115         int in_message_body;
116         int in_thread;
117         int in_subject;
118         int in_error;
119         int in_message_error;
120         int in_iq;
121         int in_presence;
122         int in_status;
123 };
124 typedef struct jabber_state_machine_struct jabber_machine;
125
126
127 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
128
129 // ---------------------------------------------------------------------------------
130 // Transport session.  This maintains all the various parts of a session
131 // ---------------------------------------------------------------------------------
132 struct transport_session_struct {
133
134         /* our socket connection */
135         socket_manager* sock_mgr;
136
137         /* our Jabber state machine */
138         jabber_machine* state_machine;
139         /* our SAX push parser context */
140         xmlParserCtxtPtr parser_ctxt;
141
142         /* our text buffers for holding text data */
143         growing_buffer* body_buffer;
144         growing_buffer* subject_buffer;
145         growing_buffer* thread_buffer;
146         growing_buffer* from_buffer;
147         growing_buffer* recipient_buffer;
148         growing_buffer* status_buffer;
149         growing_buffer* message_error_type;
150         growing_buffer* session_id;
151         int message_error_code;
152
153         /* for OILS extenstions */
154         growing_buffer* router_to_buffer;
155         growing_buffer* router_from_buffer;
156         growing_buffer* router_class_buffer;
157         growing_buffer* router_command_buffer;
158         growing_buffer* osrf_xid_buffer;
159         int router_broadcast;
160
161         /* this can be anything.  It will show up in the 
162                 callbacks for your convenience. Otherwise, it's
163                 left untouched.  */
164         void* user_data;
165
166         char* server;
167         char* unix_path;
168         int     port;
169         int sock_id;
170
171         int component; /* true if we're a component */
172
173         /* the Jabber message callback */
174         void (*message_callback) ( void* user_data, transport_message* msg );
175         //void (iq_callback) ( void* user_data, transport_iq_message* iq );
176 };
177 typedef struct transport_session_struct transport_session;
178
179
180 // ------------------------------------------------------------------
181 // Allocates and initializes the necessary transport session
182 // data structures.
183 // If port > 0, then this session uses  TCP connection.  Otherwise,
184 // if unix_path != NULL, it uses a UNIX domain socket.
185 // ------------------------------------------------------------------
186 transport_session* init_transport( const char* server, int port, 
187         const char* unix_path, void* user_data, int component );
188
189 // ------------------------------------------------------------------
190 // Waits  at most 'timeout' seconds  for data to arrive from the 
191 // TCP handler. A timeout of -1 means to wait indefinitely.
192 // ------------------------------------------------------------------
193 int session_wait( transport_session* session, int timeout );
194
195 // ---------------------------------------------------------------------------------
196 // Sends the given Jabber message
197 // ---------------------------------------------------------------------------------
198 int session_send_msg( transport_session* session, transport_message* msg );
199
200 // ---------------------------------------------------------------------------------
201 // Returns 1 if this session is connected to the jabber server. 0 otherwise
202 // ---------------------------------------------------------------------------------
203 int session_connected( transport_session* );
204
205 // ------------------------------------------------------------------
206 // Deallocates session memory
207 // ------------------------------------------------------------------
208 int session_free( transport_session* session );
209
210 // ------------------------------------------------------------------
211 // Connects to the Jabber server.  Waits at most connect_timeout
212 // seconds before failing
213 // ------------------------------------------------------------------
214 int session_connect( transport_session* session, 
215                 const char* username, const char* password, 
216                 const char* resource, int connect_timeout, 
217                 enum TRANSPORT_AUTH_TYPE auth_type );
218
219 int session_disconnect( transport_session* session );
220
221 #ifdef __cplusplus
222 }
223 #endif
224
225 #endif