Author: nornagon
Date: 2005-01-03 23:56:42 -0500 (Mon, 03 Jan 2005)
New Revision: 515

Modified:
   trunk/clients/termvisual/termvisual.pl
Log:
Term::Visual client updates:
        * CTCP PING works
        * Lagmeter implemented
        * Time on left of statusbar
        * Various other bugfixes I've forgotten ^_^


Modified: trunk/clients/termvisual/termvisual.pl
===================================================================
--- trunk/clients/termvisual/termvisual.pl      2005-01-04 03:34:24 UTC (rev 
514)
+++ trunk/clients/termvisual/termvisual.pl      2005-01-04 04:56:42 UTC (rev 
515)
@@ -10,6 +10,9 @@
 use warnings;
 use Carp;
 
+use POSIX qw(strftime);
+use Time::HiRes;
+
 use POE;
 use POE::Wheel::SocketFactory;
 use POE::Wheel::ReadWrite;
@@ -22,7 +25,7 @@
 use Haver::Config;
 use Haver::OS;
 
-my (%ucommands, %scommands);
+my (%ucommands, %scommands, $vt, $window_id);
 
 my $config_handle = Haver::Config->new(
     file => "config",
@@ -34,6 +37,7 @@
         Server  => 'localhost',
         Port    => '7070',
         CommandChars    => '/.',
+        TimeFormat  => '%H:%M:%S',
     },
 );
 my $config = $config_handle->config;
@@ -43,6 +47,8 @@
     nick    => 'default on default',
     nickdecs    => 'bright black on default',
     ncolor  => 'default on default',
+    st_frames   => 'cyan on blue',
+    st_values   => 'white on blue',
 );
 
 our %Bindings = (
@@ -58,6 +64,8 @@
         socket_error    => \&handle_socket_error,
         socket_input    => \&handle_socket_input,
         shutdown    => \&handle_shutdown,
+        poke_server => \&poke_server,
+        update_time => \&update_time,
         _stop       => \&handle_stop,
     },
 );
@@ -69,28 +77,33 @@
 sub handle_start {
     my ($kernel, $heap) = @_[KERNEL, HEAP];
 
-    $heap->{vt} = Term::Visual->new(
+    $vt = Term::Visual->new(
         Alias        => "user_interface",
         Common_Input => 1,
         Tab_Complete => \&tab_complete,
     );
 
-    $heap->{vt}->set_palette(%palette);
+    $vt->set_palette(%palette);
 
-    $heap->{window_id} = $heap->{vt}->create_window(
+    $window_id = $vt->create_window(
         Window_Name     => "foo",
-        Status          => { 0 => { format => "Server: %s",
-                               fields => [qw(server)] },
+        Status          => { 0 => {
+                                    format =>
+                                    " [\0(st_values)%s\0(st_frames)] ".
+                                    "[\0(st_values)%s\0(st_frames)] ".
+                                    "[\0(st_values)Lag: %s\0(st_frames)]",
+                                    fields => [qw(time server lag)] },
                            },
         Buffer_Size     => $config->{BufferSize},
         History_Size    => $config->{HistSize},
         Status_Height   => 1,
-        Title           => "Haver"
+        Title           => "Haver",
     );
     
-    $kernel->post( user_interface => send_me_input => "user_input" );   
+    $kernel->post( user_interface => send_me_input => "user_input" );
+    $kernel->yield('update_time');
 
-    $heap->{vt}->set_status_field($heap->{window_id}, server => "[none]");
+    $vt->set_status_field($window_id, server => "none");
 #   $kernel->alarm(update_time => int(time() / 60) * 60 + 60);
 }
 
@@ -100,8 +113,8 @@
 sub handle_shutdown {
     my ($kernel, $heap) = @_[KERNEL, HEAP];
     $config_handle->save;
-    $heap->{vt}->delete_window($heap->{window_id});
-    $heap->{vt}->shutdown;
+    $vt->delete_window($window_id);
+    $vt->shutdown;
     exit;
 }
 
@@ -123,7 +136,7 @@
             my $cmd = shift @input;
             &{$ucommands{$cmd}}(@input);
         } else {
-            $heap->{vt}->print($heap->{window_id}, "Unknown command: 
$input[0]");
+            vt_print("Unknown command: $input[0]");
         }
     } else {
         chan_msg('lobby', $input);
@@ -135,7 +148,7 @@
 sub handle_socket_connected {
     my ($heap, $socket) = @_[HEAP, ARG0];
     
-    $heap->{vt}->print($heap->{window_id}, "Connected.");
+    vt_print("Connected.");
     delete $heap->{socketfactory};
     $heap->{socket} = new POE::Wheel::ReadWrite(
         Handle  => $socket,
@@ -144,7 +157,7 @@
         Filter  => new Haver::Protocol::Filter,
     );
 
-    $heap->{vt}->set_status_field($heap->{window_id},
+    $vt->set_status_field($window_id,
         server => $config->{Server});
 
     $heap->{socket}->put(['HAVER', 'Term::Visual/0.01']);
@@ -153,17 +166,16 @@
 sub handle_socket_error {
     my ($heap, $syscall, $errno, $error) = @_[HEAP, ARG0 .. ARG2];
     $error = "Normal disconnection" unless $errno;
-    vt_print($heap->{window_id}, "Encountered $syscall error $errno: $error");
+    vt_print("Encountered $syscall error $errno: $error");
 }
 
 sub handle_socket_input {
     my ($heap, $input) = @_[HEAP, ARG0];
-#    $heap->{vt}->print($heap->{window_id}, 'Received '.join(' ', @$input));
     if (exists $scommands{$$input[0]}) {
         my $cmd = shift @$input;
         &{$scommands{$cmd}}(@$input);
     } else {
-        $heap->{vt}->print($heap->{window_id}, "Can't handle $$input[0]");
+        vt_print("Can't handle $$input[0]");
     }
 }
 
@@ -182,11 +194,12 @@
     QUIT    => \&server_quit,
     MSG     => \&server_msg,
     PMSG    => \&server_pmsg,
+    OUCH    => \&server_ouch,
 );
 
 sub server_haver {
     my $heap = $poe_kernel->get_active_session()->get_heap();
-    $heap->{vt}->print($heap->{window_id}, "Server version: ".shift);
+    vt_print("Server version: ".shift);
 }
 
 sub server_want {
@@ -203,6 +216,7 @@
 sub server_accept {
     my ($uid) = $_[0];
     vt_print("UID accepted: $uid");
+    poke_server();
 }
 
 sub server_fail {
@@ -252,24 +266,43 @@
                  "\0(ncolor)$msg");
     } elsif ($type eq ':') { # Action
         vt_print("\0(channel)$cid\0(ncolor):* \0(nick)$user\0(ncolor) $msg");
-    } elsif ($type eq 'PING?') {
-
     } else {
         vt_print("Received odd message type '$type'");
     }
 }
 
 sub server_pmsg {
-    my ($user, $msg) = @_;
+    my ($user, $type, $msg) = @_;
     $msg = '' if (!defined $msg);
-    vt_print("<$user> $msg");
+    if ($type eq '"') {
+        vt_print("<$user> $msg");
+    } elsif ($type eq ':') {
+        vt_print("* $user $msg");
+    } elsif ($type eq 'PING?') {
+        vt_print("$user requested PING");
+        get_heap()->{socket}->put(['PMSG', $user, 'PING', $msg]);
+    } elsif ($type eq 'PING') {
+        vt_print("PING reply from $user: ".sprintf("%.3f",
+            Time::HiRes::time - $msg));
+    } else {
+        vt_print("Received odd message type: '$type'");
+    }
 }
 
+sub server_ouch {
+    my ($servertime, $data) = @_;
+    $vt->set_status_field($window_id, lag => sprintf "%.3f",
+        Time::HiRes::time - $data);
+}
+
 ###########################################################
 #                      USER COMMANDS                      #
 ###########################################################
 
 %ucommands = (
+    raw     => \&command_raw,
+    poke    => \&command_poke,
+    ping    => \&command_ping,
     quit    => \&command_quit,
     connect => \&command_connect,
     save    => \&command_save,
@@ -280,11 +313,31 @@
     say     => \&command_say,
     msg     => \&command_msg,
     palette => \&command_palette,
+    set     => \&command_set,
 );
 
+sub command_raw {
+    my $command = join(" ", @_);
+    $command =~ s/\\t/\t/g;
+    vt_print("Sent: ".$command);
+    get_heap()->{socket}->put([split "\t", $command]);
+}
+
+sub command_poke {
+    poke_server();
+    vt_print("Server poked.");
+}
+
+sub command_ping {
+    my ($user) = $_[0];
+    if (!defined $user) {
+        vt_print("Usage: /ping <user>");
+    }
+    vt_print("Requested PING? from $user");
+    get_heap()->{socket}->put(['PMSG', $user, 'PING?', Time::HiRes::time]);
+}
+
 sub command_quit {
-#   my $heap = $poe_kernel->get_active_session()->get_heap();
-#   $heap->{vt}->print($heap->{window_id}, "Shutting down...");
     $poe_kernel->yield("shutdown");
     return;
 }
@@ -303,8 +356,7 @@
         $config->{Port}   = $port;
     }
     
-    $heap->{vt}->print($heap->{window_id},
-        "Connecting to $config->{Server}:$config->{Port}...");
+    vt_print("Connecting to $config->{Server}:$config->{Port}...");
 
     $heap->{socketfactory} = POE::Wheel::SocketFactory->new(
         RemoteAddress   => $config->{Server},
@@ -317,7 +369,7 @@
 sub command_save {
     my $heap = $poe_kernel->get_active_session()->get_heap();
     $config_handle->save;
-    $heap->{vt}->print($heap->{window_id}, "Configuration saved.");
+    vt_print("Configuration saved.");
 }
 
 sub command_join {
@@ -362,24 +414,32 @@
         return;
     }
     $msg = '' if (!defined $msg);
-    $heap->{socket}->put(['PMSG', $user, $msg]);
+    $heap->{socket}->put(['PMSG', $user, '"', $msg]);
 }
 
 sub command_palette {
-    my ($heap, $name, $color) = (get_heap(), shift, join(" ", @_));
+    my ($name, $color) = (shift, join(" ", @_));
     $palette{$name} = $color;
-    $heap->{vt}->set_palette(%palette);
-    Term::Visual::_refresh_title($heap->{vt}, $heap->{window_id});
-    Term::Visual::_refresh_status($heap->{vt}, $heap->{window_id});
-    Term::Visual::_refresh_buffer($heap->{vt}, $heap->{window_id});
-    Term::Visual::_refresh_edit($heap->{vt}, $heap->{window_id});
+    $vt->set_palette(%palette);
+    Term::Visual::_refresh_title($vt, $window_id);
+    Term::Visual::_refresh_status($vt, $window_id);
+    Term::Visual::_refresh_buffer($vt, $window_id);
+    Term::Visual::_refresh_edit($vt, $window_id);
     Term::Visual::doupdate();
 }
 
+sub command_set {
+    my ($key, $val) = (shift, join(" ", @_));
+    $config->{$key} = $val;
+    vt_print("$key set to '$val'");
+}
+
 # Misc functions
 
 sub vt_print {
-    get_heap()->{vt}->print(get_heap()->{window_id}, @_);
+    my $time = strftime $config->{TimeFormat}, localtime;
+    @_ = map {$time." ".$_} @_;
+    $vt->print($window_id, @_);
 }
 
 sub chan_msg {
@@ -388,6 +448,18 @@
     $heap->{socket}->put(['MSG', $chan, '"', $msg]);
 }
 
+sub poke_server {
+    get_heap()->{socket}->put(['POKE', Time::HiRes::time]);
+    $vt->set_status_field($window_id, lag => '(??)');
+    $poe_kernel->delay(poke_server => 60);
+}
+
+sub update_time {
+    $vt->set_status_field($window_id, time => strftime($config->{TimeFormat},
+                            localtime));
+    $poe_kernel->alarm(update_time => time() + 1.4);
+}
+
 sub get_heap {
     return $poe_kernel->get_active_session()->get_heap();
 }


Reply via email to