Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Daemon with Net::Daemon

by bm (Hermit)
on Sep 02, 2003 at 10:31 UTC ( #288284=note: print w/replies, xml ) Need Help??


in reply to Daemon with Net::Daemon

Here is an explanation of Net::Daemon.

Define a new class that inherits from Net::Daemon in daemon.pl

require Net::Daemon; package Contdmon; use vars qw(@ISA); @ISA = qw(Net::Daemon); # to inherit from Net::Daem
In your class's constructor, call the Net::Daemon constructor and then do any other initialisation you need.
sub new ($$;$) { my($class, $attr, $args) = @_; my($self) = $class->SUPER::new($attr, $args); # set up the logfile my $file = IO::File->new ( '/path', "a" ); $file->autoflush ( 1 ) ; $self->{'logfile'} = $file; $self; }
Now write your Run method. This method overrides the Run method in Net::Daemon. It is executed whenever a client connects to the daemon
sub Run ($) { my($self) = @_; my($line, $result, $sock); $sock = $self->{'socket'}; while (1) { if (!defined($line = $sock->getline())) { if ($sock->error()) { $self->Error("Client connection error %s", $sock->error()); } $sock->close(); return; } } print "Server received request: $line\n"; }
Now go ahead and create your server
package main; my $args = { 'facility' => 'daemon', 'pidfile' => '/users/ccm_root/logs/contdaem.pid', 'logfile' => '/users/ccm_root/logs/contdaem.log', 'user' => 'ccm_root', 'group' => 'ccm_root', 'localport' => '5440', 'mode' => 'single', }; my $server = Contdmon->new ({}, $args); $server->Bind();
So at this point you have a daemon sitting there waiting for a request. Run this script on server A.
Now you need a client to submit a request from server B. In client.pl:
use strict; use IO::Socket; $| = 1; # flush output buffers immediately my ( $host ) = 'server'; my ( $port ) = 1234; # create the socket my $sock = IO::Socket::INET->new ( Proto => 'tcp', Type => SOCK_STREAM, PeerAddr => $host, PeerPort => $port ) or die "Cannot create socket : $!"; # so output goes to daemon immediately $sock->autoflush (1); print $sock "What is the time, Mr Wolf?\n";
Run client.pl on your client machine, webserver(B), and it will send a request via the TCP protocol to your daemon waiting on server A.
Note that there are some security concerns with this. Don't let your daemon accept just any list of commands to run on server A. Perhaps sends 'RunA' from client.pl, and your daemon will then go ahead and execute a hardcoded set of commands before sending back the response.
Hope this helps
--
bm

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://288284]
help
Chatterbox?
[ambrus]: (Alternately a deep source dive and then rewrite that event loop to make it better, and then as a bonus you get an idle method.)
[ambrus]: The 0.2 seconds wakeup is likely a workaround for some bug, but I can't guess what bug that is.
[ambrus]: It's been there since Prima 1.00 iirc
[Corion]: Hmmm... Weird. Maybe it needs that for doing its timers or something. Still weird.
[Corion]: But I guess I'll find out soonish, as my application seems to use Prima for more than just displaying the OpenGL output soonish ;))
[ambrus]: No, I think the timers are handled fine in the event loop, as far as I can see.
[Corion]: So far, I liked Prima quite well as it simply seems to do what I want even if it has an upside-down coordinate system. But it installs from CPAN out of the box and is cross-platform

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2016-12-09 10:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.