-
Notifications
You must be signed in to change notification settings - Fork 131
Items Socket_Item
ggodart edited this page Jan 6, 2021
·
4 revisions
See original
# An example of a server socket
# Add this mh.ini parm: server_speak_port = 8090
$socket_server = new Socket_Item(undef, undef, 'server_speak');
if (my $data = said $socket_server) {
print "speak_server socket data: $data\n";
speak $data;
}
# An example of a client socket
my $lcdproc_address = 'misterhouse:13666';
$lcdproc = new Socket_Item(undef, undef, $lcdproc_address, 'lcdproc');
$Vcmd_viavoice = new main::Socket_Item(undef, undef, 'localhost:3234', 'viavoice');
# This example walks the reboot menu of a router
$router_reboot = new Voice_Cmd 'Reboot the router';
$router_client = new Socket_Item(undef, undef, $config_parms{router_address} . ":23", 'router', 'tcp', 'raw');
set_expect $router_client (Password => $config_parms{router_password}, Number => 24, Number => 4, Number => 11) if said $router_reboot;
# Example of sending a message to all clients
set $telnet_server "Hi to all clients", 'all';
Read/Write to TCP/IP socket ports.
Server socket ports are specified with mh.ini parms (see 'server options' in mh.ini)
Method | Description |
---|---|
new('data_stream', 'state_name', 'host_port', 'name', 'host_protocol', 'datatype', 'break_char') |
|
set_port |
Allows you to change the server_name/port. |
add('data_stream', 'state_name') |
'host_port' - must be of the form ip_address:port (when creating a client) or it must match a server_* parameter in mh.ini (when creating a server) (e.g. server_telnet_port ).'host_protocol' - can be 'tcp' or 'udp'. Default is 'tcp' 'datatype' - can be 'raw', 'rawout', or 'record'. If 'raw', the said method returns data as read. If record, said only returns data when reaching a newline. When writing to the port, a newline is added unless datatype is 'raw' or 'rawout'. 'break_char' - This will be added to data sent with set and, unless datatype=raw, will be used to break incoming data into state_now records. Default is \n\r .'name' - is optional. If used you can set debug to this string to turn on debug for just this socket data. |
start |
Connect to the specified port. This allows mh to act as a client, rather than a server, and initiate communications with a server. |
stop |
This will drop the client or server from mh and free up the port. |
is_available |
Returns 1 if the socket is available, 0 if not |
active |
True if the port is active. |
active_now |
True for the MisterHouse pass that the socket first becomes active. |
inactive_now |
True for the MisterHouse pass that the socket first becomes inactive. |
` | said |
said_next |
Reads and returns the next record from the socket handle |
handle |
Returns the socket handle, so you can loop on reading/writing data to it directly. |
set_echo |
Use this to control echoing of incoming characters: - set_echo $my_socket 0 -> Do not echo incoming characters - set_echo $my_socket 1 -> echo incoming characters - set_echo $my_socket '*' -> echo incoming characters with *
|
set |
Sets the item to the specified state. - If the specified state has not been defined with 'new' or 'add', the state data is sent. Otherwise the data_stream associated with that state is sent. - If there is more than one client connected to this port, an optional 2nd parm can be used to pick which client set to. Values are: - 'all' (send to all connected clients), - ip_address (a regular expresion of the ip address(s) you want to send to - client_number (which client to send to, 0 .. #). Note this can change as clients are added and dropped. - socket hash name, which can be captured when reading data from the port with this: $client = $Socket_Ports{$port_name}{socka};
|
set_expect |
Used to send a series of strings to the port, waiting for a specified prompt for each string. This is useful for walking through menus. The arguments passed to set_expect are pairs of 'prompt' => 'response' strings . See example below. |
Example;
sub set_expect {
my ($self, @set_expect_cmds) = @_;
if (active $self) {
&main::print_log("set_expect: $$self{port_name} is already active");
}
else {
&main::print_log("set_expect: $$self{port_name} start");
$self->start;
@{$$self{set_expect_cmds}} = @set_expect_cmds;
$$self{set_expect_timer} = new Timer;
$$self{set_expect_timer}-> set(10);
&::MainLoop_pre_add_hook( \&Socket_Item::set_expect_check, 0, $self );
}
}
Method | Description |
---|---|
state |
Returns the last state that was received or sent |
state_now |
Returns the state that was received or sent in the current pass. |
More examples are in mh/code/bruce/telnet.pl
, mh/code/examples/socket_test*.pl
, mh/code/bruce/monitor_shoutcast.pl
, bruce/wxserver_server.pl
, and bruce/monitor_router.pl