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