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