1 #include "opensrf/transport_client.h"
2 #include "osrf_message.h"
3 #include "osrf_system.h"
4 #include "string_array.h"
5 #include "osrf_config.h"
7 #include "objson/object.h"
8 #include "objson/json_parser.h"
10 #ifndef OSRF_APP_SESSION
11 #define OSRF_APP_SESSION
14 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
15 #define DEF_QUEUE_SIZE
17 enum OSRF_SESSION_STATE { OSRF_SESSION_CONNECTING, OSRF_SESSION_CONNECTED, OSRF_SESSION_DISCONNECTED };
18 enum OSRF_SESSION_TYPE { OSRF_SESSION_SERVER, OSRF_SESSION_CLIENT };
20 struct osrf_app_request_struct {
21 /** Our controlling session */
22 struct osrf_app_session_struct* session;
26 /** True if we have received a 'request complete' message from our request */
28 /** Our original request payload */
29 osrf_message* payload;
30 /** List of responses to our request */
33 /* if set to true, then a call that is waiting on a response, will reset the
34 timeout and set this variable back to false */
37 /** So we can be listified */
38 struct osrf_app_request_struct* next;
40 typedef struct osrf_app_request_struct osrf_app_request;
42 struct osrf_app_session_struct {
44 /** Our messag passing object */
45 transport_client* transport_handle;
46 /** Cache of active app_request objects */
47 osrf_app_request* request_queue;
49 /** The original remote id of the remote service we're talking to */
51 /** The current remote id of the remote service we're talking to */
54 /** Who we're talking to */
57 /** The current request thread_trace */
62 /* true if this session does not require connect messages */
65 /** The connect state */
66 enum OSRF_SESSION_STATE state;
68 /** SERVER or CLIENT */
69 enum OSRF_SESSION_TYPE type;
71 /** So we can be listified */
72 struct osrf_app_session_struct* next;
74 typedef struct osrf_app_session_struct osrf_app_session;
78 // --------------------------------------------------------------------------
80 // --------------------------------------------------------------------------
82 /** Allocates a initializes a new app_session */
83 osrf_app_session* osrf_app_client_session_init( char* remote_service );
85 /** Allocates and initializes a new server session. The global session cache
86 * is checked to see if this session already exists, if so, it's returned
88 osrf_app_session* osrf_app_server_session_init(
89 char* session_id, char* our_app, char* remote_service, char* remote_id );
91 /** returns a session from the global session hash */
92 osrf_app_session* osrf_app_session_find_session( char* session_id );
94 /** Builds a new app_request object with the given payload andn returns
95 * the id of the request. This id is then used to perform work on the
100 int osrf_app_session_make_req(
101 osrf_app_session* session, object* params,
102 char* method_name, int protocol, string_array* param_strings);
105 int osrf_app_session_make_request(
106 osrf_app_session* session, json* params,
107 char* method_name, int protocol, string_array* arr );
110 /** Sets the given request to complete state */
111 void osrf_app_session_set_complete( osrf_app_session* session, int request_id );
113 /** Returns true if the given request is complete */
114 int osrf_app_session_request_complete( osrf_app_session* session, int request_id );
116 /** Does a recv call on the given request */
117 osrf_message* osrf_app_session_request_recv(
118 osrf_app_session* session, int request_id, int timeout );
120 /** Removes the request from the request set and frees the reqest */
121 void osrf_app_session_request_finish( osrf_app_session* session, int request_id );
123 /** Resends the orginal request with the given request id */
124 int osrf_app_session_request_resend( osrf_app_session*, int request_id );
126 /** Resets the remote connection target to that of the original*/
127 void osrf_app_session_reset_remote( osrf_app_session* );
129 /** Sets the remote target to 'remote_id' */
130 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id );
132 /** pushes the given message into the result list of the app_request
133 * whose request_id matches the messages thread_trace
135 int osrf_app_session_push_queue( osrf_app_session*, osrf_message* msg );
137 /** Attempts to connect to the remote service. Returns 1 on successful
138 * connection, 0 otherwise.
140 int osrf_app_session_connect( osrf_app_session* );
142 /** Sends a disconnect message to the remote service. No response is expected */
143 int osrf_app_session_disconnect( osrf_app_session* );
145 /** Waits up to 'timeout' seconds for some data to arrive.
146 * Any data that arrives will be processed according to its
147 * payload and message type. This method will return after
148 * any data has arrived.
150 int osrf_app_session_queue_wait( osrf_app_session*, int timeout );
152 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the
153 * global session cache and frees the session. Needless to say, only call this when the
154 * session is completey done.
156 void osrf_app_session_destroy ( osrf_app_session* );
160 // --------------------------------------------------------------------------
161 // --------------------------------------------------------------------------
163 // --------------------------------------------------------------------------
165 /** Allocations and initializes a new app_request object */
166 osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg );
168 /** Frees memory used by an app_request object */
169 void _osrf_app_request_free( osrf_app_request * req );
171 /** Pushes the given message onto the list of 'responses' to this request */
172 void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload );
174 /** Checks the receive queue for messages. If any are found, the first
175 * is popped off and returned. Otherwise, this method will wait at most timeout
176 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
177 * If no messages arrive in the timeout provided, null is returned.
179 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout );
181 /** Resend this requests original request message */
182 int _osrf_app_request_resend( osrf_app_request* req );
185 /* tells the request to reset it's wait timeout */
186 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id );
188 // --------------------------------------------------------------------------
189 // --------------------------------------------------------------------------
191 // --------------------------------------------------------------------------
193 /** Returns the app_request with the given thread_trace (request_id) */
194 osrf_app_request* _osrf_app_session_get_request( osrf_app_session*, int thread_trace );
196 /** frees memory held by a session. Note: We delete all requests in the request list */
197 void _osrf_app_session_free( osrf_app_session* );
199 /** adds a session to the global session cache */
200 void _osrf_app_session_push_session( osrf_app_session* );
202 /** removes from global session cache */
203 void _osrf_app_session_remove_session( char* session_id );
205 /** Adds an app_request to the request set */
206 void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req );
208 /** Removes an app_request from this session request set, freeing the request object */
209 void _osrf_app_session_remove_request( osrf_app_session*, osrf_app_request* req );
211 /** Send the given message */
212 int _osrf_app_session_send( osrf_app_session*, osrf_message* msg );