]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/transport_client.c
Fix a bug whereby, if there was only one <service> entry for the
[OpenSRF.git] / src / libopensrf / transport_client.c
1 #include <opensrf/transport_client.h>
2
3 static void client_message_handler( void* client, transport_message* msg );
4
5 //int main( int argc, char** argv );
6
7 /*
8 int main( int argc, char** argv ) {
9
10         transport_message* recv;
11         transport_message* send;
12
13         transport_client* client = client_init( "spacely.georgialibraries.org", 5222 );
14
15         // try to connect, allow 15 second connect timeout 
16         if( client_connect( client, "admin", "asdfjkjk", "system", 15 ) ) {
17                 printf("Connected...\n");
18         } else { 
19                 printf( "NOT Connected...\n" ); exit(99); 
20         }
21
22         while( (recv = client_recv( client, -1 )) ) {
23
24                 if( recv->body ) {
25                         int len = strlen(recv->body);
26                         char buf[len + 20];
27                         osrf_clearbuf( buf, 0, sizeof(buf)); 
28                         sprintf( buf, "Echoing...%s", recv->body );
29                         send = message_init( buf, "Echoing Stuff", "12345", recv->sender, "" );
30                 } else {
31                         send = message_init( " * ECHOING * ", "Echoing Stuff", "12345", recv->sender, "" );
32                 }
33
34                 if( send == NULL ) { printf("something's wrong"); }
35                 client_send_message( client, send );
36                                 
37                 message_free( send );
38                 message_free( recv );
39         }
40
41         printf( "ended recv loop\n" );
42
43         return 0;
44
45 }
46 */
47
48
49 transport_client* client_init( const char* server, int port, const char* unix_path, int component ) {
50
51         if(server == NULL) return NULL;
52
53         /* build and clear the client object */
54         transport_client* client = safe_malloc( sizeof( transport_client) );
55
56         /* start with an empty message queue */
57         client->msg_q_head = NULL;
58         client->msg_q_tail = NULL;
59         
60         /* build the session */
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                 const char* username, const char* password, const 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( const 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         int error = 0;  /* boolean */
100
101         if( NULL == client->msg_q_head ) {
102
103                 /* no messaage available?  try to get one */
104                 if( timeout == -1 ) {  /* wait potentially forever for data to arrive */
105
106                         int x;
107                         do {
108                                 if( (x = session_wait( client->session, -1 )) ) {
109                                         osrfLogDebug(OSRF_LOG_MARK, "session_wait returned failure code %d\n", x);
110                                         error = 1;
111                                         break;
112                                 }
113                         } while( client->msg_q_head == NULL );
114
115                 } else {    /* loop up to 'timeout' seconds waiting for data to arrive  */
116
117                         /* This loop assumes that a time_t is denominated in seconds -- not */
118                         /* guaranteed by Standard C, but a fair bet for Linux or UNIX       */
119
120                         time_t start = time(NULL);
121                         time_t remaining = (time_t) timeout;
122
123                         int wait_ret;
124                         do {
125                                 if( (wait_ret = session_wait( client->session, (int) remaining)) ) {
126                                         error = 1;
127                                         osrfLogDebug(OSRF_LOG_MARK,
128                                                 "session_wait returned failure code %d: setting error=1\n", wait_ret);
129                                         break;
130                                 }
131
132                                 remaining -= time(NULL) - start;
133                         } while( NULL == client->msg_q_head && remaining > 0 );
134                 }
135         }
136         
137         transport_message* msg = NULL;
138
139         if( error )
140                 client->error = 1;
141         else if( client->msg_q_head != NULL ) {
142                 /* got message(s); dequeue the oldest one */
143                 msg = client->msg_q_head;
144                 client->msg_q_head = msg->next;
145                 msg->next = NULL;  /* shouldn't be necessary; nullify for good hygiene */
146                 if( NULL == client->msg_q_head )
147                         client->msg_q_tail = NULL;
148         }
149
150         return msg;
151 }
152
153 // ---------------------------------------------------------------------------
154 // This is the message handler required by transport_session.  This handler
155 // takes an incoming message and adds it to the tail of a message queue.
156 // ---------------------------------------------------------------------------
157 static void client_message_handler( void* client, transport_message* msg ){
158
159         if(client == NULL) return;
160         if(msg == NULL) return; 
161
162         transport_client* cli = (transport_client*) client;
163
164         /* add the new message to the tail of the queue */
165         if( NULL == cli->msg_q_head )
166                 cli->msg_q_tail = cli->msg_q_head = msg;
167         else {
168                 cli->msg_q_tail->next = msg;
169                 cli->msg_q_tail = msg;
170         }
171         msg->next = NULL;
172 }
173
174
175 int client_free( transport_client* client ){
176         if(client == NULL) return 0; 
177
178         session_free( client->session );
179         transport_message* current = client->msg_q_head;
180         transport_message* next;
181
182         /* deallocate the list of messages */
183         while( current != NULL ) {
184                 next = current->next;
185                 message_free( current );
186                 current = next;
187         }
188
189         free( client );
190         return 1;
191 }
192