]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/osrf_app_session.h
Prepare for #inclusion in C++ programs
[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_session_struct {
26
27         /** Our messag passing object */
28         transport_client* transport_handle;
29         /** Cache of active app_request objects */
30         osrfList* request_queue;
31
32         /** The original remote id of the remote service we're talking to */
33         char* orig_remote_id;
34         /** The current remote id of the remote service we're talking to */
35         char* remote_id;
36
37         /** Who we're talking to if we're a client.  
38                 what app we're serving if we're a server */
39         char* remote_service;
40
41         /** The current request thread_trace */
42         int thread_trace;
43         /** Our ID */
44         char* session_id;
45
46         /* true if this session does not require connect messages */
47         int stateless;
48
49         /** The connect state */
50         enum OSRF_SESSION_STATE state;
51
52         /** SERVER or CLIENT */
53         enum OSRF_SESSION_TYPE type;
54
55         /** the current locale for this session **/
56         char* session_locale;
57
58         /* let the user use the session to store their own session data */
59         void* userData;
60
61         void (*userDataFree) (void*);
62
63     int transport_error;
64 };
65 typedef struct osrf_app_session_struct osrfAppSession;
66
67
68
69 // -------------------------------------------------------------------------- 
70 // PUBLIC API ***
71 // -------------------------------------------------------------------------- 
72
73 /** Allocates a initializes a new app_session */
74 osrfAppSession* osrfAppSessionClientInit( const 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 osrfAppSession* osrf_app_server_session_init(
80                 const char* session_id, const char* our_app, const char* remote_id );
81
82 /** sets the default locale for a session **/
83 char* osrf_app_session_set_locale( osrfAppSession*, const char* );
84
85 /** returns a session from the global session hash */
86 osrfAppSession* osrf_app_session_find_session( const char* session_id );
87
88 /** Builds a new app_request object with the given payload andn returns
89   * the id of the request.  This id is then used to perform work on the
90   * requeset.
91   */
92 int osrfAppSessionMakeRequest(
93                 osrfAppSession* session, const jsonObject* params,
94                 const char* method_name, int protocol, osrfStringArray* param_strings);
95
96 /** Sets the given request to complete state */
97 void osrf_app_session_set_complete( osrfAppSession* session, int request_id );
98
99 /** Returns true if the given request is complete */
100 int osrf_app_session_request_complete( const osrfAppSession* session, int request_id );
101
102 /** Does a recv call on the given request */
103 osrfMessage* osrfAppSessionRequestRecv(
104                 osrfAppSession* session, int request_id, int timeout );
105
106 /** Removes the request from the request set and frees the reqest */
107 void osrf_app_session_request_finish( osrfAppSession* session, int request_id );
108
109 /** Resends the orginal request with the given request id */
110 int osrf_app_session_request_resend( osrfAppSession*, int request_id );
111
112 /** Resets the remote connection target to that of the original*/
113 void osrf_app_session_reset_remote( osrfAppSession* );
114
115 /** Sets the remote target to 'remote_id' */
116 void osrf_app_session_set_remote( osrfAppSession* session, const char* remote_id );
117
118 /** pushes the given message into the result list of the app_request
119   * whose request_id matches the messages thread_trace 
120   */
121 int osrf_app_session_push_queue( osrfAppSession*, osrfMessage* msg );
122
123 /** Attempts to connect to the remote service. Returns 1 on successful 
124   * connection, 0 otherwise.
125   */
126 int osrfAppSessionConnect( osrfAppSession* );
127
128 /** Sends a disconnect message to the remote service.  No response is expected */
129 int osrf_app_session_disconnect( osrfAppSession* );
130
131 /**  Waits up to 'timeout' seconds for some data to arrive.
132   * Any data that arrives will be processed according to its
133   * payload and message type.  This method will return after
134   * any data has arrived.
135   */
136 int osrf_app_session_queue_wait( osrfAppSession*, int timeout, int* recvd );
137
138 /** Disconnects (if client), frees any attached app_reuqests, removes the session from the 
139   * global session cache and frees the session.  Needless to say, only call this when the
140   * session is completely done.
141   */
142 void osrfAppSessionFree( osrfAppSession* );
143
144 /* tells the request to reset it's wait timeout */
145 void osrf_app_session_request_reset_timeout( osrfAppSession* session, int req_id );
146
147 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, const jsonObject* data );
148 int osrfAppRequestRespondComplete(
149                 osrfAppSession* ses, int requestId, const jsonObject* data ); 
150
151 int osrfAppSessionStatus( osrfAppSession* ses, int type,
152                 const char* name, int reqId, const char* message );
153
154 void osrfAppSessionCleanup();
155
156
157
158 #endif