The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

FUSE::Server - Perl-FUSE server

SYNOPSIS

  use FUSE::Server;
  my $s = FUSE::Server->new({
      Port=>35008,
      MaxClients=>5000,
      Quiet=>1,
  });

  my $status = $s->bind();
  print "Server started: $status";

  $s->addCallback('BROADCASTALL',\&msg_broadcast);

  $s->addCallback('client_start',\&msg_client_start);

  $s->defaultCallback(\&unknown_command);

  $SIG{INT} = $SIG{TERM} = $SIG{HUP} = sub{$s->stop();};

  $s->start();

  sub msg_broadcast{
      my ($userid,$msg,$params) = @_;
      my @a = split /\//,$params;
      $s->sendAll($a[1],$a[2]);
  }

  sub msg_client_start{
      my ($userid,$msg,$params) = @_;
      $s->send($userid,'SECRET_KEY','123 456 789');
  }

  sub unknown_command{
      my ($userid,$msg,$params) = @_;
      print "Unknown command $msg\n";
  }

DESCRIPTION

The FUSE::Server module will create a TCP FUSE server and dispatch messages to registered event handlers.

The external interface to FUSE::Server is:

$s = FUSE::Server->new( [%options] );

The object constructor takes the following arguments in the options hash:

Quiet = 0|1

Whether to be quiet. Default is to report all events to STDOUT (not 'Quiet').

Port = n

The port for the server to listen on. Default is 1024.

MaxClients = n

Maximum incoming connections to allow. Default is SOMAXCONN.

$s->bind();

This method starts the server listening on it's port and returns the IP which it is listening on.

$s->addCallback( $message, $coderef );

This method registers the referenced subroutine as a handler for the specified message. When the server receives that message from the client, it checks it's handler hash and dispatches the decoded message to the sub. The sub should handle the following arguments:

( $userid, $msg, $params )

$userid contains the internal connection id for the client session. You can use this id to associate logins with clients. The $msg parameter contains the message the client sent. This allows one routine to handle more than one message. Messages from clients are typically uppercase, with lowercase messages being reserved for internal server events, such as client connect/disconnect. The available internal messages are:

client_start

This message is sent when a client first connects. It is typically used to issue a SECRET_KEY message to the client.

client_stop

This message is sent when a client disconnects.

$s->defaultCallback( $coderef );

For all messages without an assigned handler, the default handler (if set) is sent the message. If you'd like to handle all messages internally, then setup defaultCallback without setting up any normal addCallback's.

$s->stop();

This method shuts down the server gracefully. Since the start method loops forever, the stop method is generally set up to run on a signal.

$s->start();

This method invokes the server's internal message pump. This loop can only be broken by a signal.

$s->send( $userid, $message, $params );

This method sends a message to a single client.

$s->sendAll( $message, $params );

This method broadcasts a message to all clients.

AUTHOR

Cal Henderson, <cal@iamcal.com>