| 1 | package MyApp::Client; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use MyApp::Service::Chat; | ||
| 7 | use MyApp::Service::Auth; | ||
| 8 | |||
| 9 | use Beekeeper::Client; | ||
| 10 | use Beekeeper::Config; | ||
| 11 | |||
| 12 | my $Help = "Available commands: | ||
| 13 | /login username pass Login | ||
| 14 | /pm username message Send private message | ||
| 15 | /logout End user session | ||
| 16 | /kick user End another user session | ||
| 17 | /ping Measure latency | ||
| 18 | /quit Exit (or use Ctrl-C) | ||
| 19 | "; | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my ($class, %args) = @_; | ||
| 23 | my $self = {}; | ||
| 24 | |||
| 25 | $self->{fh} = $args{'fh'} || \*STDIN; | ||
| 26 | binmode STDOUT, ":utf8"; | ||
| 27 | binmode STDIN, ":utf8"; | ||
| 28 | |||
| 29 | my $config = Beekeeper::Config->read_config_file('client.config.json'); | ||
| 30 | |||
| 31 | # Connect to bus 'frontend', wich will forward requests to 'backend' | ||
| 32 | $self->{client} = Beekeeper::Client->instance( | ||
| 33 | bus_role => "frontend", | ||
| 34 | forward_to => "backend", | ||
| 35 | %$config, | ||
| 36 | ); | ||
| 37 | |||
| 38 | $self->{chat} = MyApp::Service::Chat->new; | ||
| 39 | $self->{auth} = MyApp::Service::Auth->new; | ||
| 40 | |||
| 41 | bless $self, $class; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub run { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | print $Help; | ||
| 48 | |||
| 49 | $self->{chat}->receive_messages( callback => sub { | ||
| 50 | my %msg = @_; | ||
| 51 | print "> "; | ||
| 52 | print $msg{from} . ": " if $msg{from}; | ||
| 53 | print $msg{message} . "\n"; | ||
| 54 | }); | ||
| 55 | |||
| 56 | if ($ARGV[-1] && $ARGV[-1] eq '--test') { | ||
| 57 | $self->run_tests; | ||
| 58 | exit; | ||
| 59 | } | ||
| 60 | |||
| 61 | $self->{hdl} = AnyEvent::Handle->new( fh => $self->{fh} ); | ||
| 62 | |||
| 63 | $self->{auth}->login( username => getpwuid($>), password => '123' ); | ||
| 64 | |||
| 65 | $self->read_line; | ||
| 66 | |||
| 67 | $self->{quit_cv} = AnyEvent->condvar; | ||
| 68 | $self->{quit_cv}->recv; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub read_line { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | $self->{hdl}->push_read( line => sub { | ||
| 75 | my ($hdl, $line) = @_; | ||
| 76 | print "\033[1A\033[K"; # move one line up and clear it | ||
| 77 | eval { $self->process_cmd($line) }; | ||
| 78 | if ($@) { print "Error: $@" } | ||
| 79 | $self->read_line; | ||
| 80 | }); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub process_cmd { | ||
| 84 | my ($self, $line) = @_; | ||
| 85 | |||
| 86 | chomp $line; | ||
| 87 | |||
| 88 | my $chat = $self->{chat}; | ||
| 89 | my $auth = $self->{auth}; | ||
| 90 | my $resp; | ||
| 91 | |||
| 92 | return unless (length $line); | ||
| 93 | |||
| 94 | if ($line =~ m|^/login \s+ (\w+) \s+ (\w+)|ix) { | ||
| 95 | |||
| 96 | $resp = $auth->login( username => $1, password => $2 ); | ||
| 97 | } | ||
| 98 | elsif ($line =~ m|^/kick \s+ (\w+)|ix) { | ||
| 99 | |||
| 100 | $resp = $auth->kick( username => $1 ); | ||
| 101 | } | ||
| 102 | elsif ($line =~ m|^/logout\b|i) { | ||
| 103 | |||
| 104 | $resp = $auth->logout; | ||
| 105 | } | ||
| 106 | elsif ($line =~ m|^/pm \s+ (\w+) \s+ (.+)|ix) { | ||
| 107 | |||
| 108 | $resp = $chat->send_private_message( to_user => $1, message => $2 ); | ||
| 109 | } | ||
| 110 | elsif ($line =~ m|^/ping\b|i) { | ||
| 111 | |||
| 112 | $resp = $chat->ping; | ||
| 113 | print "> Ping: $resp ms\n"; | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | elsif ($line =~ m|^/quit\b|i) { | ||
| 117 | $self->{quit_cv}->send; | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | $resp = $chat->send_message( message => $line ); | ||
| 122 | } | ||
| 123 | |||
| 124 | unless ($resp->success) { | ||
| 125 | print "ERR: " . $resp->message . "\n"; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub run_tests { | ||
| 130 | my $self = shift; | ||
| 131 | |||
| 132 | my @cmds = ( | ||
| 133 | '/login Dave 123', | ||
| 134 | 'Public message', | ||
| 135 | '/pm Dave Private message', | ||
| 136 | '/logout', | ||
| 137 | '/login Zoe 123', | ||
| 138 | 'Hello', | ||
| 139 | '/ping', | ||
| 140 | '/kick Zoe', | ||
| 141 | ); | ||
| 142 | |||
| 143 | foreach my $cmd (@cmds) { | ||
| 144 | $self->process_cmd("$cmd\n"); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; |