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