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