Skip to content

Commit

Permalink
Output connection debugging to STDERR; require debug level 2 for raw …
Browse files Browse the repository at this point in the history
…messages
  • Loading branch information
japhb committed Feb 23, 2024
1 parent 1fac5d0 commit dfc061a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
12 changes: 6 additions & 6 deletions lib/MUGS/Client/Connection/Supplier.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ class MUGS::Client::Connection::Supplier does MUGS::Client::Connection {
has $!debug = $*DEBUG // 0;

method connect-to-server(::?CLASS:D: :$server!) {
put "client connecting to '$server' ..." if $!debug;
note "client connecting to '$server' ..." if $!debug;
my $connection = MUGS::Server::Connection::Supplier.new(:client-conn(self));
$!server-conn = $server.accept-connection(:$connection)
or X::MUGS::Connection::Can'tConnect.new(:$server).throw;
put "client successfully connected to '$server'" if $!debug;
note "client successfully connected to '$server'" if $!debug;
}

method disconnect() {
put "client disconnecting ..." if $!debug;
note "client disconnecting ..." if $!debug;
.disconnect with $!server-conn;
$!server-conn = Nil;
$!to-client.done;
put "client disconnected" if $!debug;
note "client disconnected" if $!debug;
}

method send-to-client(Blob:D $cbor) {
my $struct = cbor-decode $cbor;
put "client --> CLIENT:\n{$struct.raku.indent(4)}\n" if $!debug;
note "client --> CLIENT:\n{$struct.raku.indent(4)}\n" if $!debug >= 2;

$!to-client.emit:
$struct<request-id> ?? MUGS::Message::Response.from-struct($struct) !!
Expand All @@ -41,7 +41,7 @@ class MUGS::Client::Connection::Supplier does MUGS::Client::Connection {
}

method send-to-server(MUGS::Message:D $message) {
put "client --> SERVER:\n{$message.to-debug}\n" if $!debug;
note "client --> SERVER:\n{$message.to-debug}\n" if $!debug >= 2;
$!server-conn.send-to-server($message.to-cbor);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/MUGS/Client/Connection/WebSocket.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ class MUGS::Client::Connection::WebSocket does MUGS::Client::Connection {
has $!debug = $*DEBUG // 0;

method connect-to-server(::?CLASS:D: :$server!, :%ca) {
put "client connecting to '$server' ..." if $!debug;
note "client connecting to '$server' ..." if $!debug;
$!cro-client = Cro::CBOR::WebSocket::Client.new(:uri($server), :cbor);
$!server-conn = await $!cro-client.connect(:%ca);
put "client successfully connected to '$server'" if $!debug;
note "client successfully connected to '$server'" if $!debug;
}

method disconnect() {
put "client disconnecting ..." if $!debug;
note "client disconnecting ..." if $!debug;
await .close with $!server-conn;
put "client disconnected" if $!debug;
note "client disconnected" if $!debug;
}

method send-to-server(MUGS::Message:D $message) {
my $struct = $message.to-struct;
put "client --> SERVER:\n{ to-json $struct, :sorted-keys }\n" if $!debug;
note "client --> SERVER:\n{ to-json $struct, :sorted-keys }\n" if $!debug >= 2;
$!server-conn.send: Cro::WebSocket::Message.new(:!fragmented,
:body($struct));
}

method from-server-supply() {
supply whenever $!server-conn.messages -> $message {
whenever $message.body -> $struct {
put "From server:\n{ to-json $struct, :sorted-keys }\n" if $!debug;
note "From server:\n{ to-json $struct, :sorted-keys }\n" if $!debug >= 2;
emit $struct<request-id> ?? MUGS::Message::Response.from-struct($struct) !!
$struct<pack> ?? MUGS::Message::PushPack.from-struct($struct) !!
MUGS::Message::Push.from-struct($struct)
Expand Down
8 changes: 4 additions & 4 deletions lib/MUGS/Server/Connection/Supplier.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ class MUGS::Server::Connection::Supplier does MUGS::Server::Connection {
has $!debug = $*DEBUG // 0;

method disconnect() {
put "server disconnecting ..." if $!debug;
note "server disconnecting ..." if $!debug;
# .disconnect with $!client-conn;
$!client-conn = Nil;
$!to-server.done;
put "server disconnected" if $!debug;
note "server disconnected" if $!debug;
}

method send-to-client(MUGS::Message:D $message) {
put "server --> CLIENT:\n{$message.to-debug}\n" if $!debug;
note "server --> CLIENT:\n{$message.to-debug}\n" if $!debug >= 2;
$!client-conn.send-to-client($message.to-cbor) if $!client-conn;
}

method send-to-server(Blob:D $cbor) {
my $struct = cbor-decode $cbor;
put "server --> SERVER:\n{$struct.raku.indent(4)}\n" if $!debug;
note "server --> SERVER:\n{$struct.raku.indent(4)}\n" if $!debug >= 2;

$!to-server.emit: $struct<game-id>
?? MUGS::Message::Request::InGame.from-struct($struct)
Expand Down
10 changes: 5 additions & 5 deletions lib/MUGS/Server/Connection/WebSocket.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ class MUGS::Server::Connection::WebSocket does MUGS::Server::Connection {
has $!debug = $*DEBUG // 0;

method disconnect() {
put "server disconnecting from '$.debug-name'..." if $!debug;
note "server disconnecting from '$.debug-name'..." if $!debug;
# .close with $!client-conn;
$!client-conn = Nil;
$!to-client.done;
put "server disconnected from '$.debug-name'" if $!debug;
note "server disconnected from '$.debug-name'" if $!debug;
}

method send-to-client(MUGS::Message:D $message) {
if $!debug {
if $!debug >= 2 {
my $t0 = now;
my $struct = $message.to-struct;

put "{$t0.DateTime}] server --> CLIENT ($.debug-name):\n{ to-json $struct, :sorted-keys }\n";
note "{$t0.DateTime}] server --> CLIENT ($.debug-name):\n{ to-json $struct, :sorted-keys }\n";
$!to-client.emit: Cro::WebSocket::Message.new(:!fragmented, :body($struct));
}
else {
Expand All @@ -40,7 +40,7 @@ class MUGS::Server::Connection::WebSocket does MUGS::Server::Connection {
method from-client-supply() {
supply whenever $!client-conn -> $message {
whenever $message.body -> $struct {
put "From client ($.debug-name):\n{ to-json $struct, :sorted-keys }\n" if $!debug;
note "From client ($.debug-name):\n{ to-json $struct, :sorted-keys }\n" if $!debug >= 2;
emit $struct<game-id>
?? MUGS::Message::Request::InGame.from-struct($struct)
!! MUGS::Message::Request.from-struct($struct);
Expand Down

0 comments on commit dfc061a

Please sign in to comment.