]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libtransport/basic_client.c
817b502c982e47e33c0b855ebe685915d432c1af
[Evergreen.git] / OpenSRF / src / libtransport / basic_client.c
1 #include "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                 fatal_handler( "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                 info_handler("Connected...\n");
24          else  
25                 fatal_handler( "NOT Connected...\n" ); 
26         
27         if( (pid=fork()) ) { /* parent */
28
29                 signal(SIGINT, sig_int);
30                 fprintf(stderr, "Listener: %d\n", getpid() );   
31                 char buf[300];
32                 memset(buf, 0, 300);
33                 printf("=> ");
34
35                 while( fgets( buf, 299, stdin) ) {
36
37                         // remove newline
38                         buf[strlen(buf)-1] = '\0';
39
40                         if( strcmp(buf, "exit")==0) { 
41                                 client_free( client );  
42                                 break; 
43                         }
44
45                         send = message_init( buf, "", "123454321", argv[4], NULL );
46                         client_send_message( client, send );
47                         message_free( send );
48                         printf("\n=> ");
49                         memset(buf, 0, 300);
50                 }
51                 fprintf(stderr, "Killing child %d\n", pid );
52                 kill( pid, SIGKILL );
53                 return 0;
54
55         } else {
56
57                 fprintf(stderr, "Sender: %d\n", getpid() );     
58
59                 transport_message* recv;
60                 while( (recv=client_recv( client, -1)) ) {
61                         if( recv->is_error )
62                                 fprintf( stderr, "\nReceived Error\t: ------------------\nFrom:\t\t"
63                                         "%s\nRouterFrom:\t%s\nBody:\t\t%s\nType %s\nCode %d\n=> ", 
64                                         recv->sender, recv->router_from, recv->body, recv->error_type, recv->error_code );
65                         else
66                                 fprintf( stderr, "\nReceived\t: ------------------\nFrom:\t\t"
67                                         "%s\nRouterFrom:\t%s\nBody:\t\t%s\n=> ", recv->sender, recv->router_from, recv->body );
68
69                         message_free( recv );
70                 }
71
72         }
73         return 0;
74
75 }
76
77
78
79