| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # Copyright (c) 2010 AdCopy | ||
| 4 | # Author: Jeff Weisberg | ||
| 5 | # Created: 2010-Jan-18 13:20 (EST) | ||
| 6 | # Function: send messages to end-user console | ||
| 7 | # | ||
| 8 | # $Id: EUConsole.pm,v 1.1 2010/11/01 18:41:41 jaw Exp $ | ||
| 9 | |||
| 10 | package AC::MrGamoo::EUConsole; | ||
| 11 | use AC::MrGamoo::Debug 'euconsole'; | ||
| 12 | use AC::MrGamoo::MySelf; | ||
| 13 | use AC::Daemon; | ||
| 14 | use AC::MrGamoo::Protocol; | ||
| 15 | use Socket; | ||
| 16 | use strict; | ||
| 17 | |||
| 18 | |||
| 19 | sub new { | ||
| 20 | my $class = shift; | ||
| 21 | my $jobid = shift; | ||
| 22 | my $console = shift; # ipaddr:port | ||
| 23 | |||
| 24 | return bless {}, $class unless $console; | ||
| 25 | |||
| 26 | # open socket to end-user console session, if requested | ||
| 27 | my($addr, $port) = split /:/, $console; | ||
| 28 | my $ip = inet_aton($addr); | ||
| 29 | |||
| 30 | my $fd; | ||
| 31 | my $i = socket($fd, PF_INET, SOCK_DGRAM, 0); | ||
| 32 | $i = connect( $fd, sockaddr_in($port, $ip) ); | ||
| 33 | unless( $i ){ | ||
| 34 | verbose("cannot connect to user console: $!"); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | return bless { | ||
| 39 | fd => $fd, | ||
| 40 | jobid => $jobid, | ||
| 41 | msgid => (time() & 0xFFFF), | ||
| 42 | }, $class; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub send_msg { | ||
| 46 | my $me = shift; | ||
| 47 | my $type = shift; | ||
| 48 | my $msg = shift; | ||
| 49 | |||
| 50 | my $fd = $me->{fd}; | ||
| 51 | return unless $fd; | ||
| 52 | |||
| 53 | my $req = AC::MrGamoo::Protocol->encode_request( { | ||
| 54 | type => 'mrgamoo_diagmsg', | ||
| 55 | want_reply => 0, | ||
| 56 | msgid => $me->{msgid}++, | ||
| 57 | }, { | ||
| 58 | jobid => $me->{jobid}, | ||
| 59 | server_id => my_server_id(), | ||
| 60 | type => $type, | ||
| 61 | msg => $msg, | ||
| 62 | } ); | ||
| 63 | |||
| 64 | send $fd, $req, 0; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; |