| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | # Define the MCP package object. Most of the code is inherited from GA::Generic::Mcp; all we need to | ||
| 4 | # do is to write a custom ->msg function. The ->msg function is called whenever a valid MCP | ||
| 5 | # message for this package is received from the world | ||
| 6 | |||
| 7 | { package Games::Axmud::Mcp::PingPong; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | # use diagnostics; | ||
| 12 | |||
| 13 | use Glib qw(TRUE FALSE); | ||
| 14 | |||
| 15 | our @ISA = qw(Games::Axmud::Generic::Mcp Games::Axmud::Generic::Atcp Games::Axmud); | ||
| 16 | |||
| 17 | ################## | ||
| 18 | # Constructors | ||
| 19 | |||
| 20 | ################## | ||
| 21 | # Methods | ||
| 22 | |||
| 23 | sub msg { | ||
| 24 | |||
| 25 | # Called by GA::Session->processMcpMsg whenever an MCP message for this package is received | ||
| 26 | # | ||
| 27 | # Expected arguments | ||
| 28 | # $session - The calling GA::Session | ||
| 29 | # $msg - The MCP message name, e.g. 'mcp-negotiate-can' | ||
| 30 | # | ||
| 31 | # Optional arguments | ||
| 32 | # %hash - A hash of key-value pairs which accompany the MCP message (may be an | ||
| 33 | # empty hash) | ||
| 34 | # | ||
| 35 | # Return values | ||
| 36 | # 'undef' on improper arguments or if there's an error processing the message | ||
| 37 | # 1 if the message is processed successfully | ||
| 38 | |||
| 39 | my ($self, $session, $msg, %hash) = @_; | ||
| 40 | |||
| 41 | # Local variables | ||
| 42 | my ($name, $text, $winObj); | ||
| 43 | |||
| 44 | # Check for improper arguments | ||
| 45 | if (! defined $session || ! defined $msg) { | ||
| 46 | |||
| 47 | return $axmud::CLIENT->writeImproper($self->_objClass . '->msg', @_); | ||
| 48 | } | ||
| 49 | |||
| 50 | # Check that the MCP message actually belongs to this package | ||
| 51 | $name = $self->name; | ||
| 52 | if (! ($msg =~ m/^$name\-/)) { | ||
| 53 | |||
| 54 | return undef; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Check that the message is recognised by this package | ||
| 58 | if ( | ||
| 59 | $msg ne 'dns-org-mud-moo-pingpong-ping' | ||
| 60 | && $msg ne 'dns-org-mud-moo-pingpong-pong' | ||
| 61 | ) { | ||
| 62 | return undef; | ||
| 63 | } | ||
| 64 | |||
| 65 | # # Check that the required key-value pairs are all present; ignore any unexpected keys | ||
| 66 | # if (! exists $hash{'foo'} || ! exists $hash{'bar'} || ! exists $hash{'baz'}) { | ||
| 67 | # | ||
| 68 | # return undef; | ||
| 69 | # } | ||
| 70 | |||
| 71 | # Respond to the message. If we received a ping, send a pong. If we received a pong, send a | ||
| 72 | # ping. (Obviously, this would not be a good idea in a real MCP package implemtation) | ||
| 73 | # By the way, if you want to send a multiline MCP message, call | ||
| 74 | # GA::Session->mcpSendMultiLine() instead | ||
| 75 | if ($msg eq 'dns-org-mud-moo-pingpong-ping') { | ||
| 76 | |||
| 77 | # Send an MCP message back to the server | ||
| 78 | $session->mcpSendMsg( | ||
| 79 | 'dns-org-mud-moo-pingpong-pong', | ||
| 80 | # # Optional key-value pairs | ||
| 81 | # 'key1', | ||
| 82 | # 'val1', | ||
| 83 | # 'key2', | ||
| 84 | # 'val2', | ||
| 85 | ); | ||
| 86 | |||
| 87 | } elsif ($msg eq 'dns-org-mud-moo-pingpong-pong') { | ||
| 88 | |||
| 89 | # Send an MCP message back to the server | ||
| 90 | $session->mcpSendMsg( | ||
| 91 | 'dns-org-mud-moo-pingpong-ping', | ||
| 92 | # # Optional key-value pairs | ||
| 93 | # 'key1', | ||
| 94 | # 'val1', | ||
| 95 | # 'key2', | ||
| 96 | # 'val2', | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | # Return a true value to show that the message was accepted | ||
| 101 | return 1; | ||
| 102 | } | ||
| 103 | |||
| 104 | ################## | ||
| 105 | # Accessors - set | ||
| 106 | |||
| 107 | ################## | ||
| 108 | # Accessors - get | ||
| 109 | } | ||
| 110 | |||
| 111 | # Package must return a true value | ||
| 112 | 1 |