]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libstack/osrf_app_session.h
bdb16442695ff8591d509536875f8963d242619d
[OpenSRF.git] / src / libstack / osrf_app_session.h
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"
6
7 #include "objson/object.h"
8 #include "objson/json_parser.h"
9
10 #ifndef OSRF_APP_SESSION
11 #define OSRF_APP_SESSION
12
13
14 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
15 #define DEF_QUEUE_SIZE  
16
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 };
19
20 /* entry point for data into the stack.  gets set in osrf_stack.c */
21 int (*osrf_stack_entry_point) (transport_client* client, int timeout );
22
23 struct osrf_app_request_struct {
24         /** Our controlling session */
25         struct osrf_app_session_struct* session;
26
27         /** our "id" */
28         int request_id;
29         /** True if we have received a 'request complete' message from our request */
30         int complete;
31         /** Our original request payload */
32         osrf_message* payload; 
33         /** List of responses to our request */
34         osrf_message* result;
35
36         /* if set to true, then a call that is waiting on a response, will reset the 
37                 timeout and set this variable back to false */
38         int reset_timeout;
39
40         /** So we can be listified */
41         struct osrf_app_request_struct* next;
42 };
43 typedef struct osrf_app_request_struct osrf_app_request;
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         osrf_app_request* request_queue;
51
52         /** The original remote id of the remote service we're talking to */
53         char* orig_remote_id;
54         /** The current remote id of the remote service we're talking to */
55         char* remote_id;
56
57         /** Who we're talking to if we're a client.  
58                 what app we're serving if we're a server */
59         char* remote_service;
60
61         /** The current request thread_trace */
62         int thread_trace;
63         /** Our ID */
64         char* session_id;
65
66         /* true if this session does not require connect messages */
67         int stateless;
68
69         /** The connect state */
70         enum OSRF_SESSION_STATE state;
71
72         /** SERVER or CLIENT */
73         enum OSRF_SESSION_TYPE type;
74
75         /** So we can be listified */
76         struct osrf_app_session_struct* next;
77
78 };
79 typedef struct osrf_app_session_struct osrf_app_session;
80
81
82
83 // -------------------------------------------------------------------------- 
84 // PUBLIC API ***
85 // -------------------------------------------------------------------------- 
86
87 /** Allocates a initializes a new app_session */
88 osrf_app_session* osrf_app_client_session_init( char* remote_service );
89
90 /** Allocates and initializes a new server session.  The global session cache
91   * is checked to see if this session already exists, if so, it's returned 
92   */
93 osrf_app_session* osrf_app_server_session_init( 
94                 char* session_id, char* our_app, char* remote_id );
95
96 /** returns a session from the global session hash */
97 osrf_app_session* osrf_app_session_find_session( char* session_id );
98
99 /** Builds a new app_request object with the given payload andn returns
100   * the id of the request.  This id is then used to perform work on the
101   * requeset.
102   */
103 int osrf_app_session_make_req( 
104                 osrf_app_session* session, object* params, 
105                 char* method_name, int protocol, string_array* param_strings);
106
107 /** Sets the given request to complete state */
108 void osrf_app_session_set_complete( osrf_app_session* session, int request_id );
109
110 /** Returns true if the given request is complete */
111 int osrf_app_session_request_complete( osrf_app_session* session, int request_id );
112
113 /** Does a recv call on the given request */
114 osrf_message* osrf_app_session_request_recv( 
115                 osrf_app_session* session, int request_id, int timeout );
116
117 /** Removes the request from the request set and frees the reqest */
118 void osrf_app_session_request_finish( osrf_app_session* session, int request_id );
119
120 /** Resends the orginal request with the given request id */
121 int osrf_app_session_request_resend( osrf_app_session*, int request_id );
122
123 /** Resets the remote connection target to that of the original*/
124 void osrf_app_session_reset_remote( osrf_app_session* );
125
126 /** Sets the remote target to 'remote_id' */
127 void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id );
128
129 /** pushes the given message into the result list of the app_request
130   * whose request_id matches the messages thread_trace 
131   */
132 int osrf_app_session_push_queue( osrf_app_session*, osrf_message* msg );
133
134 /** Attempts to connect to the remote service. Returns 1 on successful 
135   * connection, 0 otherwise.
136   */
137 int osrf_app_session_connect( osrf_app_session* );
138
139 /** Sends a disconnect message to the remote service.  No response is expected */
140 int osrf_app_session_disconnect( osrf_app_session* );
141
142 /**  Waits up to 'timeout' seconds for some data to arrive.
143   * Any data that arrives will be processed according to its
144   * payload and message type.  This method will return after
145   * any data has arrived.
146   */
147 int osrf_app_session_queue_wait( osrf_app_session*, int timeout );
148
149 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the 
150   * global session cache and frees the session.  Needless to say, only call this when the
151   * session is completey done.
152   */
153 void osrf_app_session_destroy ( osrf_app_session* );
154
155
156
157 // --------------------------------------------------------------------------
158 // --------------------------------------------------------------------------
159 // Request functions
160 // --------------------------------------------------------------------------
161
162 /** Allocations and initializes a new app_request object */
163 osrf_app_request* _osrf_app_request_init( osrf_app_session* session, osrf_message* msg );
164
165 /** Frees memory used by an app_request object */
166 void _osrf_app_request_free( osrf_app_request * req );
167
168 /** Pushes the given message onto the list of 'responses' to this request */
169 void _osrf_app_request_push_queue( osrf_app_request*, osrf_message* payload );
170
171 /** Checks the receive queue for messages.  If any are found, the first
172   * is popped off and returned.  Otherwise, this method will wait at most timeout 
173   * seconds for a message to appear in the receive queue.  Once it arrives it is returned.
174   * If no messages arrive in the timeout provided, null is returned.
175   */
176 osrf_message* _osrf_app_request_recv( osrf_app_request* req, int timeout );
177
178 /** Resend this requests original request message */
179 int _osrf_app_request_resend( osrf_app_request* req );
180
181
182 /* tells the request to reset it's wait timeout */
183 void osrf_app_session_request_reset_timeout( osrf_app_session* session, int req_id );
184
185 // --------------------------------------------------------------------------
186 // --------------------------------------------------------------------------
187 // Session functions 
188 // --------------------------------------------------------------------------
189
190 /** Returns the app_request with the given thread_trace (request_id) */
191 osrf_app_request* _osrf_app_session_get_request( osrf_app_session*, int thread_trace );
192
193 /** frees memory held by a session. Note: We delete all requests in the request list */
194 void _osrf_app_session_free( osrf_app_session* );
195
196 /** adds a session to the global session cache */
197 void _osrf_app_session_push_session( osrf_app_session* );
198
199 /** removes from global session cache */
200 void _osrf_app_session_remove_session( char* session_id );
201
202 /** Adds an app_request to the request set */
203 void _osrf_app_session_push_request( osrf_app_session*, osrf_app_request* req );
204
205 /** Removes an app_request from this session request set, freeing the request object */
206 void _osrf_app_session_remove_request( osrf_app_session*, osrf_app_request* req );
207
208 /** Send the given message */
209 int _osrf_app_session_send( osrf_app_session*, osrf_message* msg );
210
211 #endif