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