1 #include <opensrf/transport_client.h>
3 #define MESSAGE_LIST_HEAD 1
4 #define MESSAGE_LIST_ITEM 2
6 // ---------------------------------------------------------------------------
7 // Represents a node in a linked list. The node holds a pointer to the next
8 // node (which is null unless set), a pointer to a transport_message, and
9 // and a type variable (which is not really curently necessary).
10 // ---------------------------------------------------------------------------
11 struct message_list_struct {
12 struct message_list_struct* next;
13 transport_message* message;
16 typedef struct message_list_struct transport_message_list;
17 typedef struct message_list_struct transport_message_node;
19 static void client_message_handler( void* client, transport_message* msg );
21 //int main( int argc, char** argv );
24 int main( int argc, char** argv ) {
26 transport_message* recv;
27 transport_message* send;
29 transport_client* client = client_init( "spacely.georgialibraries.org", 5222 );
31 // try to connect, allow 15 second connect timeout
32 if( client_connect( client, "admin", "asdfjkjk", "system", 15 ) ) {
33 printf("Connected...\n");
35 printf( "NOT Connected...\n" ); exit(99);
38 while( (recv = client_recv( client, -1 )) ) {
41 int len = strlen(recv->body);
43 osrf_clearbuf( buf, 0, sizeof(buf));
44 sprintf( buf, "Echoing...%s", recv->body );
45 send = message_init( buf, "Echoing Stuff", "12345", recv->sender, "" );
47 send = message_init( " * ECHOING * ", "Echoing Stuff", "12345", recv->sender, "" );
50 if( send == NULL ) { printf("something's wrong"); }
51 client_send_message( client, send );
57 printf( "ended recv loop\n" );
65 transport_client* client_init( const char* server, int port, const char* unix_path, int component ) {
67 if(server == NULL) return NULL;
69 /* build and clear the client object */
70 transport_client* client = safe_malloc( sizeof( transport_client) );
72 /* build and clear the message list */
73 client->m_list = safe_malloc( sizeof( transport_message_list ) );
75 client->m_list->next = NULL;
76 client->m_list->message = NULL;
77 client->m_list->type = MESSAGE_LIST_HEAD;
79 /* build the session */
81 client->session = init_transport( server, port, unix_path, client, component );
83 client->session->message_callback = client_message_handler;
90 int client_connect( transport_client* client,
91 const char* username, const char* password, const char* resource,
92 int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
93 if(client == NULL) return 0;
94 return session_connect( client->session, username,
95 password, resource, connect_timeout, auth_type );
99 int client_disconnect( transport_client* client ) {
100 if( client == NULL ) { return 0; }
101 return session_disconnect( client->session );
104 int client_connected( const transport_client* client ) {
105 if(client == NULL) return 0;
106 return client->session->state_machine->connected;
109 int client_send_message( transport_client* client, transport_message* msg ) {
110 if(client == NULL) return 0;
111 if( client->error ) return -1;
112 return session_send_msg( client->session, msg );
116 transport_message* client_recv( transport_client* client, int timeout ) {
117 if( client == NULL ) { return NULL; }
119 transport_message_node* node;
120 transport_message* msg;
123 /* see if there are any message in the messages queue */
124 if( client->m_list->next != NULL ) {
125 /* pop off the first one... */
126 node = client->m_list->next;
127 client->m_list->next = node->next;
133 if( timeout == -1 ) { /* wait potentially forever for data to arrive */
135 while( client->m_list->next == NULL ) {
136 // if( ! session_wait( client->session, -1 ) ) {
138 if( (x = session_wait( client->session, -1 )) ) {
139 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d\n", x);
145 } else { /* wait at most timeout seconds */
148 /* if not, loop up to 'timeout' seconds waiting for data to arrive */
149 time_t start = time(NULL);
150 time_t remaining = (time_t) timeout;
155 while( client->m_list->next == NULL && remaining >= 0 ) {
157 if( (wait_ret= session_wait( client->session, remaining)) ) {
159 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d: setting error=1\n", wait_ret);
166 // session_wait returns -1 if there is no more data and we're a router
167 if( remaining == 0 ) { // && wait_ret == -1 ) {
171 if( remaining == 0 ) // or infinite loop
175 remaining -= (int) (time(NULL) - start);
180 /* again, see if there are any messages in the message queue */
181 if( client->m_list->next != NULL ) {
182 /* pop off the first one... */
183 node = client->m_list->next;
184 client->m_list->next = node->next;
194 // ---------------------------------------------------------------------------
195 // This is the message handler required by transport_session. This handler
196 // takes all incoming messages and puts them into the back of a linked list
198 // ---------------------------------------------------------------------------
199 static void client_message_handler( void* client, transport_message* msg ){
201 if(client == NULL) return;
202 if(msg == NULL) return;
204 transport_client* cli = (transport_client*) client;
206 transport_message_node* node = safe_malloc( sizeof( transport_message_node) );
208 node->type = MESSAGE_LIST_ITEM;
212 /* find the last node and put this onto the end */
213 transport_message_node* tail = cli->m_list;
214 transport_message_node* current = tail->next;
216 while( current != NULL ) {
218 current = current->next;
224 int client_free( transport_client* client ){
225 if(client == NULL) return 0;
227 session_free( client->session );
228 transport_message_node* current = client->m_list->next;
229 transport_message_node* next;
231 /* deallocate the list of messages */
232 while( current != NULL ) {
233 next = current->next;
234 message_free( current->message );
239 free( client->m_list );