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"
11 #include "objson/object.h"
12 #include "objson/json_parser.h"
16 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
17 #define DEF_QUEUE_SIZE
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 };
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 );
25 struct osrf_app_request_struct {
26 /** Our controlling session */
27 struct osrf_app_session_struct* session;
31 /** True if we have received a 'request complete' message from our request */
33 /** Our original request payload */
34 osrf_message* payload;
35 /** List of responses to our request */
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 */
42 /** So we can be listified */
43 struct osrf_app_request_struct* next;
45 typedef struct osrf_app_request_struct osrf_app_request;
46 typedef struct osrf_app_request_struct osrfAppRequest;
48 struct osrf_app_session_struct {
50 /** Our messag passing object */
51 transport_client* transport_handle;
52 /** Cache of active app_request objects */
53 osrf_app_request* request_queue;
55 /** The original remote id of the remote service we're talking to */
57 /** The current remote id of the remote service we're talking to */
60 /** Who we're talking to if we're a client.
61 what app we're serving if we're a server */
64 /** The current request thread_trace */
69 /* true if this session does not require connect messages */
72 /** The connect state */
73 enum OSRF_SESSION_STATE state;
75 /** SERVER or CLIENT */
76 enum OSRF_SESSION_TYPE type;
78 /** So we can be listified */
79 struct osrf_app_session_struct* next;
82 typedef struct osrf_app_session_struct osrf_app_session;
83 typedef struct osrf_app_session_struct osrfAppSession;
87 // --------------------------------------------------------------------------
89 // --------------------------------------------------------------------------
91 /** Allocates a initializes a new app_session */
92 osrf_app_session* osrfAppSessionClientInit( char* remote_service );
93 osrf_app_session* osrf_app_client_session_init( char* remote_service );
95 /** Allocates and initializes a new server session. The global session cache
96 * is checked to see if this session already exists, if so, it's returned
98 osrf_app_session* osrf_app_server_session_init(
99 char* session_id, char* our_app, char* remote_id );
101 /** returns a session from the global session hash */
102 osrf_app_session* osrf_app_session_find_session( char* session_id );
104 /** Builds a new app_request object with the given payload andn returns
105 * the id of the request. This id is then used to perform work on the
108 int osrfAppSessionMakeRequest(
109 osrf_app_session* session, jsonObject* params,
110 char* method_name, int protocol, string_array* param_strings);
112 int osrf_app_session_make_req(
113 osrf_app_session* session, jsonObject* params,
114 char* method_name, int protocol, string_array* param_strings);
116 /** Sets the given request to complete state */
117 void osrf_app_session_set_complete( osrf_app_session* session, int request_id );
119 /** Returns true if the given request is complete */
120 int osrf_app_session_request_complete( osrf_app_session* session, int request_id );
122 /** Does a recv call on the given request */
123 osrf_message* osrfAppSessionRequestRecv(
124 osrf_app_session* session, int request_id, int timeout );
125 osrf_message* osrf_app_session_request_recv(
126 osrf_app_session* session, int request_id, int timeout );
128 /** Removes the request from the request set and frees the reqest */
129 void osrf_app_session_request_finish( osrf_app_session* session, int request_id );
131 /** Resends the orginal request with the given request id */
132 int osrf_app_session_request_resend( osrf_app_session*, int request_id );
134 /** Resets the remote connection target to that of the original*/
135 void osrf_app_session_reset_remote( osrf_app_session* );
137 /** Sets the remote target to 'remote_id' */
138 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id );
140 /** pushes the given message into the result list of the app_request
141 * whose request_id matches the messages thread_trace
143 int osrf_app_session_push_queue( osrf_app_session*, osrf_message* msg );
145 /** Attempts to connect to the remote service. Returns 1 on successful
146 * connection, 0 otherwise.
148 int osrf_app_session_connect( osrf_app_session* );
150 /** Sends a disconnect message to the remote service. No response is expected */
151 int osrf_app_session_disconnect( osrf_app_session* );
153 /** Waits up to 'timeout' seconds for some data to arrive.
154 * Any data that arrives will be processed according to its
155 * payload and message type. This method will return after
156 * any data has arrived.
158 int osrf_app_session_queue_wait( osrf_app_session*, int timeout );
160 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the
161 * global session cache and frees the session. Needless to say, only call this when the
162 * session is completey done.
164 void osrf_app_session_destroy ( osrf_app_session* );
168 // --------------------------------------------------------------------------
169 // --------------------------------------------------------------------------
171 // --------------------------------------------------------------------------
173 /** Allocations and initializes a new app_request object */
174 osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg );
176 /** Frees memory used by an app_request object */
177 void _osrf_app_request_free( osrf_app_request * req );
179 /** Pushes the given message onto the list of 'responses' to this request */
180 void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload );
182 /** Checks the receive queue for messages. If any are found, the first
183 * is popped off and returned. Otherwise, this method will wait at most timeout
184 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
185 * If no messages arrive in the timeout provided, null is returned.
187 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout );
189 /** Resend this requests original request message */
190 int _osrf_app_request_resend( osrf_app_request* req );
193 /* tells the request to reset it's wait timeout */
194 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id );
196 // --------------------------------------------------------------------------
197 // --------------------------------------------------------------------------
199 // --------------------------------------------------------------------------
201 /** Returns the app_request with the given thread_trace (request_id) */
202 osrf_app_request* _osrf_app_session_get_request( osrf_app_session*, int thread_trace );
204 /** frees memory held by a session. Note: We delete all requests in the request list */
205 void _osrf_app_session_free( osrf_app_session* );
207 /** adds a session to the global session cache */
208 void _osrf_app_session_push_session( osrf_app_session* );
210 /** removes from global session cache */
211 void _osrf_app_session_remove_session( char* session_id );
213 /** Adds an app_request to the request set */
214 void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req );
216 /** Removes an app_request from this session request set, freeing the request object */
217 void _osrf_app_session_remove_request( osrf_app_session*, osrf_app_request* req );
219 /** Send the given message */
220 int _osrf_app_session_send( osrf_app_session*, osrf_message* msg );
222 int osrfAppSessionSendBatch( osrf_app_session*, osrf_message* msgs[], int size );
224 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data );
225 int osrfAppRequestRespondComplete( osrfAppSession* ses, int requestId, jsonObject* data );
227 int osrfAppSessionStatus( osrfAppSession* ses, int type, int reqId, char* message );