]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libopensrf/basic_client.c
Patch that:
[OpenSRF.git] / src / libopensrf / basic_client.c
1 #include <opensrf/transport_client.h>
2 #include "signal.h"
3
4 pid_t pid;
5 void sig_int( int sig ) {
6         fprintf(stderr, "Killing child %d\n", pid );
7         kill( pid, SIGKILL );
8 }
9
10 /* connects and registers with the router */
11 int main( int argc, char** argv ) {
12
13         if( argc < 5 ) {
14                 osrfLogError( OSRF_LOG_MARK, "Usage: %s <username> <host> <resource> <recipient> \n", argv[0] );
15                 return 99;
16         }
17
18         transport_message* send;
19         transport_client* client = client_init( argv[2], 5222, 0 );
20
21         // try to connect, allow 15 second connect timeout 
22         if( client_connect( client, argv[1], "jkjkasdf", argv[3], 15, AUTH_DIGEST ) ) 
23                 osrfLogInfo(OSRF_LOG_MARK, "Connected...\n");
24          else { 
25                 osrfLogError( OSRF_LOG_MARK, "NOT Connected...\n" ); 
26                 return -1;
27          }
28         
29         if( (pid=fork()) ) { /* parent */
30
31                 signal(SIGINT, sig_int);
32                 fprintf(stderr, "Listener: %ld\n", (long) getpid() );   
33                 char buf[300];
34                 osrf_clearbuf(buf, sizeof(buf));
35                 printf("=> ");
36
37                 while( fgets( buf, sizeof(buf), stdin) ) {
38
39                         // remove newline
40                         buf[strlen(buf)-1] = '\0';
41
42                         if( strcmp(buf, "exit")==0) { 
43                                 client_free( client );  
44                                 break; 
45                         }
46
47                         send = message_init( buf, "", "123454321", argv[4], NULL );
48                         client_send_message( client, send );
49                         message_free( send );
50                         printf("\n=> ");
51                         osrf_clearbuf(buf, sizeof(buf));
52                 }
53                 fprintf(stderr, "Killing child %d\n", pid );
54                 kill( pid, SIGKILL );
55                 return 0;
56
57         } else {
58
59                 fprintf(stderr, "Sender: %ld\n", (long) getpid() );     
60
61                 transport_message* recv;
62                 while( (recv=client_recv( client, -1)) ) {
63                         if( recv->is_error )
64                                 fprintf( stderr, "\nReceived Error\t: ------------------\nFrom:\t\t"
65                                         "%s\nRouterFrom:\t%s\nBody:\t\t%s\nType %s\nCode %d\n=> ", 
66                                         recv->sender, recv->router_from, recv->body, recv->error_type, recv->error_code );
67                         else
68                                 fprintf( stderr, "\nReceived\t: ------------------\nFrom:\t\t"
69                                         "%s\nRouterFrom:\t%s\nBody:\t\t%s\n=> ", recv->sender, recv->router_from, recv->body );
70
71                         message_free( recv );
72                 }
73
74         }
75         return 0;
76
77 }
78
79
80
81