1 #include "libjson/json.h"
2 #include "opensrf/transport_client.h"
3 #include "osrf_message.h"
4 #include "osrf_system.h"
5 #include "string_array.h"
6 #include "osrf_config.h"
8 #ifndef OSRF_APP_SESSION
9 #define OSRF_APP_SESSION
12 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
13 #define DEF_QUEUE_SIZE
15 enum OSRF_SESSION_STATE { OSRF_SESSION_CONNECTING, OSRF_SESSION_CONNECTED, OSRF_SESSION_DISCONNECTED };
16 enum OSRF_SESSION_TYPE { OSRF_SESSION_SERVER, OSRF_SESSION_CLIENT };
18 struct osrf_app_request_struct {
19 /** Our controlling session */
20 struct osrf_app_session_struct* session;
24 /** True if we have received a 'request complete' message from our request */
26 /** Our original request payload */
27 osrf_message* payload;
28 /** List of responses to our request */
31 /** So we can be listified */
32 struct osrf_app_request_struct* next;
34 typedef struct osrf_app_request_struct osrf_app_request;
36 struct osrf_app_session_struct {
38 /** Our messag passing object */
39 transport_client* transport_handle;
40 /** Cache of active app_request objects */
41 osrf_app_request* request_queue;
43 /** The original remote id of the remote service we're talking to */
45 /** The current remote id of the remote service we're talking to */
48 /** Who we're talking to */
51 /** The current request thread_trace */
56 /* true if this session does not require connect messages */
59 /** The connect state */
60 enum OSRF_SESSION_STATE state;
62 /** SERVER or CLIENT */
63 enum OSRF_SESSION_TYPE type;
65 /** So we can be listified */
66 struct osrf_app_session_struct* next;
68 typedef struct osrf_app_session_struct osrf_app_session;
72 // --------------------------------------------------------------------------
74 // --------------------------------------------------------------------------
76 /** Allocates a initializes a new app_session */
77 osrf_app_session* osrf_app_client_session_init( char* remote_service );
79 /** Allocates and initializes a new server session. The global session cache
80 * is checked to see if this session already exists, if so, it's returned
82 osrf_app_session* osrf_app_server_session_init(
83 char* session_id, char* our_app, char* remote_service, char* remote_id );
85 /** returns a session from the global session hash */
86 osrf_app_session* osrf_app_session_find_session( char* session_id );
88 /** Builds a new app_request object with the given payload andn returns
89 * the id of the request. This id is then used to perform work on the
92 int osrf_app_session_make_request(
93 osrf_app_session* session, json* params,
94 char* method_name, int protocol, string_array* arr );
96 /** Sets the given request to complete state */
97 void osrf_app_session_set_complete( osrf_app_session* session, int request_id );
99 /** Returns true if the given request is complete */
100 int osrf_app_session_request_complete( osrf_app_session* session, int request_id );
102 /** Does a recv call on the given request */
103 osrf_message* osrf_app_session_request_recv(
104 osrf_app_session* session, int request_id, int timeout );
106 /** Removes the request from the request set and frees the reqest */
107 void osrf_app_session_request_finish( osrf_app_session* session, int request_id );
109 /** Resends the orginal request with the given request id */
110 int osrf_app_session_request_resend( osrf_app_session*, int request_id );
112 /** Resets the remote connection target to that of the original*/
113 void osrf_app_session_reset_remote( osrf_app_session* );
115 /** Sets the remote target to 'remote_id' */
116 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id );
118 /** pushes the given message into the result list of the app_request
119 * whose request_id matches the messages thread_trace
121 int osrf_app_session_push_queue( osrf_app_session*, osrf_message* msg );
123 /** Attempts to connect to the remote service. Returns 1 on successful
124 * connection, 0 otherwise.
126 int osrf_app_session_connect( osrf_app_session* );
128 /** Sends a disconnect message to the remote service. No response is expected */
129 int osrf_app_session_disconnect( osrf_app_session* );
131 /** Waits up to 'timeout' seconds for some data to arrive.
132 * Any data that arrives will be processed according to its
133 * payload and message type. This method will return after
134 * any data has arrived.
136 int osrf_app_session_queue_wait( osrf_app_session*, int timeout );
138 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the
139 * global session cache and frees the session. Needless to say, only call this when the
140 * session is completey done.
142 void osrf_app_session_destroy ( osrf_app_session* );
146 // --------------------------------------------------------------------------
147 // --------------------------------------------------------------------------
149 // --------------------------------------------------------------------------
151 /** Allocations and initializes a new app_request object */
152 osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg );
154 /** Frees memory used by an app_request object */
155 void _osrf_app_request_free( osrf_app_request * req );
157 /** Pushes the given message onto the list of 'responses' to this request */
158 void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload );
160 /** Checks the receive queue for messages. If any are found, the first
161 * is popped off and returned. Otherwise, this method will wait at most timeout
162 * seconds for a message to appear in the receive queue. Once it arrives it is returned.
163 * If no messages arrive in the timeout provided, null is returned.
165 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout );
167 /** Resend this requests original request message */
168 int _osrf_app_request_resend( osrf_app_request* req );
171 // --------------------------------------------------------------------------
172 // --------------------------------------------------------------------------
174 // --------------------------------------------------------------------------
176 /** Returns the app_request with the given thread_trace (request_id) */
177 osrf_app_request* _osrf_app_session_get_request( osrf_app_session*, int thread_trace );
179 /** frees memory held by a session. Note: We delete all requests in the request list */
180 void _osrf_app_session_free( osrf_app_session* );
182 /** adds a session to the global session cache */
183 void _osrf_app_session_push_session( osrf_app_session* );
185 /** removes from global session cache */
186 void _osrf_app_session_remove_session( char* session_id );
188 /** Adds an app_request to the request set */
189 void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req );
191 /** Removes an app_request from this session request set, freeing the request object */
192 void _osrf_app_session_remove_request( osrf_app_session*, osrf_app_request* req );
194 /** Send the given message */
195 int _osrf_app_session_send( osrf_app_session*, osrf_message* msg );