]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_app_session.h
Patch from Scott McKellar:
[OpenSRF.git] / include / opensrf / osrf_app_session.h
1 #ifndef _OSRF_APP_SESSION
2 #define _OSRF_APP_SESSION
3
4 #include <opensrf/transport_client.h>
5 #include <opensrf/osrf_message.h>
6 #include <opensrf/osrf_system.h>
7 #include <opensrf/string_array.h>
8 #include <opensrf/osrfConfig.h>
9 #include <opensrf/osrf_hash.h>
10 #include <opensrf/osrf_list.h>
11
12 #include <opensrf/osrf_json.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, int* recvd );
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 typedef struct osrf_app_request_struct osrf_app_request;
43 typedef struct osrf_app_request_struct osrfAppRequest;
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
51         //osrfAppRequest* request_queue;
52
53         osrfList* request_queue;
54
55         /** The original remote id of the remote service we're talking to */
56         char* orig_remote_id;
57         /** The current remote id of the remote service we're talking to */
58         char* remote_id;
59
60         /** Who we're talking to if we're a client.  
61                 what app we're serving if we're a server */
62         char* remote_service;
63
64         /** The current request thread_trace */
65         int thread_trace;
66         /** Our ID */
67         char* session_id;
68
69         /* true if this session does not require connect messages */
70         int stateless;
71
72         /** The connect state */
73         enum OSRF_SESSION_STATE state;
74
75         /** SERVER or CLIENT */
76         enum OSRF_SESSION_TYPE type;
77
78         /** the current locale for this session **/
79         char* session_locale;
80
81         /* let the user use the session to store their own session data */
82         void* userData;
83
84         void (*userDataFree) (void*);
85 };
86 typedef struct osrf_app_session_struct osrf_app_session;
87 typedef struct osrf_app_session_struct osrfAppSession;
88
89
90
91 // -------------------------------------------------------------------------- 
92 // PUBLIC API ***
93 // -------------------------------------------------------------------------- 
94
95 /** Allocates a initializes a new app_session */
96 osrfAppSession* osrfAppSessionClientInit( const char* remote_service );
97 osrfAppSession* osrf_app_client_session_init( const char* remote_service );
98
99 /** Allocates and initializes a new server session.  The global session cache
100   * is checked to see if this session already exists, if so, it's returned 
101   */
102 osrfAppSession* osrf_app_server_session_init(
103                 const char* session_id, const char* our_app, const char* remote_id );
104
105 /** sets the default locale for a session **/
106 char* osrf_app_session_set_locale( osrfAppSession*, const char* );
107
108 /** returns a session from the global session hash */
109 osrfAppSession* osrf_app_session_find_session( const char* session_id );
110
111 /** Builds a new app_request object with the given payload andn returns
112   * the id of the request.  This id is then used to perform work on the
113   * requeset.
114   */
115 int osrfAppSessionMakeRequest(
116                 osrfAppSession* session, const jsonObject* params,
117                 const char* method_name, int protocol, string_array* param_strings);
118
119 int osrf_app_session_make_req( 
120                 osrfAppSession* session, const jsonObject* params,
121                 const char* method_name, int protocol, string_array* param_strings);
122
123 /** Sets the given request to complete state */
124 void osrf_app_session_set_complete( osrfAppSession* session, int request_id );
125
126 /** Returns true if the given request is complete */
127 int osrf_app_session_request_complete( const osrfAppSession* session, int request_id );
128
129 /** Does a recv call on the given request */
130 osrf_message* osrfAppSessionRequestRecv(
131                 osrfAppSession* session, int request_id, int timeout );
132 osrf_message* osrf_app_session_request_recv( 
133                 osrfAppSession* session, int request_id, int timeout );
134
135 /** Removes the request from the request set and frees the reqest */
136 void osrf_app_session_request_finish( osrfAppSession* session, int request_id );
137
138 /** Resends the orginal request with the given request id */
139 int osrf_app_session_request_resend( osrfAppSession*, int request_id );
140
141 /** Resets the remote connection target to that of the original*/
142 void osrf_app_session_reset_remote( osrfAppSession* );
143
144 /** Sets the remote target to 'remote_id' */
145 void osrf_app_session_set_remote( osrfAppSession* session, const char* remote_id );
146
147 /** pushes the given message into the result list of the app_request
148   * whose request_id matches the messages thread_trace 
149   */
150 int osrf_app_session_push_queue( osrfAppSession*, osrf_message* msg );
151
152 /** Attempts to connect to the remote service. Returns 1 on successful 
153   * connection, 0 otherwise.
154   */
155 int osrf_app_session_connect( osrfAppSession* );
156 int osrfAppSessionConnect( osrfAppSession* );
157
158 /** Sends a disconnect message to the remote service.  No response is expected */
159 int osrf_app_session_disconnect( osrfAppSession* );
160
161 /**  Waits up to 'timeout' seconds for some data to arrive.
162   * Any data that arrives will be processed according to its
163   * payload and message type.  This method will return after
164   * any data has arrived.
165   */
166 int osrf_app_session_queue_wait( osrfAppSession*, int timeout, int* recvd );
167
168 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the 
169   * global session cache and frees the session.  Needless to say, only call this when the
170   * session is completey done.
171   */
172 void osrf_app_session_destroy ( osrfAppSession* );
173 void osrfAppSessionFree( osrfAppSession* );
174
175 /* tells the request to reset it's wait timeout */
176 void osrf_app_session_request_reset_timeout( osrfAppSession* session, int req_id );
177
178 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, const jsonObject* data );
179 int osrfAppRequestRespondComplete(
180                 osrfAppSession* ses, int requestId, const jsonObject* data ); 
181
182 int osrfAppSessionStatus( osrfAppSession* ses, int type,
183                 const char* name, int reqId, const char* message );
184
185 void osrfAppSessionCleanup();
186
187
188
189 #endif