#!/usr/bin/env perl #---AUTOPRAGMASTART--- use 5.020; use strict; use warnings; use diagnostics; use mro 'c3'; use English; use Carp; our $VERSION = 4.1; use Fatal qw( close ); use Array::Contains; #---AUTOPRAGMAEND--- my $isDebugging = 0; if(defined($ARGV[1]) && $ARGV[1] eq "--debug") { $isDebugging = 1; } use Net::Clacks::Server; my $configfile = shift @ARGV; croak("No Config file parameter") if(!defined($configfile) || $configfile eq ''); my $worker = Net::Clacks::Server->new($isDebugging, $configfile); $worker->init; $worker->run; #### Clacks Master 127.0.0.1 18888 600 10 exampleserver.crt exampleserver.key exampleuser unsafepassword 5000 1 128.0.0.1 ::1 #### openssl req -new -newkey rsa:4096 -x509 -sha256 -days 36500 -nodes -out exampleserver.crt -keyout exampleserver.key #### perl server.pl clacks_master.xml #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use 5.020; use strict; use warnings; use diagnostics; use mro 'c3'; use English; use Carp; our $VERSION = 4.1; use Fatal qw( close ); use Array::Contains; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Term::ReadKey; use Time::HiRes qw(sleep); use Data::Dumper; my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'clock'; my $is_caching = 0; my $chat = Net::Clacks::Client->new('127.0.0.1', 18888, $username, $password, $applicationname, $is_caching); my $clockname = 'example::notify'; my $last = ''; while(1) { my $now = getCurrentMinute(); if($now ne $last) { $chat->notify($clockname); $chat->ping(); $last = $now; } $chat->doNetwork(); while((my $msg = $chat->getNext())) { if($msg->{type} eq 'disconnect') { print '+++ Disconnected by server, reason given: ', $msg->{data}, "\n"; last; } } sleep(0.2); } sub getCurrentMinute { my ($sec,$min, $hour, $mday,$mon, $year, $wday,$yday, $isdst) = localtime time; $year += 1900; $mon += 1; $mon = doFPad($mon, 2); $mday = doFPad($mday, 2); $hour = doFPad($hour, 2); $min = doFPad($min, 2); return "$year-$mon-$mday $hour:$min"; } sub doFPad { my ($val, $len) = @_; while(length($val) < $len) { $val = '0' . $val; } return $val; } #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use 5.020; use strict; use warnings; use diagnostics; use mro 'c3'; use English; use Carp; our $VERSION = 4.1; use Fatal qw( close ); use Array::Contains; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Term::ReadKey; use Time::HiRes qw(sleep); use Data::Dumper; my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'chatclient'; my $is_caching = 0; my $chat = Net::Clacks::Client->new('127.0.0.1', 18888, $username, $password, $applicationname, $is_caching); #print 'Connected to server. Info given: ', $chat->getServerinfo(), "\n"; my $chatname = 'example::chat'; my $clockname = 'example::notify'; $chat->listen($chatname); $chat->listen($clockname); $chat->ping(); $chat->doNetwork(); my $nextping = time + 60; while(1) { my $line = ReadLine -1; if(defined($line)) { chomp $line; if(length($line)) { last if(uc $line eq 'QUIT' || uc $line eq 'EXIT'); $chat->set($chatname, $line); } } if($nextping < time) { $chat->ping(); $nextping = time + 60; } $chat->doNetwork(); while((my $msg = $chat->getNext())) { if($msg->{type} eq 'set' && $msg->{name} eq $chatname) { print '>>> ', $msg->{data}, "\n"; } elsif($msg->{type} eq 'notify' && $msg->{name} eq $clockname) { print "*** Another minute has passed ***\n"; } elsif($msg->{type} eq 'disconnect') { print '+++ Disconnected by server, reason given: ', $msg->{data}, "\n"; last; } } sleep(0.2); } #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use 5.020; use strict; use warnings; use diagnostics; use mro 'c3'; use English; use Carp; our $VERSION = 4.1; use Fatal qw( close ); use Array::Contains; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Term::ReadKey; use Time::HiRes qw(sleep); use Data::Dumper; my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'chatbot'; my $is_caching = 0; my $chat = Net::Clacks::Client->new('127.0.0.1', 18888, $username, $password, $applicationname, $is_caching); #print 'Connected to server. Info given: ', $chat->getServerinfo(), "\n"; my $chatname = 'example::chat'; my $clockname = 'example::notify'; my $countname = 'chatbot::linecount'; $chat->listen($chatname); $chat->listen($clockname); $chat->ping(); $chat->doNetwork(); my $nextping = time + 60; while(1) { my $line = ReadLine -1; if(defined($line)) { chomp $line; if(length($line)) { last if(uc $line eq 'QUIT' || uc $line eq 'EXIT'); $chat->set($chatname, $line); } } if($nextping < time) { $chat->ping(); $nextping = time + 60; } $chat->doNetwork(); while((my $msg = $chat->getNext())) { if($msg->{type} eq 'set' && $msg->{name} eq $chatname) { # Increment count for every chat message $chat->increment($countname); # very non-AI answer implementation of "simon says" if($msg->{data} =~ /simon/i) { $chat->set($chatname, 'RoboSimon says: ' . $msg->{data}); $chat->increment($countname); } } elsif($msg->{type} eq 'notify' && $msg->{name} eq $clockname) { # Every minute, retrieve our current count, send a chat message and decrease line count accordingly my $linecount = $chat->retrieve($countname); if(!defined($linecount)) { $linecount = 0; $chat->store($countname, 0); } $chat->set($chatname, $linecount . ' new chat messages during the last minute'); $chat->decrement($countname, $linecount); } elsif($msg->{type} eq 'disconnect') { print '+++ Disconnected by server, reason given: ', $msg->{data}, "\n"; last; } } $chat->doNetwork(); sleep(1); } #### Clacks Master 127.0.0.1 18889 600 10 exampleserver.crt exampleserver.key exampleuser unsafepassword 5000 1 127.0.0.1 18888 #### perl server.pl clacks_slaveserver.xml