]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libtransport/transport_client.c
32e3920650478787ddd6f1f184dfb982de7fa5b8
[OpenSRF.git] / 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, char* unix_path, 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, unix_path, client, component );
62
63         client->session->message_callback = client_message_handler;
64         client->error = 0;
65
66         return client;
67 }
68
69
70 int client_connect( transport_client* client, 
71                 char* username, char* password, 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 );
76 }
77
78
79 int client_disconnect( transport_client* client ) {
80         if( client == NULL ) { return 0; }
81         return session_disconnect( client->session );
82 }
83
84 int client_connected( transport_client* client ) {
85         if(client == NULL) return 0;
86         return client->session->state_machine->connected;
87 }
88
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 );
93 }
94
95
96 transport_message* client_recv( transport_client* client, int timeout ) {
97         if( client == NULL ) { return NULL; }
98
99         transport_message_node* node;
100         transport_message* msg;
101
102
103         /* see if there are any message in the messages queue */
104         if( client->m_list->next != NULL ) {
105                 /* pop off the first one... */
106                 node = client->m_list->next;
107                 client->m_list->next = node->next;
108                 msg = node->message;
109                 free( node );
110                 return msg;
111         }
112
113         if( timeout == -1 ) {  /* wait potentially forever for data to arrive */
114
115                 while( client->m_list->next == NULL ) {
116                 //      if( ! session_wait( client->session, -1 ) ) {
117                         int x;
118                         if( (x = session_wait( client->session, -1 )) ) {
119                                 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d\n", x);
120                                 client->error = 1;
121                                 return NULL;
122                         }
123                 }
124
125         } else { /* wait at most timeout seconds */
126
127         
128                 /* if not, loop up to 'timeout' seconds waiting for data to arrive */
129                 time_t start = time(NULL);      
130                 time_t remaining = (time_t) timeout;
131
132                 int counter = 0;
133
134                 int wait_ret;
135                 while( client->m_list->next == NULL && remaining >= 0 ) {
136
137                         if( (wait_ret= session_wait( client->session, remaining)) ) {
138                                 client->error = 1;
139                                 osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d: setting error=1\n", wait_ret);
140                                 return NULL;
141                         }
142
143                         ++counter;
144
145 #ifdef _ROUTER
146                         // session_wait returns -1 if there is no more data and we're a router
147                         if( remaining == 0 ) { // && wait_ret == -1 ) {
148                                 break;
149                         }
150 #else
151                         if( remaining == 0 ) // or infinite loop
152                                 break;
153 #endif
154
155                         remaining -= (int) (time(NULL) - start);
156                 }
157
158         }
159
160         /* again, see if there are any messages in the message queue */
161         if( client->m_list->next != NULL ) {
162                 /* pop off the first one... */
163                 node = client->m_list->next;
164                 client->m_list->next = node->next;
165                 msg = node->message;
166                 free( node );
167                 return msg;
168
169         } else {
170                 return NULL;
171         }
172 }
173
174 /* throw the message into the message queue */
175 void client_message_handler( void* client, transport_message* msg ){
176
177         if(client == NULL) return;
178         if(msg == NULL) return; 
179
180         transport_client* cli = (transport_client*) client;
181
182         size_t len = sizeof(transport_message_node);
183         transport_message_node* node = 
184                 (transport_message_node*) safe_malloc(len);
185         node->type = MESSAGE_LIST_ITEM;
186         node->message = msg;
187
188
189         /* find the last node and put this onto the end */
190         transport_message_node* tail = cli->m_list;
191         transport_message_node* current = tail->next;
192
193         while( current != NULL ) {
194                 tail = current;
195                 current = current->next;
196         }
197         tail->next = node;
198 }
199
200
201 int client_free( transport_client* client ){
202         if(client == NULL) return 0; 
203
204         session_free( client->session );
205         transport_message_node* current = client->m_list->next;
206         transport_message_node* next;
207
208         /* deallocate the list of messages */
209         while( current != NULL ) {
210                 next = current->next;
211                 message_free( current->message );
212                 free(current);
213                 current = next;
214         }
215
216         free( client->m_list );
217         free( client );
218         return 1;
219 }
220