|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +### Connects to compute engine assuming it is exposed on port 7714. |
| 4 | +### Enter a query to execute it. For anything else, |
| 5 | +### @file.json will send the json in file. |
| 6 | + |
| 7 | +#use strict; |
| 8 | +use warnings; |
| 9 | + |
| 10 | +use lib 'lib'; |
| 11 | +use AnyEvent::Handle; |
| 12 | +use AnyEvent::Socket; |
| 13 | +use Devel::Hexdump 'xd'; |
| 14 | +use Cpanel::JSON::XS qw(decode_json encode_json); |
| 15 | +use File::Slurp; |
| 16 | + |
| 17 | +my $con_ready = AnyEvent->condvar; |
| 18 | +my $handle; |
| 19 | +my $connectStr = '{"msg":"open","argument":"","dbname":"rc2","wspaceId":100,"dbuser":"rc2","dbhost":"dbserver","sessionRecId":463}'; |
| 20 | +tcp_connect "localhost", 7714, sub { |
| 21 | + my ($fh) = @_ or return $con_ready->send; |
| 22 | + |
| 23 | + $handle = new AnyEvent::Handle( |
| 24 | + fh => $fh, |
| 25 | + on_error => sub { |
| 26 | + print STDERR "got error\n"; |
| 27 | + $_[0]->destroy; |
| 28 | + }, |
| 29 | + on_eof => sub { |
| 30 | + $handle->destroy; |
| 31 | + }, |
| 32 | + on_read => sub { |
| 33 | + shift->unshift_read ( chunk => 8, sub { |
| 34 | + my ($bit, $len) = unpack("NN", $_[1]); |
| 35 | + shift->unshift_read (chunk => $len, sub { |
| 36 | + print $_[1] . "\n"; |
| 37 | + }); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + my $read_watcher; $read_watcher = AnyEvent->io ( |
| 42 | + fh => $fh, |
| 43 | + poll => "r", |
| 44 | + cb => sub { |
| 45 | + print STDERR "connection open\n"; |
| 46 | + }); |
| 47 | + |
| 48 | + &sendMessage($connectStr); |
| 49 | +# my $data = pack("NNa*", 0x21, length($connectStr), $connectStr); |
| 50 | +# print STDERR xd $data; |
| 51 | +# syswrite $fh, $data; |
| 52 | +}; |
| 53 | + |
| 54 | +my $input; $input = AnyEvent->io ( |
| 55 | + fh => \*STDIN, |
| 56 | + poll => "r", |
| 57 | + cb => sub { |
| 58 | + chomp(my $name = <STDIN>); |
| 59 | + my $cmd = $name; |
| 60 | + print STDERR "cmd = $name\n"; |
| 61 | + if ($name =~ m/^@(.*)/) { |
| 62 | + my $fname = $1; |
| 63 | + unless (-e $fname) { |
| 64 | + warn "failed to read $fname\n"; |
| 65 | + return; |
| 66 | + } |
| 67 | + $cmd = read_file($fname); |
| 68 | + } else { |
| 69 | + my %msg; |
| 70 | + $msg{argument} = $name; |
| 71 | + $msg{msg} = "execScript"; |
| 72 | + $cmd = encode_json(\%msg); |
| 73 | + } |
| 74 | + &sendMessage($cmd); |
| 75 | + }); |
| 76 | + |
| 77 | +$SIG{'INT'} = sub { &sendMessage('{"msg":"close"}'); $con_ready->send; }; |
| 78 | + |
| 79 | +$con_ready->recv; |
| 80 | + |
| 81 | +sub sendMessage { |
| 82 | + my ($msg) = @_; |
| 83 | + print STDERR "writing $msg\n"; |
| 84 | + my $data = pack("NNa*", 0x21, length($msg), $msg); |
| 85 | + $handle->push_write($data); |
| 86 | +# syswrite $fh, $data; |
| 87 | +} |
0 commit comments