Clacks Server example.sock 600 60 clackspersistance.dat exampleuser unsafepassword rwuser foo 1 1 0 rouser bar 1 0 0 100 5 #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use English qw(-no_match_vars); use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 29; use autodie qw( close ); use Array::Contains; use utf8; #---AUTOPRAGMAEND--- my $isDebugging = 0; BEGIN { if(defined($ARGV[1]) && $ARGV[1] eq "--debug") { print("Development INC activated\n\n"); unshift @INC, "../lib"; # Point to your local copy of Net-Clacks/lib if you want to play around with the server code without "make install" every time you change something $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->run; #### $ perl server.pl server.xml --debug Development INC activated PC_CONFIG_PATHS undefined, falling back to legacy mode Loading config file server.xml ------- Parsing config file server.xml ------ Removing old unix domain socket file example.sock Listening on Unix domain socket example.sock Sorry, no valid persistance file found. Starting server 'blankety-blank' Ready. Saving persistance file #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use English qw(-no_match_vars); use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 29; use autodie qw( close ); use Array::Contains; use utf8; use Encode qw(is_utf8 encode_utf8 decode_utf8); use Data::Dumper; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Time::HiRes qw(sleep); my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'debugmonitor'; my $keepRunning = 1; $SIG{TERM} = sub{ print "SIGTERM, exiting program\n"; $keepRunning = 0; }; # Client side caching is a whole other topic. Basically, it doesn't resend stuff when the value hasn't changed # In most cases this isn't required or can even be counter-productive. This is one of those advanced topics... my $is_caching = 0; # Let's connect my $debug = Net::Clacks::Client->newSocket('example.sock', $username, $password, $applicationname, $is_caching) or croak("Connection to server failed"); # If you want to use TCP, you can do this instead: #my $debug = Net::Clacks::Client->new('127.0.0.1', 49888, $username, $password, $applicationname, $is_caching); $debug->setMonitormode(1); # NSA mode activate ;-) $debug->disablePing(); # Disable ping timeouts $debug->doNetwork(); # Send buffered commands to server while($keepRunning) { $debug->doNetwork(); # Sync with server # Iterate over all messages in our in-buffer while((my $msg = $debug->getNext())) { if($msg->{type} eq 'debug') { print $msg->{host}, ": ", $msg->{command}, "\n"; } elsif($msg->{type} eq 'serverinfo') { print '+++ Connected to server version: ', $msg->{data}, "\n"; } elsif($msg->{type} eq 'disconnect') { print '+++ Disconnected by server, reason given: ', $msg->{data}, "\n"; last; } } sleep(0.2); } $debug->disconnect(); exit(0); #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use English qw(-no_match_vars); use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 29; use autodie qw( close ); use Array::Contains; use utf8; use Encode qw(is_utf8 encode_utf8 decode_utf8); use Data::Dumper; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Time::HiRes qw(sleep); my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'debugmonitor'; my $keepRunning = 1; $SIG{TERM} = sub{ print "SIGTERM, exiting program\n"; $keepRunning = 0; }; my $cycletime = 5; my $nextping = time + 10; my $nextrun = 0; # Client side caching is a whole other topic. Basically, it doesn't resend stuff when the value hasn't changed # In most cases this isn't required or can even be counter-productive. This is one of those advanced topics... my $is_caching = 0; # Let's connect my $clacks = Net::Clacks::Client->newSocket('example.sock', $username, $password, $applicationname, $is_caching) or croak("Connection to server failed"); initClacks(); while($keepRunning) { $clacks->doNetwork(); # Sync with server if($nextping < time) { $clacks->ping(); # Send a keepalive ping $nextping = time + 10; } # Iterate over all messages in our in-buffer while((my $msg = $clacks->getNext())) { if($msg->{type} eq 'disconnect') { print '+++ Disconnected by server, reason given: ', $msg->{data}, "\n"; sleep(2); # Wait a couple of seconds, should automatically reconnect on next doNetwork initClacks(); # Need to resend listen() and stuff } elsif($msg->{type} eq 'serverinfo') { print '+++ Connected to server version: ', $msg->{data}, "\n"; } elsif($msg->{type} eq 'set' && $msg->{name} eq 'FAKESENSOR::cycletime') { $cycletime = 0 + $msg->{data}; print "Cycle time has changed to $cycletime seconds\n"; } } if($nextrun < time) { # Do a cycle $nextrun = time + $cycletime; my $newval = int(rand(1000)); print "New sensor value: $newval\n"; $clacks->setAndStore('FAKESENSOR::value', $newval); # broadcast new value and also store it $clacks->increment('FAKESENSOR::count', 1); # Increment the sensor reading count by 1 (stepsize is an optional argument). Autovivifies the value if not defined $clacks->doNetwork(); } sleep(0.2); } $clacks->disconnect(); exit(0); sub initClacks() { # We use the key/value store for long-term storage... my $ct = $clacks->retrieve('FAKESENSOR::cycletime'); if(!defined($ct)) { # Fallback to default of 5 seconds print "Storing default cycletime in clacks\n"; $clacks->store('FAKESENSOR::cycletime', $cycletime); } else { $cycletime = 0 + $ct; print "Got a preset cycletime of $cycletime seconds\n"; } # ...but LISTEN to real-time changes. This implies the client changing this value will use setAndStore() $clacks->listen('FAKESENSOR::cycletime'); $clacks->doNetwork(); return; } #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use English qw(-no_match_vars); use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 29; use autodie qw( close ); use Array::Contains; use utf8; use Encode qw(is_utf8 encode_utf8 decode_utf8); use Data::Dumper; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Time::HiRes qw(sleep); my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'debugmonitor'; my $keepRunning = 1; $SIG{TERM} = sub{ print "SIGTERM, exiting program\n"; $keepRunning = 0; }; my $nextping = time + 10; my $nextrun = 0; # Client side caching is a whole other topic. Basically, it doesn't resend stuff when the value hasn't changed # In most cases this isn't required or can even be counter-productive. This is one of those advanced topics... my $is_caching = 0; # Let's connect my $clacks = Net::Clacks::Client->newSocket('example.sock', $username, $password, $applicationname, $is_caching) or croak("Connection to server failed"); initClacks(); while($keepRunning) { $clacks->doNetwork(); # Sync with server if($nextping < time) { $clacks->ping(); # Send a keepalive ping $nextping = time + 10; } # Iterate over all messages in our in-buffer while((my $msg = $clacks->getNext())) { if($msg->{type} eq 'disconnect') { print '+++ Disconnected by server, reason given: ', $msg->{data}, "\n"; sleep(2); # Wait a couple of seconds, should automatically reconnect on next doNetwork initClacks(); # Need to resend listen() and stuff } elsif($msg->{type} eq 'serverinfo') { print '+++ Connected to server version: ', $msg->{data}, "\n"; } elsif($msg->{type} eq 'set' && $msg->{name} eq 'FAKESENSOR::value') { print "Fake sensor reports a value of: ", $msg->{data}, "\n"; } } sleep(0.2); } $clacks->disconnect(); exit(0); sub initClacks() { # LISTEN to real-time changes. This implies the client changing this value will use setAndStore() $clacks->listen('FAKESENSOR::value'); $clacks->doNetwork(); return; } #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use English qw(-no_match_vars); use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 29; use autodie qw( close ); use Array::Contains; use utf8; use Encode qw(is_utf8 encode_utf8 decode_utf8); use Data::Dumper; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Time::HiRes qw(sleep); my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'debugmonitor'; my $keepRunning = 1; $SIG{TERM} = sub{ print "SIGTERM, exiting program\n"; $keepRunning = 0; }; # Client side caching is a whole other topic. Basically, it doesn't resend stuff when the value hasn't changed # In most cases this isn't required or can even be counter-productive. This is one of those advanced topics... my $is_caching = 0; # Let's connect my $clacks = Net::Clacks::Client->newSocket('example.sock', $username, $password, $applicationname, $is_caching) or croak("Connection to server failed"); $clacks->doNetwork(); # Sync with server # RETRIEVE stored value from clacks server my $val = $clacks->retrieve('FAKESENSOR::value'); if(!defined($val)) { print "No known sensor data\n"; } else { print "Last sensor value: $val\n"; } $clacks->disconnect(); #### #!/usr/bin/env perl #---AUTOPRAGMASTART--- use v5.36; use strict; use diagnostics; use English qw(-no_match_vars); use Carp qw[carp croak confess cluck longmess shortmess]; our $VERSION = 29; use autodie qw( close ); use Array::Contains; use utf8; use Encode qw(is_utf8 encode_utf8 decode_utf8); use Data::Dumper; #---AUTOPRAGMAEND--- use Net::Clacks::Client; use Time::HiRes qw(sleep); my $username = 'exampleuser'; my $password = 'unsafepassword'; my $applicationname = 'debugmonitor'; my $keepRunning = 1; $SIG{TERM} = sub{ print "SIGTERM, exiting program\n"; $keepRunning = 0; }; my $cycletime = shift @ARGV; if(!defined($cycletime)) { print "Usage: perl cycletime.pl CYCLEINSECONDS\n"; exit(0); } $cycletime = 0 + $cycletime; # Make sure its a number if(!$cycletime || $cycletime < 0) { print "Cycletime must be a positive numer and can not be zero\n"; exit(0); } # Client side caching is a whole other topic. Basically, it doesn't resend stuff when the value hasn't changed # In most cases this isn't required or can even be counter-productive. This is one of those advanced topics... my $is_caching = 0; # Let's connect my $clacks = Net::Clacks::Client->newSocket('example.sock', $username, $password, $applicationname, $is_caching) or croak("Connection to server failed"); # SETANDSTORE new cycletime. This sends a real-time message AND stores the value in the key/vallue store for later retrieval $clacks->setAndStore('FAKESENSOR::cycletime', $cycletime); $clacks->doNetwork(); # Sync with server $clacks->disconnect();