1 #include <opensrf/transport_client.h>
3 static void client_message_handler( void* client, transport_message* msg );
5 //int main( int argc, char** argv );
8 int main( int argc, char** argv ) {
10 transport_message* recv;
11 transport_message* send;
13 transport_client* client = client_init( "spacely.georgialibraries.org", 5222 );
15 // try to connect, allow 15 second connect timeout
16 if( client_connect( client, "admin", "asdfjkjk", "system", 15 ) ) {
17 printf("Connected...\n");
19 printf( "NOT Connected...\n" ); exit(99);
22 while( (recv = client_recv( client, -1 )) ) {
25 int len = strlen(recv->body);
27 osrf_clearbuf( buf, 0, sizeof(buf));
28 sprintf( buf, "Echoing...%s", recv->body );
29 send = message_init( buf, "Echoing Stuff", "12345", recv->sender, "" );
31 send = message_init( " * ECHOING * ", "Echoing Stuff", "12345", recv->sender, "" );
34 if( send == NULL ) { printf("something's wrong"); }
35 client_send_message( client, send );
41 printf( "ended recv loop\n" );
49 transport_client* client_init( const char* server, int port, const char* unix_path, int component ) {
51 if(server == NULL) return NULL;
53 /* build and clear the client object */
54 transport_client* client = safe_malloc( sizeof( transport_client) );
56 /* start with an empty message queue */
57 client->msg_q_head = NULL;
58 client->msg_q_tail = NULL;
60 /* build the session */
61 client->session = init_transport( server, port, unix_path, client, component );
63 client->session->message_callback = client_message_handler;
70 int client_connect( transport_client* client,
71 const char* username, const char* password, const char* resource,
72 int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
73 if(client == NULL) return 0;
74 return session_connect( client->session, username,
75 password, resource, connect_timeout, auth_type );
79 int client_disconnect( transport_client* client ) {
80 if( client == NULL ) { return 0; }
81 return session_disconnect( client->session );
84 int client_connected( const transport_client* client ) {
85 if(client == NULL) return 0;
86 return client->session->state_machine->connected;
89 int client_send_message( transport_client* client, transport_message* msg ) {
90 if(client == NULL) return 0;
91 if( client->error ) return -1;
92 return session_send_msg( client->session, msg );
96 transport_message* client_recv( transport_client* client, int timeout ) {
97 if( client == NULL ) { return NULL; }
99 int error = 0; /* boolean */
101 if( NULL == client->msg_q_head ) {
103 /* no messaage available? try to get one */
104 if( timeout == -1 ) { /* wait potentially forever for data to arrive */
108 if( (x = session_wait( client->session, -1 )) ) {
109 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d\n", x);
113 } while( client->msg_q_head == NULL );
115 } else { /* loop up to 'timeout' seconds waiting for data to arrive */
117 /* This loop assumes that a time_t is denominated in seconds -- not */
118 /* guaranteed by Standard C, but a fair bet for Linux or UNIX */
120 time_t start = time(NULL);
121 time_t remaining = (time_t) timeout;
125 if( (wait_ret = session_wait( client->session, (int) remaining)) ) {
127 osrfLogDebug(OSRF_LOG_MARK,
128 "session_wait returned failure code %d: setting error=1\n", wait_ret);
132 remaining -= time(NULL) - start;
133 } while( NULL == client->msg_q_head && remaining > 0 );
137 transport_message* msg = NULL;
141 else if( client->msg_q_head != NULL ) {
142 /* got message(s); dequeue the oldest one */
143 msg = client->msg_q_head;
144 client->msg_q_head = msg->next;
145 msg->next = NULL; /* shouldn't be necessary; nullify for good hygiene */
146 if( NULL == client->msg_q_head )
147 client->msg_q_tail = NULL;
153 // ---------------------------------------------------------------------------
154 // This is the message handler required by transport_session. This handler
155 // takes an incoming message and adds it to the tail of a message queue.
156 // ---------------------------------------------------------------------------
157 static void client_message_handler( void* client, transport_message* msg ){
159 if(client == NULL) return;
160 if(msg == NULL) return;
162 transport_client* cli = (transport_client*) client;
164 /* add the new message to the tail of the queue */
165 if( NULL == cli->msg_q_head )
166 cli->msg_q_tail = cli->msg_q_head = msg;
168 cli->msg_q_tail->next = msg;
169 cli->msg_q_tail = msg;
175 int client_free( transport_client* client ){
176 if(client == NULL) return 0;
178 session_free( client->session );
179 transport_message* current = client->msg_q_head;
180 transport_message* next;
182 /* deallocate the list of messages */
183 while( current != NULL ) {
184 next = current->next;
185 message_free( current );