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