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