1 #!/usr/local/bin/perl -w
  2 #
  3 #       xmlBlasterClient.pl
  4 #
  5 #       HOWTO RUN :
  6 #               type on your keyboard :
  7 #               $ ./xmlBlasterClient.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 # 04/07/02 17:21 mad@ktaland.com
 24 #       upgrade to fit new xmlBlaster implementation
 25 #       done with BRANCH_0_7_9g on date 2002-07-08
 26 #
 27 #       - create package 'XmlBlaster' to encapsulate xml-rpc calls.
 28 #       - create package 'EraseReturnQos'
 29 #       - create package 'PublishReturnQos'
 30 #
 31 # 2002-02-11 16:27 mad@ktaland.com
 32 #
 33 #       - create package 'connectQos'
 34 #       - create package 'messageUnit'
 35 #
 36 # 2001-12-17 11:54 mAd@ktaland.com
 37 #
 38 #       Connect to xmlBlaster via XMLRPC
 39 #       IOR with MICO is to heavy to install ;o{
 40 #
 41 
 42 use Frontier::Client;
 43 use Data::Dumper;
 44 
 45 use strict;
 46 
 47 use lib( '.' );
 48 
 49 use xmlBlaster::Exception ;
 50 use xmlBlaster::XmlBlaster ;
 51 use xmlBlaster::MsgUnit ;
 52 use xmlBlaster::EraseReturnQos ;
 53 
 54 my $securityServiceType = 'htpasswd' ;
 55 
 56 my @profiles = (
 57         {'user'=>'admin' ,
 58         'passwd'=>'secret' ,
 59         },
 60         {'user'=>'guest' ,
 61         'passwd'=>'secret' ,
 62         },
 63 );
 64 
 65 # MAIN
 66 try
 67 {
 68         my $server =  $ARGV[0];
 69         if( ! defined($server) ){
 70                 print "give me a server url like : MyHost:8080\n";
 71                 exit ;
 72         }
 73 
 74         #
 75         #       Connecting 2 clients
 76         #
 77 
 78         print "> Construct client I (",$profiles[0]->{'user'},")...\n";
 79         my $xb1 = xmlBlaster::XmlBlaster->new(
 80                                         {
 81                                         'host'=> $server,
 82                                         'user'=> $profiles[0]->{'user'},
 83                                         'passwd'=> $profiles[0]->{'passwd'},
 84                                         'securityService.type'=> 'htpasswd',
 85                                         }
 86                                 );
 87 
 88         print "> Connect client I ...\n";
 89         $xb1->connect();
 90 
 91         print "> Construct client II (",$profiles[1]->{'user'},")...\n";
 92         my $xb2 = xmlBlaster::XmlBlaster->new(
 93                                         {
 94                                         'host'=> $server,
 95                                         'user'=> $profiles[1]->{'user'},
 96                                         'passwd'=> $profiles[1]->{'passwd'},
 97                                         'securityService.type'=> 'htpasswd',
 98                                         }
 99                                 );
100         print "> Connect client II ...\n";
101         $xb2->connect();
102 
103         #
104         #
105         #
106         print "> Get server informations ...\n";
107         getServerSysInfo( $xb1 );
108 
109         #
110         #
111         #
112         print "> Testing Publish/Subscribe ...\n";
113         testPubGet( $xb1, $xb2 );
114 
115         #
116         #       Leave the place
117         #
118 
119         print "> Disconnect client I ...\n";
120         $xb1->logout();
121         print "> Disconnect client II ...\n";
122         $xb2->logout();
123 
124 }
125 catch
126 {
127     my $exception = shift ;
128         print $exception->dump ;
129         #print '='x40,"\n",Dumper( $exception ),"\n",'='x40,"\n";
130 };
131 
132 
133 ##################################
134 sub testPubGet {
135 
136         my( $srv1, $srv2)=@_;
137 
138         my( $key, $content );
139         my( $keyoid, $messages );
140 
141         #
142         #       User 1 publish 2 messages
143         #
144 
145         $key = "<key oid='myHello1' contentMime='text/plain' />" ;
146         $content = 'my first HELLO!' ;
147         print "> user [",$profiles[0]->{'user'},"] publish [",$content,"]...\n";
148 
149         $keyoid = $srv1->publish( $key, $content );
150 
151         $key = "<key oid='myHello2' contentMime='text/plain' />" ;
152         $content = 'my HELLO number 2!' ;
153         print "> user [",$profiles[0]->{'user'},"] publish [",$content,"]...\n";
154 
155         $keyoid = $srv1->publish( $key, $content );
156 
157         #
158         #       User 2 get messages just posted by User 1
159         #
160 
161         print "> user [",$profiles[1]->{'user'},"] get messages ...\n" ;
162 
163         $key = "<key oid='' queryType='XPATH'>/xmlBlaster/key[starts-with(\@oid,'myHel')]</key>" ;
164 
165         $messages = $srv2->get( $key );
166 
167         if( scalar(@$messages) <=0 ){
168                 print "No message found ! It's like a error ! Abort.\n";
169                 return undef ;
170         }
171 
172         foreach my $message ( @$messages ){
173                 my $message_unit = xmlBlaster::MsgUnit->new( $message );
174                 print "Found message = [",$message_unit->keyOid()," / ", $message_unit->content(), "]\n";
175         }
176 
177         #
178         #       User 2 erase those messages
179         #
180 
181         print "> user [",$profiles[1]->{'user'},"] erase messages ...\n";
182 
183         my $eraseRetQos_aref = $srv2->erase( $key );
184 
185         foreach( @$eraseRetQos_aref ){
186                 my $eraseRetQos = xmlBlaster::EraseReturnQos->new( $_ );
187                 print "Erased message = [", $eraseRetQos->keyOid() , "]\n";
188         }
189 
190 }#testPubGet
191 
192 ##################################
193 sub getServerSysInfo {
194 
195         my( $server )=@_;
196 
197         # Call the server and get its current memory consumption.
198 
199         # 08/07/02 15:39 cyrille@ktaland.com 
200         # from demo/HelloWorld.java :
201         # MsgUnit[] msgs = con.get("<key oid='__cmd:?freeMem'/>", null);
202         #
203 
204         try {
205 
206                 my $messages ;
207                 my $message_unit ;
208         
209                 my @sysInternal = ('__cmd:?totalMem','__cmd:?usedMem','__cmd:?freeMem','__cmd:?clientList' );
210         
211                 foreach my $keyoid ( @sysInternal ){
212         
213                         print "> get $keyoid... \n";
214         
215                         $messages = $server->get( "<key oid='$keyoid' />" );
216                         foreach my $message ( @$messages ){
217 
218                                 my $message_unit = xmlBlaster::MsgUnit->new( $message );
219 
220                                 print " $keyoid = [", $message_unit->content(), "]\n";
221                         }
222                 }
223 
224         }
225         catch
226         {
227             my $exception = shift ;
228                 print $exception->dump ;
229                 #print '='x40,"\n",Dumper( $exception ),"\n",'='x40,"\n";
230         };
231 
232 }#getServerSysInfo
233 
234 1;


syntax highlighted by Code2HTML, v. 0.9.1