Skip to content
This repository
Browse code

don't pass around - it's available if needed

  • Loading branch information...
commit 4bb7235b9b471c6bbe47cc1c73c4ff040d3795d5 1 parent 528bb48
Grant McLean authored June 03, 2010
79  bin/bcvi
@@ -74,6 +74,7 @@ sub client_class { return $class_map{client};  }
74 74
 sub server_class { return $class_map{server};  }
75 75
 sub pod_class    { return $class_map{pod};     }
76 76
 sub map_class    { $class_map{$_[1]} = $_[2];  }
  77
+sub sock         { shift->{sock};              }
77 78
 
78 79
 sub installable_files { return @installables;     }
79 80
 sub message_from_code { $response_message{$_[1]}; }
@@ -611,14 +612,14 @@ sub send_command {
611 612
         "Command: "    . $command          . $LF
612 613
     ) or die "Error sending command through backchannel: $!";
613 614
 
614  
-    $self->send_body($sock, \@files);
615  
-    return if $self->check_response($sock) =~ /^(?:200|300)$/;
  615
+    $self->send_body(\@files);
  616
+    return if $self->check_response() =~ /^(?:200|300)$/;
616 617
     die $self->response_message . "\n";
617 618
 }
618 619
 
619 620
 
620 621
 sub send_body {
621  
-    my($self, $sock, $files) = @_;
  622
+    my($self, $files) = @_;
622 623
 
623 624
     my $translate_paths = ! $self->opt('no-path-xlate');
624 625
 
@@ -627,7 +628,7 @@ sub send_body {
627 628
         "$_$LF";
628 629
     } @$files;
629 630
 
630  
-    $sock->write(
  631
+    $self->sock->write(
631 632
         'Content-Length: ' . length($body) . $LF .
632 633
         $LF .
633 634
         $body
@@ -636,12 +637,12 @@ sub send_body {
636 637
 
637 638
 
638 639
 sub check_response {
639  
-    my($self, $sock) = @_;
  640
+    my($self) = @_;
640 641
 
641  
-    my $response = $sock->getline() or die "Server hung up\n";
  642
+    my $response = $self->sock->getline() or die "Server hung up\n";
642 643
     if(my($code, $message) = $response =~ m{^(\d\d\d) (.*)$}) {
643 644
         ($self->{response_code}, $self->{response_message}) = ($code, $message);
644  
-        $self->read_response($sock) if $code eq '300';
  645
+        $self->read_response() if $code eq '300';
645 646
         return $code;
646 647
     }
647 648
     die "Unexpected response: '$response'\n";
@@ -649,8 +650,9 @@ sub check_response {
649 650
 
650 651
 
651 652
 sub read_response {
652  
-    my($self, $sock) = @_;
  653
+    my($self) = @_;
653 654
 
  655
+    my $sock = $self->sock();
654 656
     my $resp = {};
655 657
     while(my($line) = $sock->getline() || '') {
656 658
         chomp($line);
@@ -969,7 +971,6 @@ sub start_listener {
969 971
 
970 972
 
971 973
 sub auth_key     { shift->{auth_key}; }
972  
-sub sock         { shift->{sock};     }
973 974
 sub client_sock  { shift->{client};   }
974 975
 sub request      { shift->{request};  }
975 976
 sub calling_host { shift->{request}->{host_alias};  }
@@ -1055,7 +1056,7 @@ sub accept_loop {
1055 1056
 
1056 1057
     $SIG{CHLD} = 'IGNORE';  # let Perl reap the zombies
1057 1058
 
1058  
-    my $sock = $self->sock;
  1059
+    my $sock = $self->sock();
1059 1060
     while(1) {
1060 1061
         my $new = $sock->accept();
1061 1062
         next if $!{EINTR};
@@ -1064,7 +1065,8 @@ sub accept_loop {
1064 1065
         }
1065 1066
         else {        # In child
1066 1067
             close $sock;
1067  
-            $self->dispatch_request($new);
  1068
+            $self->{sock} = $new;
  1069
+            $self->dispatch_request();
1068 1070
             exit(0);
1069 1071
         }
1070 1072
     }
@@ -1072,16 +1074,16 @@ sub accept_loop {
1072 1074
 
1073 1075
 
1074 1076
 sub dispatch_request {
1075  
-    my($self, $sock) = @_;
  1077
+    my($self) = @_;
1076 1078
 
1077  
-    $self->send_response($sock, 100);
1078  
-    my $req = $self->collect_headers($sock);
  1079
+    $self->send_response(100);
  1080
+    my $req = $self->collect_headers();
1079 1081
     $self->validate_auth_key($req->{auth_key})
1080  
-        or $self->exit_response($sock, 900);
  1082
+        or $self->exit_response(900);
1081 1083
     my $method = $self->command_handler($req->{command})
1082  
-        or $self->exit_response($sock, 910);
1083  
-    $self->$method($sock);
1084  
-    $self->send_response($sock, 200);
  1084
+        or $self->exit_response(910);
  1085
+    $self->$method();
  1086
+    $self->send_response(200);
1085 1087
 }
1086 1088
 
1087 1089
 
@@ -1096,26 +1098,27 @@ sub validate_auth_key {
1096 1098
 
1097 1099
 
1098 1100
 sub send_response {
1099  
-    my($self, $sock, $code) = @_;
  1101
+    my($self, $code) = @_;
1100 1102
 
1101 1103
     my $message = $self->message_from_code($code) || 'Invalid response code';
1102 1104
     $message = Encode::encode('utf8', $message);
1103  
-    $sock->write(qq{$code $message\x0A});
  1105
+    $self->sock->write(qq{$code $message\x0A});
1104 1106
 }
1105 1107
 
1106 1108
 
1107 1109
 sub exit_response {
1108  
-    my($self, $sock, $code) = @_;
  1110
+    my($self, $code) = @_;
1109 1111
 
1110  
-    $self->send_response($sock, $code);
  1112
+    $self->send_response($code);
1111 1113
     exit(0);
1112 1114
 }
1113 1115
 
1114 1116
 
1115 1117
 sub collect_headers {
1116  
-    my($self, $sock) = @_;
  1118
+    my($self) = @_;
1117 1119
 
1118  
-    my $req = {};
  1120
+    my $sock = $self->sock();
  1121
+    my $req  = {};
1119 1122
     while(my($line) = $sock->getline() || '') {
1120 1123
         chomp($line);
1121 1124
         last if $line eq '';
@@ -1130,11 +1133,11 @@ sub collect_headers {
1130 1133
 
1131 1134
 
1132 1135
 sub read_request_body {
1133  
-    my($self, $sock) = @_;
  1136
+    my($self) = @_;
1134 1137
 
1135 1138
     my $bytes = $self->request->{content_length};
1136  
-
1137  
-    my $buf = '';
  1139
+    my $sock  = $self->sock();
  1140
+    my $buf   = '';
1138 1141
     while(my $count = $sock->read($buf, $bytes, length($buf))) {
1139 1142
         $bytes -= $count;
1140 1143
         last if $bytes < 1;
@@ -1144,19 +1147,19 @@ sub read_request_body {
1144 1147
 
1145 1148
 
1146 1149
 sub get_filenames {
1147  
-    my($self, $sock) = @_;
  1150
+    my($self) = @_;
1148 1151
 
1149  
-    return split /\x0a/, Encode::decode('utf8', $self->read_request_body($sock));
  1152
+    return split /\x0a/, Encode::decode('utf8', $self->read_request_body());
1150 1153
 }
1151 1154
 
1152 1155
 
1153 1156
 sub execute_commands_pod {
1154  
-    my($self, $sock) = @_;
  1157
+    my($self) = @_;
1155 1158
 
1156  
-    $self->send_response($sock, 300);
  1159
+    $self->send_response(300);
1157 1160
     my $pod = $self->pod_class->command_detail();
1158 1161
     $pod = Encode::encode('utf8', $pod);
1159  
-    $sock->write(
  1162
+    $self->sock->write(
1160 1163
         'Content-Type: text/pod'          . $LF .
1161 1164
         'Content-Length: ' . length($pod) . $LF .
1162 1165
         $LF .
@@ -1166,30 +1169,30 @@ sub execute_commands_pod {
1166 1169
 
1167 1170
 
1168 1171
 sub execute_vi {
1169  
-    my($self, $sock) = @_;
  1172
+    my($self) = @_;
1170 1173
 
1171 1174
     my $alias = $self->calling_host();
1172  
-    my @files = map { "scp://$alias/$_" } $self->get_filenames($sock);
  1175
+    my @files = map { "scp://$alias/$_" } $self->get_filenames();
1173 1176
     system('gvim', '--', @files);
1174 1177
 }
1175 1178
 
1176 1179
 
1177 1180
 sub execute_viwait {
1178  
-    my($self, $sock) = @_;
  1181
+    my($self) = @_;
1179 1182
 
1180 1183
     my $alias = $self->calling_host();
1181  
-    my @files = map { "scp://$alias/$_" } $self->get_filenames($sock);
  1184
+    my @files = map { "scp://$alias/$_" } $self->get_filenames();
1182 1185
     system('gvim', '-f', '--', @files);
1183 1186
 }
1184 1187
 
1185 1188
 
1186 1189
 sub execute_scpd {
1187  
-    my($self, $sock) = @_;
  1190
+    my($self) = @_;
1188 1191
 
1189 1192
     my $dest = File::Spec->catdir($self->home_directory(), 'Desktop');
1190 1193
 
1191 1194
     my $alias = $self->calling_host();
1192  
-    my @files = map { "$alias:$_" } $self->get_filenames($sock);
  1195
+    my @files = map { "$alias:$_" } $self->get_filenames();
1193 1196
 
1194 1197
     system('scp', '-q', '--', @files, $dest);
1195 1198
 }
24  lib/App/BCVI/Plugins.pod
Source Rendered
@@ -47,10 +47,10 @@ C<gedit> (the GNOME text editor) - I did warn you it was a silly example:
47 47
   use warnings;
48 48
   
49 49
   sub execute_vi {
50  
-      my($self, $sock) = @_;
  50
+      my($self) = @_;
51 51
 
52 52
       my $alias = $self->calling_host();
53  
-      my @files = map { "sftp://${alias}$_" } $self->get_filenames($sock);
  53
+      my @files = map { "sftp://${alias}$_" } $self->get_filenames();
54 54
       system('gedit', '--', @files);
55 55
   }
56 56
   
@@ -271,27 +271,31 @@ This method call requires no arguments:
271 271
 =head1 COMMAND HANDLERS
272 272
 
273 273
 When the listener receives a command it looks up the registered commands to
274  
-locate a handler method.  The handler method will be called and passed a single
275  
-parameter - the socket filehandle for the incoming connection.
  274
+locate a handler method and then calls that method (with no arguments).
276 275
 
277 276
 If the handler method expects a list of filenames, it can get them by calling:
278 277
 
279  
-  $self->get_filenames($sock)
  278
+  $self->get_filenames()
280 279
 
281 280
 Alternatively, if the handler method expects string data rather than filenames,
282 281
 it can call:
283 282
 
284  
-  $self->read_request_body($sock)
  283
+  $self->read_request_body()
285 284
 
286 285
 for non-ASCII text data you may want to decode the bytes to characters using
287 286
 the L<Encode> module:
288 287
 
289  
-  decode('utf8', $self->read_request_body($sock))
  288
+  decode('utf8', $self->read_request_body())
290 289
 
291 290
 The handler can also access the request headers via the hashref returned by:
292 291
 
293 292
   $self->request()
294 293
 
  294
+If for some reason the handler method needs direct read or write access to the
  295
+client socket, it can get the socket filehandle with:
  296
+
  297
+  $self->sock();
  298
+
295 299
 =head2 Response Status Codes
296 300
 
297 301
 You probably don't need to worry about this section - usually a handler does
@@ -315,11 +319,11 @@ client (but most command handlers will never need to use them):
315 319
 
316 320
 You can send a response by calling:
317 321
 
318  
-  $self->send_response($sock, $code)   # eg: $code = 900
  322
+  $self->send_response($code)   # eg: $code = 900
319 323
 
320 324
 There is currently no way to register additional codes, but of course a handler
321  
-routine could send any data back over the socket (using C<< $sock->write >>)
322  
-and then exit.
  325
+routine could make up its own status code, write it directly to the socket
  326
+(using C<< $self->sock->write >>) and then exit.
323 327
 
324 328
 The '300' response is useful for the situation where the client sent a request
325 329
 and is expecting data in the body of the response.  If you want to see an
4  plugins/NotifyDesktop/lib/App/BCVI/NotifyDesktop.pm
@@ -9,11 +9,11 @@ use Encode qw(encode decode);
9 9
 
10 10
 
11 11
 sub execute_notify {
12  
-    my($self, $sock) = @_;
  12
+    my($self) = @_;
13 13
 
14 14
     my $title = "Notification from " . $self->calling_host();
15 15
 
16  
-    my $message = decode('utf8', $self->read_request_body($sock));
  16
+    my $message = decode('utf8', $self->read_request_body());
17 17
 
18 18
     eval { require Desktop::Notify; };
19 19
     if($@) {

0 notes on commit 4bb7235

Please sign in to comment.
Something went wrong with that request. Please try again.