1 #ifndef _OSRF_APP_SESSION
2 #define _OSRF_APP_SESSION
4 #include "opensrf/transport_client.h"
5 #include "objson/object.h"
6 #include "osrf_message.h"
7 #include "osrf_system.h"
8 #include "opensrf/string_array.h"
9 #include "osrfConfig.h"
10 #include "osrf_hash.h"
11 #include "osrf_list.h"
13 #include "objson/object.h"
14 #include "objson/json_parser.h"
18 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
19 #define DEF_QUEUE_SIZE
21 enum OSRF_SESSION_STATE { OSRF_SESSION_CONNECTING, OSRF_SESSION_CONNECTED, OSRF_SESSION_DISCONNECTED };
22 enum OSRF_SESSION_TYPE { OSRF_SESSION_SERVER, OSRF_SESSION_CLIENT };
24 /* entry point for data into the stack. gets set in osrf_stack.c */
25 int (*osrf_stack_entry_point) (transport_client* client, int timeout, int* recvd );
27 struct osrf_app_request_struct {
28 /** Our controlling session */
29 struct osrf_app_session_struct* session;
33 /** True if we have received a 'request complete' message from our request */
35 /** Our original request payload */
36 osrf_message* payload;
37 /** List of responses to our request */
40 /* if set to true, then a call that is waiting on a response, will reset the
41 timeout and set this variable back to false */
44 typedef struct osrf_app_request_struct osrf_app_request;
45 typedef struct osrf_app_request_struct osrfAppRequest;
47 struct osrf_app_session_struct {
49 /** Our messag passing object */
50 transport_client* transport_handle;
51 /** Cache of active app_request objects */
53 //osrf_app_request* request_queue;
55 osrfList* request_queue;
57 /** The original remote id of the remote service we're talking to */
59 /** The current remote id of the remote service we're talking to */
62 /** Who we're talking to if we're a client.
63 what app we're serving if we're a server */
66 /** The current request thread_trace */
71 /* true if this session does not require connect messages */
74 /** The connect state */
75 enum OSRF_SESSION_STATE state;
77 /** SERVER or CLIENT */
78 enum OSRF_SESSION_TYPE type;
80 /* let the user use the session to store their own session data */
83 void (*userDataFree) (void*);
85 typedef struct osrf_app_session_struct osrf_app_session;
86 typedef struct osrf_app_session_struct osrfAppSession;
90 // --------------------------------------------------------------------------
92 // --------------------------------------------------------------------------
94 /** Allocates a initializes a new app_session */
95 osrf_app_session* osrfAppSessionClientInit( char* remote_service );
96 osrf_app_session* osrf_app_client_session_init( char* remote_service );
98 /** Allocates and initializes a new server session. The global session cache
99 * is checked to see if this session already exists, if so, it's returned
101 osrf_app_session* osrf_app_server_session_init(
102 char* session_id, char* our_app, char* remote_id );
104 /** returns a session from the global session hash */
105 osrf_app_session* osrf_app_session_find_session( char* session_id );
107 /** Builds a new app_request object with the given payload andn returns
108 * the id of the request. This id is then used to perform work on the
111 int osrfAppSessionMakeRequest(
112 osrf_app_session* session, jsonObject* params,
113 char* method_name, int protocol, string_array* param_strings);
115 int osrf_app_session_make_req(
116 osrf_app_session* session, jsonObject* params,
117 char* method_name, int protocol, string_array* param_strings);
119 /** Sets the given request to complete state */
120 void osrf_app_session_set_complete( osrf_app_session* session, int request_id );
122 /** Returns true if the given request is complete */
123 int osrf_app_session_request_complete( osrf_app_session* session, int request_id );
125 /** Does a recv call on the given request */
126 osrf_message* osrfAppSessionRequestRecv(
127 osrf_app_session* session, int request_id, int timeout );
128 osrf_message* osrf_app_session_request_recv(
129 osrf_app_session* session, int request_id, int timeout );
131 /** Removes the request from the request set and frees the reqest */
132 void osrf_app_session_request_finish( osrf_app_session* session, int request_id );
134 /** Resends the orginal request with the given request id */
135 int osrf_app_session_request_resend( osrf_app_session*, int request_id );
137 /** Resets the remote connection target to that of the original*/
138 void osrf_app_session_reset_remote( osrf_app_session* );
140 /** Sets the remote target to 'remote_id' */
141 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id );
143 /** pushes the given message into the result list of the app_request
144 * whose request_id matches the messages thread_trace
146 int osrf_app_session_push_queue( osrf_app_session*, osrf_message* msg );
148 /** Attempts to connect to the remote service. Returns 1 on successful
149 * connection, 0 otherwise.
151 int osrf_app_session_connect( osrf_app_session* );
152 int osrfAppSessionConnect( osrf_app_session* );
154 /** Sends a disconnect message to the remote service. No response is expected */
155 int osrf_app_session_disconnect( osrf_app_session* );
157 /** Waits up to 'timeout' seconds for some data to arrive.
158 * Any data that arrives will be processed according to its
159 * payload and message type. This method will return after
160 * any data has arrived.
162 int osrf_app_session_queue_wait( osrf_app_session*, int timeout, int* recvd );
164 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the
165 * global session cache and frees the session. Needless to say, only call this when the
166 * session is completey done.
168 void osrf_app_session_destroy ( osrf_app_session* );
169 void osrfAppSessionFree( osrfAppSession* );
173 // --------------------------------------------------------------------------
174 // --------------------------------------------------------------------------
176 // --------------------------------------------------------------------------
178 /** Allocations and initializes a new app_request object */
179 osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg );
181 /** Frees memory used by an app_request object */
182 void _osrf_app_request_free( void * req );
184 /** Pushes the given message onto the list of 'responses' to this request */
185 void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload );
187 /** Checks the receive queue for messages. If any are found, the first
188 * is popped off and returned. Otherwise, this method will wait at most timeout
189 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
190 * If no messages arrive in the timeout provided, null is returned.
192 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout );
194 /** Resend this requests original request message */
195 int _osrf_app_request_resend( osrf_app_request* req );
198 /* tells the request to reset it's wait timeout */
199 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id );
201 // --------------------------------------------------------------------------
202 // --------------------------------------------------------------------------
204 // --------------------------------------------------------------------------
206 /** Returns the app_request with the given thread_trace (request_id) */
207 osrf_app_request* _osrf_app_session_get_request( osrf_app_session*, int thread_trace );
209 /** frees memory held by a session. Note: We delete all requests in the request list */
210 void _osrf_app_session_free( osrf_app_session* );
212 /** adds a session to the global session cache */
213 void _osrf_app_session_push_session( osrf_app_session* );
215 /** Adds an app_request to the request set */
216 void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req );
218 /** Removes an app_request from this session request set, freeing the request object */
219 void _osrf_app_session_remove_request( osrf_app_session*, osrf_app_request* req );
221 /** Send the given message */
222 int _osrf_app_session_send( osrf_app_session*, osrf_message* msg );
224 int osrfAppSessionSendBatch( osrf_app_session*, osrf_message* msgs[], int size );
226 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data );
227 int osrfAppRequestRespondComplete( osrfAppSession* ses, int requestId, jsonObject* data );
229 int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message );
231 void osrfAppSessionCleanup();