]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/libtransport/transport_client.c
b7b3b9e87b9fc923c521be006bc4afcb20b6350e
[working/Evergreen.git] / OpenSRF / src / libtransport / transport_client.c
1 #include "transport_client.h"
2
3
4 //int main( int argc, char** argv );
5
6 /*
7 int main( int argc, char** argv ) {
8
9         transport_message* recv;
10         transport_message* send;
11
12         transport_client* client = client_init( "spacely.georgialibraries.org", 5222 );
13
14         // try to connect, allow 15 second connect timeout 
15         if( client_connect( client, "admin", "asdfjkjk", "system", 15 ) ) {
16                 printf("Connected...\n");
17         } else { 
18                 printf( "NOT Connected...\n" ); exit(99); 
19         }
20
21         while( (recv = client_recv( client, -1 )) ) {
22
23                 if( recv->body ) {
24                         int len = strlen(recv->body);
25                         char buf[len + 20];
26                         memset( buf, 0, len + 20); 
27                         sprintf( buf, "Echoing...%s", recv->body );
28                         send = message_init( buf, "Echoing Stuff", "12345", recv->sender, "" );
29                 } else {
30                         send = message_init( " * ECHOING * ", "Echoing Stuff", "12345", recv->sender, "" );
31                 }
32
33                 if( send == NULL ) { printf("something's wrong"); }
34                 client_send_message( client, send );
35                                 
36                 message_free( send );
37                 message_free( recv );
38         }
39
40         printf( "ended recv loop\n" );
41
42         return 0;
43
44 }
45 */
46
47
48 transport_client* client_init( char* server, int port, int component ) {
49
50         if(server == NULL) return NULL;
51
52         /* build and clear the client object */
53         size_t c_size = sizeof( transport_client);
54         transport_client* client = (transport_client*) safe_malloc( c_size );
55
56         /* build and clear the message list */
57         size_t l_size = sizeof( transport_message_list );
58         client->m_list = (transport_message_list*) safe_malloc( l_size );
59
60         client->m_list->type = MESSAGE_LIST_HEAD;
61         client->session = init_transport( server, port, client, component );
62
63
64         if(client->session == NULL) {
65                 fatal_handler( "client_init(): Out of Memory"); 
66                 return NULL;
67         }
68         client->session->message_callback = client_message_handler;
69
70         return client;
71 }
72
73
74 int client_connect( transport_client* client, 
75                 char* username, char* password, char* resource, 
76                 int connect_timeout, enum TRANSPORT_AUTH_TYPE  auth_type ) {
77         if(client == NULL) return 0; 
78         return session_connect( client->session, username, 
79                         password, resource, connect_timeout, auth_type );
80 }
81
82
83 int client_disconnect( transport_client* client ) {
84         if( client == NULL ) { return 0; }
85         return session_disconnect( client->session );
86 }
87
88 int client_connected( transport_client* client ) {
89         if(client == NULL) return 0;
90         return client->session->state_machine->connected;
91 }
92
93 int client_send_message( transport_client* client, transport_message* msg ) {
94         if(client == NULL) return 0;
95         return session_send_msg( client->session, msg );
96 }
97
98
99 transport_message* client_recv( transport_client* client, int timeout ) {
100         if( client == NULL ) { return NULL; }
101
102         transport_message_node* node;
103         transport_message* msg;
104
105
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;
111                 msg = node->message;
112                 free( node );
113                 return msg;
114         }
115
116         if( timeout == -1 ) {  /* wait potentially forever for data to arrive */
117
118                 while( client->m_list->next == NULL ) {
119                         if( ! session_wait( client->session, -1 ) ) {
120                                 return NULL;
121                         }
122                 }
123
124         } else { /* wait at most timeout seconds */
125
126         
127                 /* if not, loop up to 'timeout' seconds waiting for data to arrive */
128                 time_t start = time(NULL);      
129                 time_t remaining = (time_t) timeout;
130
131                 int counter = 0;
132
133                 int wait_ret;
134                 while( client->m_list->next == NULL && remaining >= 0 ) {
135
136                         if( ! (wait_ret= session_wait( client->session, remaining)) ) 
137                                 return NULL;
138
139                         ++counter;
140
141 #ifdef _ROUTER
142                         // session_wait returns -1 if there is no more data and we're a router
143                         if( remaining == 0 && wait_ret == -1 ) {
144                                 break;
145                         }
146 #else
147                         if( remaining == 0 ) // or infinite loop
148                                 break;
149 #endif
150
151                         remaining -= (int) (time(NULL) - start);
152                 }
153
154         }
155
156         /* again, see if there are any messages in the message queue */
157         if( client->m_list->next != NULL ) {
158                 /* pop off the first one... */
159                 node = client->m_list->next;
160                 client->m_list->next = node->next;
161                 msg = node->message;
162                 free( node );
163                 return msg;
164         } else {
165                 return NULL;
166         }
167 }
168
169 /* throw the message into the message queue */
170 void client_message_handler( void* client, transport_message* msg ){
171
172         if(client == NULL) return;
173         if(msg == NULL) return; 
174
175         transport_client* cli = (transport_client*) client;
176
177         size_t len = sizeof(transport_message_node);
178         transport_message_node* node = 
179                 (transport_message_node*) safe_malloc(len);
180         node->type = MESSAGE_LIST_ITEM;
181         node->message = msg;
182
183
184         /* find the last node and put this onto the end */
185         transport_message_node* tail = cli->m_list;
186         transport_message_node* current = tail->next;
187
188         while( current != NULL ) {
189                 tail = current;
190                 current = current->next;
191         }
192         tail->next = node;
193 }
194
195
196 int client_free( transport_client* client ){
197         if(client == NULL) return 0; 
198
199         session_free( client->session );
200         transport_message_node* current = client->m_list->next;
201         transport_message_node* next;
202
203         /* deallocate the list of messages */
204         while( current != NULL ) {
205                 next = current->next;
206                 message_free( current->message );
207                 free(current);
208                 current = next;
209         }
210
211         free( client->m_list );
212         free( client );
213         return 1;
214 }
215