1 #ifndef _OSRF_APP_SESSION
2 #define _OSRF_APP_SESSION
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>
12 #include <opensrf/osrf_json.h>
16 #define DEF_RECV_TIMEOUT 6 /* receive timeout */
17 #define DEF_QUEUE_SIZE
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 };
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 );
25 struct osrf_app_session_struct {
27 /** Our messag passing object */
28 transport_client* transport_handle;
29 /** Cache of active app_request objects */
30 osrfList* request_queue;
32 /** The original remote id of the remote service we're talking to */
34 /** The current remote id of the remote service we're talking to */
37 /** Who we're talking to if we're a client.
38 what app we're serving if we're a server */
41 /** The current request thread_trace */
46 /* true if this session does not require connect messages */
49 /** The connect state */
50 enum OSRF_SESSION_STATE state;
52 /** SERVER or CLIENT */
53 enum OSRF_SESSION_TYPE type;
55 /** the current locale for this session **/
58 /* let the user use the session to store their own session data */
61 void (*userDataFree) (void*);
65 typedef struct osrf_app_session_struct osrfAppSession;
69 // --------------------------------------------------------------------------
71 // --------------------------------------------------------------------------
73 /** Allocates a initializes a new app_session */
74 osrfAppSession* osrfAppSessionClientInit( const char* remote_service );
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
79 osrfAppSession* osrf_app_server_session_init(
80 const char* session_id, const char* our_app, const char* remote_id );
82 /** sets the default locale for a session **/
83 char* osrf_app_session_set_locale( osrfAppSession*, const char* );
85 /** returns a session from the global session hash */
86 osrfAppSession* osrf_app_session_find_session( const char* session_id );
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
92 int osrfAppSessionMakeRequest(
93 osrfAppSession* session, const jsonObject* params,
94 const char* method_name, int protocol, osrfStringArray* param_strings);
96 /** Sets the given request to complete state */
97 void osrf_app_session_set_complete( osrfAppSession* session, int request_id );
99 /** Returns true if the given request is complete */
100 int osrf_app_session_request_complete( const osrfAppSession* session, int request_id );
102 /** Does a recv call on the given request */
103 osrfMessage* osrfAppSessionRequestRecv(
104 osrfAppSession* session, int request_id, int timeout );
106 /** Removes the request from the request set and frees the reqest */
107 void osrf_app_session_request_finish( osrfAppSession* session, int request_id );
109 /** Resends the orginal request with the given request id */
110 int osrf_app_session_request_resend( osrfAppSession*, int request_id );
112 /** Resets the remote connection target to that of the original*/
113 void osrf_app_session_reset_remote( osrfAppSession* );
115 /** Sets the remote target to 'remote_id' */
116 void osrf_app_session_set_remote( osrfAppSession* session, const char* remote_id );
118 /** pushes the given message into the result list of the app_request
119 * whose request_id matches the messages thread_trace
121 int osrf_app_session_push_queue( osrfAppSession*, osrfMessage* msg );
123 /** Attempts to connect to the remote service. Returns 1 on successful
124 * connection, 0 otherwise.
126 int osrfAppSessionConnect( osrfAppSession* );
128 /** Sends a disconnect message to the remote service. No response is expected */
129 int osrf_app_session_disconnect( osrfAppSession* );
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.
136 int osrf_app_session_queue_wait( osrfAppSession*, int timeout, int* recvd );
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.
142 void osrfAppSessionFree( osrfAppSession* );
144 /* tells the request to reset it's wait timeout */
145 void osrf_app_session_request_reset_timeout( osrfAppSession* session, int req_id );
147 int osrfAppRequestRespond( osrfAppSession* ses, int requestId, const jsonObject* data );
148 int osrfAppRequestRespondComplete(
149 osrfAppSession* ses, int requestId, const jsonObject* data );
151 int osrfAppSessionStatus( osrfAppSession* ses, int type,
152 const char* name, int reqId, const char* message );
154 void osrfAppSessionCleanup();