Skip to content

Commit

Permalink
Demonstrate that no players lead to error.
Browse files Browse the repository at this point in the history
  • Loading branch information
hakre committed Aug 4, 2012
1 parent 0202ead commit 0cf5df0
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 17 deletions.
12 changes: 11 additions & 1 deletion README.markdown
Expand Up @@ -34,4 +34,14 @@ A stdClass object on success, FALSE on failure.

## License

Copyright (c) 2011 Vex Software LLC, released under the GPL v3.
Copyright (c) 2011 Vex Software LLC, released under the GPL v3.

## Coding Standard:

Line Ending: UNIX
Indent: Tabs
Tab Size: 4
Brackets open at end of line for:
- Functions
- Classes
- Class Functions
68 changes: 52 additions & 16 deletions minequery.class.php
Expand Up @@ -21,6 +21,11 @@
* The Minequery PHP class.
*/
class Minequery {
/**
* @var string name of itself for testing
*/
public static $classname = 'Minequery';

/**
* Queries a Minequery server.
*
Expand All @@ -33,23 +38,14 @@ class Minequery {
public static function query($address, $port = 25566, $timeout = 30) {
$query = array();

$beginning_time = microtime(true);

$socket = @fsockopen($address, $port, $errno, $errstr, $timeout);

if (!$socket) {
// Could not establish a connection to the server.
return false;
}
$write = "QUERY\n";

$end_time = microtime(true);
$class = self::$classname;

fwrite($socket, "QUERY\n");
$response = $class::read($address, $port, $errno, $errstr, $timeout, $write, $latency);

$response = "";

while(!feof($socket)) {
$response .= fgets($socket, 1024);
if (false === $response) {
return false;
}

$response = explode("\n", $response);
Expand All @@ -70,7 +66,7 @@ public static function query($address, $port = 25566, $timeout = 30) {
$query['playerList'] = explode(" ", $response[3], 2);
$query['playerList'] = explode(", ", trim($query['playerList'][1], "[]"));

$query['latency'] = ($end_time - $beginning_time) * 1000;
$query['latency'] = $latency;

return $query;
}
Expand Down Expand Up @@ -100,7 +96,7 @@ public static function query_json($address, $port = 25566, $timeout = 30) {

$response = "";

while(!feof($socket)) {
while (!feof($socket)) {
$response .= fgets($socket, 1024);
}

Expand All @@ -109,4 +105,44 @@ public static function query_json($address, $port = 25566, $timeout = 30) {

return $query;
}

/**
* Queries a Remote Socket
*
* @static
* @param string $address The address to the Minequery server.
* @param int $port The port of the Minequery server.
* @param int $errno
* @param string $errstr
* @param int $timeout The time given before the connection attempt gives up.
* @param string $write
* @param int $latency
* @return string|bool An string on success, FALSE on failure.
*/
protected static function read($address, $port, &$errno, &$errstr, $timeout, $write, &$latency) {
$latency = NULL;

$beginning_time = microtime(true);

$socket = @fsockopen($address, $port, $errno, $errstr, $timeout);

if (!$socket) {
// Could not establish a connection to the server.
return false;
}

$end_time = microtime(true);

$latency = ($end_time - $beginning_time) * 1000;

fwrite($socket, $write);

$response = "";

while (!feof($socket)) {
$response .= fgets($socket, 1024);
}

return $response;
}
}
35 changes: 35 additions & 0 deletions test-failure-no-player-query.php
@@ -0,0 +1,35 @@
<?php
/**
* If Array Of Players Is Empty, Then Echo “No Players Online”
* @link http://stackoverflow.com/q/11807495/367456
*/

require('minequery.class.php');

$host = "67.202.102.224";

/**
* Minequery Stub of a read()
*/
class MinequeryStub extends Minequery {
protected static function read($address, $port, &$errno, &$errstr, $timeout, $write, &$latency) {
$latency = 1000;
return <<<RESPONSE
_reserved_ serverPort
_reserved_ playerCount
_reserved_ maxPlayers
_reserved_ []
RESPONSE;
}
}

Minequery::$classname = 'MinequeryStub';

$query = Minequery::query($host, $port, 3);
$playerList = $query['playerList'];
$failure = array("");
$fixed = array();

printf("The error exists: %s\n", $playerList === $failure ? 'Yes' : 'No');
printf("The error is fixed: %s\n", $playerList === $fixed ? 'Yes' : 'No');
printf("Result: %s\n", $playerList === $fixed ? 'OK' : 'FAIL');

0 comments on commit 0cf5df0

Please sign in to comment.