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