]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_app_session.h
Merged libopensrf source directories (libtransport, libstack, and utils) into a singl...
[OpenSRF.git] / include / opensrf / osrf_app_session.h
1 #ifndef _OSRF_APP_SESSION
2 #define _OSRF_APP_SESSION
3
4 #include <opensrf/transport_client.h>
5 #include <opensrf/osrf_message.h>
6 #include <opensrf/osrf_system.h>
7 #include <opensrf/string_array.h>
8 #include <opensrf/osrfConfig.h>
9 #include <opensrf/osrf_hash.h>
10 #include <opensrf/osrf_list.h>
11
12 #include <objson/object.h>
13 #include <objson/json_parser.h>
14
15
16
17 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
18 #define DEF_QUEUE_SIZE  
19
20 enum OSRF_SESSION_STATE { OSRF_SESSION_CONNECTING, OSRF_SESSION_CONNECTED, OSRF_SESSION_DISCONNECTED };
21 enum OSRF_SESSION_TYPE { OSRF_SESSION_SERVER, OSRF_SESSION_CLIENT };
22
23 /* entry point for data into the stack.  gets set in osrf_stack.c */
24 int (*osrf_stack_entry_point) (transport_client* client, int timeout, int* recvd );
25
26 struct osrf_app_request_struct {
27         /** Our controlling session */
28         struct osrf_app_session_struct* session;
29
30         /** our "id" */
31         int request_id;
32         /** True if we have received a 'request complete' message from our request */
33         int complete;
34         /** Our original request payload */
35         osrf_message* payload; 
36         /** List of responses to our request */
37         osrf_message* result;
38
39         /* if set to true, then a call that is waiting on a response, will reset the 
40                 timeout and set this variable back to false */
41         int reset_timeout;
42 };
43 typedef struct osrf_app_request_struct osrf_app_request;
44 typedef struct osrf_app_request_struct osrfAppRequest;
45
46 struct osrf_app_session_struct {
47
48         /** Our messag passing object */
49         transport_client* transport_handle;
50         /** Cache of active app_request objects */
51
52         //osrf_app_request* request_queue;
53
54         osrfList* request_queue;
55
56         /** The original remote id of the remote service we're talking to */
57         char* orig_remote_id;
58         /** The current remote id of the remote service we're talking to */
59         char* remote_id;
60
61         /** Who we're talking to if we're a client.  
62                 what app we're serving if we're a server */
63         char* remote_service;
64
65         /** The current request thread_trace */
66         int thread_trace;
67         /** Our ID */
68         char* session_id;
69
70         /* true if this session does not require connect messages */
71         int stateless;
72
73         /** The connect state */
74         enum OSRF_SESSION_STATE state;
75
76         /** SERVER or CLIENT */
77         enum OSRF_SESSION_TYPE type;
78
79         /* let the user use the session to store their own session data */
80         void* userData;
81
82         void (*userDataFree) (void*);
83 };
84 typedef struct osrf_app_session_struct osrf_app_session;
85 typedef struct osrf_app_session_struct osrfAppSession;
86
87
88
89 // -------------------------------------------------------------------------- 
90 // PUBLIC API ***
91 // -------------------------------------------------------------------------- 
92
93 /** Allocates a initializes a new app_session */
94 osrf_app_session* osrfAppSessionClientInit( char* remote_service );
95 osrf_app_session* osrf_app_client_session_init( char* remote_service );
96
97 /** Allocates and initializes a new server session.  The global session cache
98   * is checked to see if this session already exists, if so, it's returned 
99   */
100 osrf_app_session* osrf_app_server_session_init( 
101                 char* session_id, char* our_app, char* remote_id );
102
103 /** returns a session from the global session hash */
104 osrf_app_session* osrf_app_session_find_session( char* session_id );
105
106 /** Builds a new app_request object with the given payload andn returns
107   * the id of the request.  This id is then used to perform work on the
108   * requeset.
109   */
110 int osrfAppSessionMakeRequest(
111                 osrf_app_session* session, jsonObject* params, 
112                 char* method_name, int protocol, string_array* param_strings);
113
114 int osrf_app_session_make_req( 
115                 osrf_app_session* session, jsonObject* params, 
116                 char* method_name, int protocol, string_array* param_strings);
117
118 /** Sets the given request to complete state */
119 void osrf_app_session_set_complete( osrf_app_session* session, int request_id );
120
121 /** Returns true if the given request is complete */
122 int osrf_app_session_request_complete( osrf_app_session* session, int request_id );
123
124 /** Does a recv call on the given request */
125 osrf_message* osrfAppSessionRequestRecv(
126                 osrf_app_session* session, int request_id, int timeout );
127 osrf_message* osrf_app_session_request_recv( 
128                 osrf_app_session* session, int request_id, int timeout );
129
130 /** Removes the request from the request set and frees the reqest */
131 void osrf_app_session_request_finish( osrf_app_session* session, int request_id );
132
133 /** Resends the orginal request with the given request id */
134 int osrf_app_session_request_resend( osrf_app_session*, int request_id );
135
136 /** Resets the remote connection target to that of the original*/
137 void osrf_app_session_reset_remote( osrf_app_session* );
138
139 /** Sets the remote target to 'remote_id' */
140 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id );
141
142 /** pushes the given message into the result list of the app_request
143   * whose request_id matches the messages thread_trace 
144   */
145 int osrf_app_session_push_queue( osrf_app_session*, osrf_message* msg );
146
147 /** Attempts to connect to the remote service. Returns 1 on successful 
148   * connection, 0 otherwise.
149   */
150 int osrf_app_session_connect( osrf_app_session* );
151 int osrfAppSessionConnect( osrf_app_session* );
152
153 /** Sends a disconnect message to the remote service.  No response is expected */
154 int osrf_app_session_disconnect( osrf_app_session* );
155
156 /**  Waits up to 'timeout' seconds for some data to arrive.
157   * Any data that arrives will be processed according to its
158   * payload and message type.  This method will return after
159   * any data has arrived.
160   */
161 int osrf_app_session_queue_wait( osrf_app_session*, int timeout, int* recvd );
162
163 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the 
164   * global session cache and frees the session.  Needless to say, only call this when the
165   * session is completey done.
166   */
167 void osrf_app_session_destroy ( osrf_app_session* );
168 void osrfAppSessionFree( osrfAppSession* );
169
170
171
172 // --------------------------------------------------------------------------
173 // --------------------------------------------------------------------------
174 // Request functions
175 // --------------------------------------------------------------------------
176
177 /** Allocations and initializes a new app_request object */
178 osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg );
179
180 /** Frees memory used by an app_request object */
181 void _osrf_app_request_free( void * req );
182
183 /** Pushes the given message onto the list of 'responses' to this request */
184 void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload );
185
186 /** Checks the receive queue for messages.  If any are found, the first
187   * is popped off and returned.  Otherwise, this method will wait at most timeout 
188   * seconds for a message to appear in the receive queue.  Once it arrives it is returned.
189   * If no messages arrive in the timeout provided, null is returned.
190   */
191 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout );
192
193 /** Resend this requests original request message */
194 int _osrf_app_request_resend( osrf_app_request* req );
195
196
197 /* tells the request to reset it's wait timeout */
198 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id );
199
200 // --------------------------------------------------------------------------
201 // --------------------------------------------------------------------------
202 // Session functions 
203 // --------------------------------------------------------------------------
204
205 /** Returns the app_request with the given thread_trace (request_id) */
206 osrf_app_request* _osrf_app_session_get_request( osrf_app_session*, int thread_trace );
207
208 /** frees memory held by a session. Note: We delete all requests in the request list */
209 void _osrf_app_session_free( osrf_app_session* );
210
211 /** adds a session to the global session cache */
212 void _osrf_app_session_push_session( osrf_app_session* );
213
214 /** Adds an app_request to the request set */
215 void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req );
216
217 /** Removes an app_request from this session request set, freeing the request object */
218 void _osrf_app_session_remove_request( osrf_app_session*, osrf_app_request* req );
219
220 /** Send the given message */
221 int _osrf_app_session_send( osrf_app_session*, osrf_message* msg );
222
223 int osrfAppSessionSendBatch( osrf_app_session*, osrf_message* msgs[], int size );
224
225 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ); 
226 int osrfAppRequestRespondComplete( osrfAppSession* ses, int requestId, jsonObject* data ); 
227
228 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message );
229
230 void osrfAppSessionCleanup();
231
232
233
234 #endif