added checking of result when building demos
[urisagit/Stem.git] / bin / stem_msg
CommitLineData
4536f655 1#!/usr/local/bin/perl
2
3use Getopt::Long ;
4use strict ;
5
6use Stem ;
7
8my %args ;
9my $hub_name ;
10my $portal ;
11
12parse_args() ;
13
14setup_hub() ;
15
16send_msg() ;
17
18Stem::Event::start_loop() ;
19
20# no return from here.
21######################
22
23sub setup_hub {
24
25 $hub_name = "Stem_msg_$$" ;
26
27 Stem::Route::register_class( __PACKAGE__ ) ;
28
29 Stem::Hub->new( 'reg_name' => $hub_name ) ;
30
31 my @portal_args ;
32
33 push @portal_args, ( 'host' => $args{'host'} ) if $args{'host'} ;
34 push @portal_args, ( 'port' => $args{'port'} ) if $args{'port'} ;
35
36#print "portal args: @portal_args\n" ;
37
38 $portal = Stem::Portal->new( @portal_args ) ;
39
40 die "Can't create Portal: $portal" if $portal ;
41}
42
43sub send_msg {
44
45 my ( @msg_args, @target ) ;
46
47 if ( $args{'cmd'} ) {
48
49 @msg_args = ( 'type' => 'cmd', 'cmd' => $args{'cmd'} ) ;
50 }
51 else {
52
53 @msg_args = ( 'type' => 'data' ) ;
54 }
55
56 @target = ( 'to_target' => $args{'target'} ) if $args{'target'} ;
57
58 push( @msg_args, ( 'ack_req' => 1 ) ) if $args{'ack'} ;
59
60 my $data = exists( $args{'data'} ) ? $args{'data'} : '' ;
61
62 my $msg = Stem::Msg->new(
63 'to_hub' => 'DEFAULT',
64 'to_cell' => $args{'cell'},
65 @target,
66 'from_cell' => __PACKAGE__,
67 'from_hub' => $hub_name,
68 'data' => \$data,
69 @msg_args,
70 ) ;
71
72 $msg->dispatch() ;
73}
74
75# this is the class method that gets back the response and ack messages.
76
77sub msg_in {
78
79 my( $class, $msg ) = @_ ;
80
81 if( $msg->type() eq 'msg_ack' ) {
82
83# print "ACK\n" ;
84 exit ;
85 }
86
87 if ( my $data = $msg->data() ) {
88
89 print ${$data} ;
90 }
91
92# $portal->shut_down() ;
93
94 exit unless $args{'ack'} ;
95
96 return ;
97}
98
99
100sub parse_args {
101
102 Getopt::Long::Configure( 'no_ignore_case' ) ;
103
104 GetOptions( \%args,
105 'cell|C=s',
106 'hub|H=s',
107 'target|T=s',
108 'cmd|c=s',
109 'data|d=s',
110 'ack|a',
111 'host|h=s',
112 'port|p=s',
113 'help|?',
114 ) ;
115
116#print map "$_ => $args{$_}\n", sort keys %args ;
117
118 usage( '' ) if $args{ 'help' } ;
119
120 usage( 'Missing Cell address' ) unless $args{ 'cell' } ;
121}
122
123sub usage {
124
125 my $err_msg = shift ;
126
127 my $usage = <<'=cut' ;
128=pod
129
130=head1 NAME
131
132stem_msg - Inject a message into a Stem Hub
133
134=head1 SYNOPSIS
135
136stem_msg -cell <cell> [-hub <hub>] [-target <target>]
137 [-cmd <cmd>] [-data <data>] [-ack]
138 [-host <host>] [-port <port>]
139
140 -C <cell> The Stem Cell to send this message to.
141 -cell <cell> This is required.
142
143 -H <hub> The hub which has the addressed Stem Cell.
144 -hub <hub>
145
146 -T <target> The target address of the Stem Cell
147 -target <target>
148
149 -c <cmd> The cmd type to send in the message
150 -cmd <cmd> If no cmd is set, it will be a data type
151 message.
152
153 -d <data> The data to be sent in the message.
154 -data <data> Default is an empty string.
155
156 -a Wait for an acknowledge message before
157 -ack exiting.
158
159 -h <host> The host which the Stem Hub is on.
160 -host <host> Default: localhost
161
162 -p <port> The port which the Stem Portal is listening
163 -port <port> to.
164 Default: 10,000 (probably will change)
165
166=head1 DESCRIPTION
167
168This program is meant to inject a single message into a Stem Hub. You
169set the Cell address with the command line options and then which
170command to execute in that Cell. If you don't set a command, then a
171data message will be sent. You can send data in the message as well.
172
173If the Cell generates a response message, then its data will be
174printed on stdout.
175
176If the -ack option is set, then the message will have the ack_req flag
177will be set in the outgoing message. This will cause an 'ack' type
178message to be sent back after the original message has been
179delivered. This is meant for when you send a message to a Cell which
180doesn't generate a response. It lets this program know that it can
181exit.
182
183=cut
184
185 $usage =~ s/^=\w+.*$//mg ;
186
187 $usage =~ s/\n{2,}/\n\n/ ;
188 $usage =~ s/\A\n+/\n/ ;
189
190 die "$err_msg\n$usage" ;
191}