425b977e334e9a903715fa44bdd26d0517c6793d
[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 // Jabber state machine.  This is how we know where we are in the Jabber
42 // conversation.
43 // ---------------------------------------------------------------------------------
44 struct jabber_state_machine_struct {
45         int connected;
46         int connecting;
47         int in_message;
48         int in_message_body;
49         int in_thread;
50         int in_subject;
51         int in_error;
52         int in_message_error;
53         int in_iq;
54         int in_presence;
55         int in_status;
56 };
57 typedef struct jabber_state_machine_struct jabber_machine;
58
59
60 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
61
62 // ---------------------------------------------------------------------------------
63 // Transport session.  This maintains all the various parts of a session
64 // ---------------------------------------------------------------------------------
65 struct transport_session_struct {
66
67         /* our socket connection */
68         socket_manager* sock_mgr;
69
70         /* our Jabber state machine */
71         jabber_machine* state_machine;
72         /* our SAX push parser context */
73         xmlParserCtxtPtr parser_ctxt;
74
75         /* our text buffers for holding text data */
76         growing_buffer* body_buffer;
77         growing_buffer* subject_buffer;
78         growing_buffer* thread_buffer;
79         growing_buffer* from_buffer;
80         growing_buffer* recipient_buffer;
81         growing_buffer* status_buffer;
82         growing_buffer* message_error_type;
83         growing_buffer* session_id;
84         int message_error_code;
85
86         /* for OILS extenstions */
87         growing_buffer* router_to_buffer;
88         growing_buffer* router_from_buffer;
89         growing_buffer* router_class_buffer;
90         growing_buffer* router_command_buffer;
91         growing_buffer* osrf_xid_buffer;
92         int router_broadcast;
93
94         /* this can be anything.  It will show up in the 
95                 callbacks for your convenience. Otherwise, it's
96                 left untouched.  */
97         void* user_data;
98
99         char* server;
100         char* unix_path;
101         int     port;
102         int sock_id;
103
104         int component; /* true if we're a component */
105
106         /* the Jabber message callback */
107         void (*message_callback) ( void* user_data, transport_message* msg );
108         //void (iq_callback) ( void* user_data, transport_iq_message* iq );
109 };
110 typedef struct transport_session_struct transport_session;
111
112
113 // ------------------------------------------------------------------
114 // Allocates and initializes the necessary transport session
115 // data structures.
116 // If port > 0, then this session uses  TCP connection.  Otherwise,
117 // if unix_path != NULL, it uses a UNIX domain socket.
118 // ------------------------------------------------------------------
119 transport_session* init_transport( const char* server, int port, 
120         const char* unix_path, void* user_data, int component );
121
122 // ------------------------------------------------------------------
123 // Waits  at most 'timeout' seconds  for data to arrive from the 
124 // TCP handler. A timeout of -1 means to wait indefinitely.
125 // ------------------------------------------------------------------
126 int session_wait( transport_session* session, int timeout );
127
128 // ---------------------------------------------------------------------------------
129 // Sends the given Jabber message
130 // ---------------------------------------------------------------------------------
131 int session_send_msg( transport_session* session, transport_message* msg );
132
133 // ---------------------------------------------------------------------------------
134 // Returns 1 if this session is connected to the jabber server. 0 otherwise
135 // ---------------------------------------------------------------------------------
136 int session_connected( transport_session* );
137
138 // ------------------------------------------------------------------
139 // Deallocates session memory
140 // ------------------------------------------------------------------
141 int session_free( transport_session* session );
142
143 // ------------------------------------------------------------------
144 // Connects to the Jabber server.  Waits at most connect_timeout
145 // seconds before failing
146 // ------------------------------------------------------------------
147 int session_connect( transport_session* session, 
148                 const char* username, const char* password, 
149                 const char* resource, int connect_timeout, 
150                 enum TRANSPORT_AUTH_TYPE auth_type );
151
152 int session_disconnect( transport_session* session );
153
154 #ifdef __cplusplus
155 }
156 #endif
157
158 #endif