1 #!/usr/local/bin/perl -w
  2 #
  3 #       binary.pl
  4 #
  5 #       HOWTO RUN :
  6 #               type on your keyboard :
  7 #               $ ./binary.pl your.host:port
  8 #
  9 #       Required libraries :
 10 #
 11 #               this demo use xml-rpc protocol to connect to xmlBlaster server.
 12 #               the xml-rpc implementation used in this demo is Frontier.
 13 #
 14 #               - XML Expat
 15 #               install expat (expat-1.95.2 at http://sourceforge.net/projects/expat/)
 16 #               on my linux redhat 7.2 it was already installed.
 17 #               - perl XML Parser :
 18 #               perl module XML-Parser (XML-Parser.2.30 at http://search.cpan.org/search?dist=XML-Parser)
 19 #               - perl XMLRPC :
 20 #               xml-rpc implemeted by Frontier (Frontier-RPC-x.xx).
 21 #               Look at CPAN for this package.
 22 #
 23 
 24 use Frontier::Client;
 25 use Data::Dumper;
 26 use MIME::Base64;
 27 
 28 use strict;
 29 
 30 use lib( '.' );
 31 
 32 use xmlBlaster::Exception ;
 33 use xmlBlaster::XmlBlaster ;
 34 use xmlBlaster::MsgUnit ;
 35 use xmlBlaster::EraseReturnQos ;
 36 
 37 my $coder = Frontier::RPC2->new;
 38 
 39 
 40 my $securityServiceType = 'htpasswd' ;
 41 
 42 my @profiles = (
 43         {'user'=>'admin' ,
 44         'passwd'=>'secret' ,
 45         },
 46         {'user'=>'guest' ,
 47         'passwd'=>'secret' ,
 48         },
 49 );
 50 
 51 # MAIN
 52 try
 53 {
 54         my $server =  $ARGV[0];
 55         if( ! defined($server) ){
 56                 print "give me a server url like : MyHost:8080\n";
 57                 exit ;
 58         }
 59 
 60         #
 61         #       Connecting 2 clients
 62         #
 63 
 64         print "> Construct client I (",$profiles[0]->{'user'},")...\n";
 65         my $xb1 = xmlBlaster::XmlBlaster->new(
 66                                         {
 67                                         'host'=> $server,
 68                                         'user'=> $profiles[0]->{'user'},
 69                                         'passwd'=> $profiles[0]->{'passwd'},
 70                                         'securityService.type'=> 'htpasswd',
 71                                         }
 72                                 );
 73 
 74         print "> Connect client I ...\n";
 75         $xb1->connect();
 76 
 77         print "> Construct client II (",$profiles[1]->{'user'},")...\n";
 78         my $xb2 = xmlBlaster::XmlBlaster->new(
 79                                         {
 80                                         'host'=> $server,
 81                                         'user'=> $profiles[1]->{'user'},
 82                                         'passwd'=> $profiles[1]->{'passwd'},
 83                                         'securityService.type'=> 'htpasswd',
 84                                         }
 85                                 );
 86         print "> Connect client II ...\n";
 87         $xb2->connect();
 88 
 89         #
 90         #
 91         #
 92         print "> Testing Publish/Subscribe ...\n";
 93         testPubGet( $xb1, $xb2 );
 94 
 95         #
 96         #       Leave the place
 97         #
 98 
 99         print "> Disconnect client I ...\n";
100         $xb1->logout();
101         print "> Disconnect client II ...\n";
102         $xb2->logout();
103 
104 }
105 catch
106 {
107     my $exception = shift ;
108         print $exception->dump ;
109         #print '='x40,"\n",Dumper( $exception ),"\n",'='x40,"\n";
110 };
111 
112 
113 ##################################
114 sub testPubGet {
115 
116         my( $srv1, $srv2)=@_;
117 
118         my( $key, $content );
119         my( $keyoid, $messages );
120 
121         #
122         #       User 1 publish message
123         #
124 
125         $key = "<key oid='myHello1' contentMime='text/plain' />" ;
126         open(DAT, "test.bin") || die("Could not open file test.bin!");
127         my $content=<DAT>;
128         print "> Sending binary [\n";
129         print $content;
130         print "]\n";
131         # You need to add a second argument "" to avoid Base64 linebreaks.
132         # Thanks to Chris Cobb for this hint
133         my $b64 = encode_base64($content, "");
134         $content=$coder->base64($b64);
135         print "> user [",$profiles[0]->{'user'},"] publish [",$b64,"]...\n";
136 
137         $keyoid = $srv1->publish( $key, $content );
138 
139         #
140         #       User 2 get message just posted by User 1
141         #
142 
143         print "> user [",$profiles[1]->{'user'},"] get messages ...\n" ;
144 
145         $key = "<key oid='' queryType='XPATH'>/xmlBlaster/key[starts-with(\@oid,'myHel')]</key>" ;
146 
147         $messages = $srv2->get( $key );
148 
149         if( scalar(@$messages) <=0 ){
150                 print "No message found ! It's like a error ! Abort.\n";
151                 return undef ;
152         }
153 
154         print "> Get returned\n";
155         foreach my $message ( @$messages ){
156                 my $written;
157                 my $content;
158                 my $message_unit = xmlBlaster::MsgUnit->new( $message );
159                 open(DAT,">return.bin") || die("Cannot Open File return.bin");
160                 #$content = decode_base64($message_unit->content());
161                 $content = $message_unit->content();
162 
163                 $written = syswrite(DAT, $content, length($content));
164                 die "syswrite failed: $!\n" unless $written == length($content);
165                 #print DAT $message_unit->content();
166                 close(DAT);
167                 print "Found message = [",$message_unit->keyOid()," / ", $content, "]\n";
168         }
169 
170         #
171         #       User 2 erase those messages
172         #
173 
174         print "> user [",$profiles[1]->{'user'},"] erase messages ...\n";
175 
176         my $eraseRetQos_aref = $srv2->erase( $key );
177 
178         foreach( @$eraseRetQos_aref ){
179                 my $eraseRetQos = xmlBlaster::EraseReturnQos->new( $_ );
180                 print "Erased message = [", $eraseRetQos->keyOid() , "]\n";
181         }
182 
183 }#testPubGet
184 
185 
186 1;


syntax highlighted by Code2HTML, v. 0.9.1