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 osrf_clearbuf( buf, 0, sizeof(buf));
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 transport_client* client = safe_malloc( sizeof( transport_client) );
55 /* build and clear the message list */
56 client->m_list = safe_malloc( sizeof( transport_message_list ) );
58 client->m_list->next = NULL;
59 client->m_list->message = NULL;
60 client->m_list->type = MESSAGE_LIST_HEAD;
62 /* build the session */
64 client->session = init_transport( server, port, unix_path, client, component );
66 client->session->message_callback = client_message_handler;
73 int client_connect( transport_client* client,
74 const char* username, const char* password, const char* resource,
75 int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
76 if(client == NULL) return 0;
77 return session_connect( client->session, username,
78 password, resource, connect_timeout, auth_type );
82 int client_disconnect( transport_client* client ) {
83 if( client == NULL ) { return 0; }
84 return session_disconnect( client->session );
87 int client_connected( const transport_client* client ) {
88 if(client == NULL) return 0;
89 return client->session->state_machine->connected;
92 int client_send_message( transport_client* client, transport_message* msg ) {
93 if(client == NULL) return 0;
94 if( client->error ) return -1;
95 return session_send_msg( client->session, msg );
99 transport_message* client_recv( transport_client* client, int timeout ) {
100 if( client == NULL ) { return NULL; }
102 transport_message_node* node;
103 transport_message* msg;
106 /* see if there are any message in the messages queue */
107 if( client->m_list->next != NULL ) {
108 /* pop off the first one... */
109 node = client->m_list->next;
110 client->m_list->next = node->next;
116 if( timeout == -1 ) { /* wait potentially forever for data to arrive */
118 while( client->m_list->next == NULL ) {
119 // if( ! session_wait( client->session, -1 ) ) {
121 if( (x = session_wait( client->session, -1 )) ) {
122 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d\n", x);
128 } else { /* wait at most timeout seconds */
131 /* if not, loop up to 'timeout' seconds waiting for data to arrive */
132 time_t start = time(NULL);
133 time_t remaining = (time_t) timeout;
138 while( client->m_list->next == NULL && remaining >= 0 ) {
140 if( (wait_ret= session_wait( client->session, remaining)) ) {
142 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d: setting error=1\n", wait_ret);
149 // session_wait returns -1 if there is no more data and we're a router
150 if( remaining == 0 ) { // && wait_ret == -1 ) {
154 if( remaining == 0 ) // or infinite loop
158 remaining -= (int) (time(NULL) - start);
163 /* again, see if there are any messages in the message queue */
164 if( client->m_list->next != NULL ) {
165 /* pop off the first one... */
166 node = client->m_list->next;
167 client->m_list->next = node->next;
177 /* throw the message into the message queue */
178 void client_message_handler( void* client, transport_message* msg ){
180 if(client == NULL) return;
181 if(msg == NULL) return;
183 transport_client* cli = (transport_client*) client;
185 transport_message_node* node = safe_malloc( sizeof( transport_message_node) );
187 node->type = MESSAGE_LIST_ITEM;
191 /* find the last node and put this onto the end */
192 transport_message_node* tail = cli->m_list;
193 transport_message_node* current = tail->next;
195 while( current != NULL ) {
197 current = current->next;
203 int client_free( transport_client* client ){
204 if(client == NULL) return 0;
206 session_free( client->session );
207 transport_message_node* current = client->m_list->next;
208 transport_message_node* next;
210 /* deallocate the list of messages */
211 while( current != NULL ) {
212 next = current->next;
213 message_free( current->message );
218 free( client->m_list );