Initial revision
[OpenSRF.git] / src / libtransport / basic_client.c
1 #include "transport_client.h"
2 #ifdef DMALLOC
3 #include "dmalloc.h"
4 #endif
5
6 /**
7   * Simple jabber client
8   */
9
10
11
12
13 /* connects and registers with the router */
14 int main( int argc, char** argv ) {
15
16         if( argc < 5 ) {
17                 fatal_handler( "Usage: %s <username> <host> <resource> <recipient> \n", argv[0] );
18                 return 99;
19         }
20
21         transport_message* send;
22         transport_client* client = client_init( argv[2], 5222 );
23
24         // try to connect, allow 15 second connect timeout 
25         if( client_connect( client, argv[1], "asdfjkjk", argv[3], 15 ) ) 
26                 info_handler("Connected...\n");
27          else  
28                 fatal_handler( "NOT Connected...\n" ); 
29         
30         if( fork() ) {
31
32                 fprintf(stderr, "Listener: %d\n", getpid() );   
33                 char buf[300];
34                 memset(buf, 0, 300);
35                 printf("=> ");
36
37                 while( fgets( buf, 299, 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                         memset(buf, 0, 300);
52                 }
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=> ", recv->sender, recv->router_from, recv->body, recv->error_type, recv->error_code );
64                         else
65                                 fprintf( stderr, "\nReceived\t: ------------------\nFrom:\t\t"
66                                         "%s\nRouterFrom:\t%s\nBody:\t\t%s\n=> ", recv->sender, recv->router_from, recv->body );
67
68                         message_free( recv );
69                 }
70
71         }
72         return 0;
73
74 }
75
76
77
78