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