1 #include <opensrf/transport_client.h>
4 //int main( int argc, char** argv );
7 int main( int argc, char** argv ) {
9 transport_message* recv;
10 transport_message* send;
12 transport_client* client = client_init( "spacely.georgialibraries.org", 5222 );
14 // try to connect, allow 15 second connect timeout
15 if( client_connect( client, "admin", "asdfjkjk", "system", 15 ) ) {
16 printf("Connected...\n");
18 printf( "NOT Connected...\n" ); exit(99);
21 while( (recv = client_recv( client, -1 )) ) {
24 int len = strlen(recv->body);
26 memset( buf, 0, len + 20);
27 sprintf( buf, "Echoing...%s", recv->body );
28 send = message_init( buf, "Echoing Stuff", "12345", recv->sender, "" );
30 send = message_init( " * ECHOING * ", "Echoing Stuff", "12345", recv->sender, "" );
33 if( send == NULL ) { printf("something's wrong"); }
34 client_send_message( client, send );
40 printf( "ended recv loop\n" );
48 transport_client* client_init( const char* server, int port, const char* unix_path, int component ) {
50 if(server == NULL) return NULL;
52 /* build and clear the client object */
53 size_t c_size = sizeof( transport_client);
54 transport_client* client = safe_malloc( c_size );
56 /* build and clear the message list */
57 size_t l_size = sizeof( transport_message_list );
58 client->m_list = safe_malloc( l_size );
60 client->m_list->next = NULL;
61 client->m_list->message = NULL;
62 client->m_list->type = MESSAGE_LIST_HEAD;
64 /* build the session */
66 client->session = init_transport( server, port, unix_path, client, component );
68 client->session->message_callback = client_message_handler;
75 int client_connect( transport_client* client,
76 char* username, char* password, char* resource,
77 int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
78 if(client == NULL) return 0;
79 return session_connect( client->session, username,
80 password, resource, connect_timeout, auth_type );
84 int client_disconnect( transport_client* client ) {
85 if( client == NULL ) { return 0; }
86 return session_disconnect( client->session );
89 int client_connected( transport_client* client ) {
90 if(client == NULL) return 0;
91 return client->session->state_machine->connected;
94 int client_send_message( transport_client* client, transport_message* msg ) {
95 if(client == NULL) return 0;
96 if( client->error ) return -1;
97 return session_send_msg( client->session, msg );
101 transport_message* client_recv( transport_client* client, int timeout ) {
102 if( client == NULL ) { return NULL; }
104 transport_message_node* node;
105 transport_message* msg;
108 /* see if there are any message in the messages queue */
109 if( client->m_list->next != NULL ) {
110 /* pop off the first one... */
111 node = client->m_list->next;
112 client->m_list->next = node->next;
118 if( timeout == -1 ) { /* wait potentially forever for data to arrive */
120 while( client->m_list->next == NULL ) {
121 // if( ! session_wait( client->session, -1 ) ) {
123 if( (x = session_wait( client->session, -1 )) ) {
124 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d\n", x);
130 } else { /* wait at most timeout seconds */
133 /* if not, loop up to 'timeout' seconds waiting for data to arrive */
134 time_t start = time(NULL);
135 time_t remaining = (time_t) timeout;
140 while( client->m_list->next == NULL && remaining >= 0 ) {
142 if( (wait_ret= session_wait( client->session, remaining)) ) {
144 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d: setting error=1\n", wait_ret);
151 // session_wait returns -1 if there is no more data and we're a router
152 if( remaining == 0 ) { // && wait_ret == -1 ) {
156 if( remaining == 0 ) // or infinite loop
160 remaining -= (int) (time(NULL) - start);
165 /* again, see if there are any messages in the message queue */
166 if( client->m_list->next != NULL ) {
167 /* pop off the first one... */
168 node = client->m_list->next;
169 client->m_list->next = node->next;
179 /* throw the message into the message queue */
180 void client_message_handler( void* client, transport_message* msg ){
182 if(client == NULL) return;
183 if(msg == NULL) return;
185 transport_client* cli = (transport_client*) client;
187 size_t len = sizeof(transport_message_node);
188 transport_message_node* node =
189 (transport_message_node*) safe_malloc(len);
190 node->type = MESSAGE_LIST_ITEM;
194 /* find the last node and put this onto the end */
195 transport_message_node* tail = cli->m_list;
196 transport_message_node* current = tail->next;
198 while( current != NULL ) {
200 current = current->next;
206 int client_free( transport_client* client ){
207 if(client == NULL) return 0;
209 session_free( client->session );
210 transport_message_node* current = client->m_list->next;
211 transport_message_node* next;
213 /* deallocate the list of messages */
214 while( current != NULL ) {
215 next = current->next;
216 message_free( current->message );
221 free( client->m_list );