Last active
December 12, 2015 04:28
-
-
Save boukeversteegh/4714567 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_once 'wopr.php'; | |
$settings = array( | |
'host' => 'localhost', | |
'port' => 1981, | |
); | |
$wopr = new WOPR($settings['host'], $settings['port']); | |
$wopr->connect(); | |
$wopr->getIndex(); // Get a new Index for WOPR. | |
$wopr->send('WRD', 'hallo'); | |
$result = $wopr->send('GEN'); | |
print_r($result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class WOPR_Exception extends Exception {}; | |
class WOPR_TimeoutException extends WOPR_Exception {}; | |
class WOPR_OverloadException extends WOPR_Exception {}; | |
class WOPR { | |
var $host = null; | |
var $port = null; | |
var $index = null; | |
var $socket = null; | |
function __construct($host, $port) { | |
$this->host = $host; | |
$this->port = $port; | |
} | |
function connect() { | |
if( $this->socket === null ) { | |
$hostip = gethostbyname($this->host); | |
$this->socket = fsockopen($hostip, $this->port, $errno, $errstr, 30); | |
stream_set_timeout($this->socket, 100); | |
} | |
} | |
function disconnect() { | |
fclose( $this->socket ); | |
$this->socket = null; | |
} | |
function setIndex($index) { | |
$this->index = $index; | |
} | |
function getIndex() { | |
$response = $this->send('START'); | |
$pdt_idx = $response['pdt_idx']; | |
if( $pdt_idx === -1 ) { | |
throw new WOPR_OverloadException("No available slots"); | |
} | |
$this->index = $pdt_idx; | |
return $pdt_idx; | |
} | |
function getSpecs($command) { | |
$commands = array( | |
'INFO' => array('args' => false, 'index' => false), | |
'START' => array('args' => false, 'index' => false), | |
'DEL' => array('args' => false, 'index' => true), | |
'FEED' => array('args' => '/^[^\\n]+$/', 'index' => true), | |
'LTR' => array('args' => '/^[^\\n\\s]$/', 'index' => true), | |
'WRD' => array('args' => '/^[^\\n\\s]+$/', 'index' => true), | |
'CTX' => array('args' => false, 'index' => true), | |
'GEN' => array('args' => false, 'index' => true), | |
'CLR' => array('args' => false, 'index' => true) | |
); | |
if( !array_key_exists($command, $commands) ) { | |
return null; | |
} else { | |
return $commands[$command]; | |
} | |
} | |
function validate($command, $index, $arguments = "") { | |
$specs = $this->getSpecs($command); | |
if( $specs === null ) { | |
throw new WOPR_Exception("Unknown command: " . json_encode($command)); | |
} | |
if( $specs['args'] === false ) { | |
if( $arguments !== null && $arguments !== "" ) { | |
throw new WOPR_Exception("Command {$command} doesn't take any arguments."); | |
} | |
} else { | |
$match = preg_match($specs['args'], $arguments); | |
if( !$match ) { | |
throw new WOPR_Exception("Invalid arguments for command {$command}"); | |
} | |
} | |
if( $specs['index'] && !is_numeric($index) ) { | |
throw new WOPR_Exception("Command {$command} requires index"); | |
} | |
return true; | |
} | |
function buildCommandString($command, $index = null, $arguments = "") { | |
$this->validate($command, $index, $arguments); | |
$specs = $this->getSpecs($command); | |
$cmdstring = $command; | |
if( $specs['index'] ) { | |
$cmdstring .= " {$index}"; | |
} | |
if( $specs['args'] !== false ) { | |
$cmdstring .= " {$arguments}"; | |
} | |
$cmdstring .= "\n"; | |
return $cmdstring; | |
} | |
// Send a command to WOPR. | |
// Returns WOPR response as a PHP-array. | |
function send($command, $arguments = "") { | |
$this->connect(); | |
$cmdstring = $this->buildCommandString($command, $this->index, $arguments); | |
fwrite($this->socket, $cmdstring); | |
$response = $this->read($command); | |
$this->disconnect(); | |
return $response; | |
} | |
function read($command) { | |
if ( ! feof($this->socket) ) { | |
$response = fgets( $this->socket ); | |
$info = stream_get_meta_data($this->socket); | |
if ($info['timed_out']) { | |
throw new WOPR_TimeoutException(); | |
} | |
if( strlen($response) > 0 ) { | |
return $this->parseResponse($response, $command); | |
} else { | |
return null; | |
} | |
} | |
} | |
function parseResponse($string, $command) { | |
$xml = new SimpleXMLElement($string); | |
$array = (array)$xml; | |
if( isset($array[0]) && $array[0] == 'error' ) { | |
throw new WOPR_Exception("Unknown WOPR error"); | |
} | |
return $array; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment