Merge pull request #7 from AnthorNet/master

Add PHP Client/Publisher
This commit is contained in:
James Muscat 2015-04-24 16:53:37 +01:00
commit bf952a736a
5 changed files with 392 additions and 17 deletions

3
examples/PHP/Client.bat Normal file
View File

@ -0,0 +1,3 @@
"php.exe" -f %~dp0\Client.php
pause

173
examples/PHP/Client.php Normal file
View File

@ -0,0 +1,173 @@
<?php
/**
* Configuration
*/
$relayEDDN = 'tcp://eddn-relay.elite-markets.net:9500';
$logFile = dirname(__FILE__) . '/Logs_EDDN_' . date('Y-m-d') . '.htm';
// A sample list of authorised softwares
$authorisedSoftwares = array(
"ED-TD.SPACE",
"EliteOCR",
"RegulatedNoise",
"RegulatedNoise__DJ",
"Maddavo's Market Share"
);
// Used this to excludes yourself for example has you don't want to handle your own messages ^^
$excludedSoftwares = array(
'My Awesome Market Uploader'
);
/**
* START
*/
$oldTime = false;
function echoLog($str)
{
global $oldTime, $logFile;
if(!file_exists($logFile))
{
file_put_contents(
$logFile,
'<style type="text/css">html { white-space: pre; font-family: Courier New,Courier,Lucida Sans Typewriter,Lucida Typewriter,monospace; }</style>'
);
}
if($oldTime != date('H:i:s') || $oldTime === false)
{
$oldTime = date('H:i:s');
$str = $oldTime . ' | ' . $str;
}
else
$str = ' ' . ' | ' . $str;
fwrite(STDOUT, $str . PHP_EOL);
file_put_contents(
$logFile,
$str . PHP_EOL,
FILE_APPEND
);
}
// UTC
date_default_timezone_set('UTC');
echoLog('Starting EDDN Subscribe');
echoLog('');
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_SUB);
$socket->setSockOpt(ZMQ::SOCKOPT_SUBSCRIBE, "");
$socket->setSockOpt(ZMQ::SOCKOPT_RCVTIMEO, 600000);
while (true)
{
try
{
$socket->connect($relayEDDN);
while (true)
{
$message = $socket->recv();
if ($message === false)
{
$socket->disconnect($relayEDDN);
break;
}
$json = zlib_decode($message);
$array = json_decode($json, true);
$converted = false;
// Handle commodity v1
if($array['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/1')
{
echoLog('Receiving commodity-v1 message...');
echoLog(' - Converting to v2...');
$temp = array();
$temp['$schemaRef'] = 'http://schemas.elite-markets.net/eddn/commodity/2';
$temp['header'] = $array['header'];
$temp['message'] = array();
$temp['message']['systemName'] = $array['message']['systemName'];
$temp['message']['stationName'] = $array['message']['stationName'];
$temp['message']['timestamp'] = $array['message']['timestamp'];
$temp['message']['commodities'] = array();
$commodity = array();
if(array_key_exists('itemName', $array['message']))
$commodity['name'] = $array['message']['itemName'];
if(array_key_exists('buyPrice', $array['message']))
$commodity['buyPrice'] = $array['message']['buyPrice'];
if(array_key_exists('stationStock', $array['message']))
$commodity['supply'] = $array['message']['stationStock'];
if(array_key_exists('supplyLevel', $array['message']))
$commodity['supplyLevel'] = $array['message']['supplyLevel'];
if(array_key_exists('sellPrice', $array['message']))
$commodity['sellPrice'] = $array['message']['sellPrice'];
if(array_key_exists('demand', $array['message']))
$commodity['demand'] = $array['message']['demand'];
if(array_key_exists('demandLevel', $array['message']))
$commodity['demandLevel'] = $array['message']['demandLevel'];
$temp['message']['commodities'][] = $commodity;
$array = $temp;
unset($temp, $commodity);
$converted = true;
}
// Handle commodity v2
if($array['$schemaRef'] == 'http://schemas.elite-markets.net/eddn/commodity/2')
{
if($converted === false)
echoLog('Receiving commodity-v2 message...');
unset($converted);
$authorised = false;
$excluded = false;
if(in_array($array['header']['softwareName'], $authorisedSoftwares))
$authorised = true;
if(in_array($array['header']['softwareName'], $excludedSoftwares))
$excluded = true;
echoLog(' - Software: ' . $array['header']['softwareName'] . ' / ' . $array['header']['softwareVersion']);
echoLog(' - ' . (($authorised === true)
? 'AUTHORISED'
: (( $excluded === true) ? 'EXCLUDED' : 'UNAUTHORISED')
));
if($authorised === true && $excluded === false)
{
// Do what you want with the data...
// Have fun !
}
unset($authorised, $excluded);
}
echoLog('');
echoLog('');
}
}
catch (ZMQSocketException $e)
{
echoLog('ZMQSocketException: ' . $e);
sleep(10);
}
}
// Exit correctly
exit(0);

197
examples/PHP/EDDN.php Normal file
View File

@ -0,0 +1,197 @@
<?php
/**
*
* $eddn = new EDDN(array(
* 'uploaderID' => 'abcdef0123456789',
* 'softwareName' => 'My Awesome Market Uploader',
* 'softwareVersion' => 'v3.14'
* ));
*
* $result = $eddn->publishCommodityV1(
* 'Eranin',
* 'Azeban Orbital',
* time(),
* array(
* "itemName" => "Gold",
* "buyPrice" => 1024,
* "supply" => 7,
* "stationStock" => "Low",
* "sellPrice" => 1138,
* "demand" => 42,
* "demandLevel" => "Med"
* )
* ); // return true;
*
*
* $result = $eddn->publishCommodityV2(
* 'Eranin',
* 'Azeban Orbital',
* time(),
* array(
* array(
* "name" => "Gold",
* "buyPrice" => 1024,
* "supply" => 7,
* "supplyLevel" => "Low",
* "sellPrice" => 1138,
* "demand" => 42,
* "demandLevel" => "Med"
* ),
* array(
* "name" => "Explosives",
* "buyPrice" => 999,
* "supply" => 1500,
* "supplyLevel" => "Low",
* "sellPrice" => 0,
* "demand" => 0
* )
* )
* ); // return true;
*
**/
class EDDN
{
static private $_debug = true;
static private $_gateways = array(
'http://eddn-gateway.elite-markets.net:8080/upload/'
);
static private $_schemas = array(
'commodity-v1' => array(
'production' => 'http://schemas.elite-markets.net/eddn/commodity/1',
'test' => 'http://schemas.elite-markets.net/eddn/commodity/1/test'
),
'commodity-v2' => array(
'production' => 'http://schemas.elite-markets.net/eddn/commodity/2',
'test' => 'http://schemas.elite-markets.net/eddn/commodity/2/test'
)
);
private $_uploaderID = null;
private $_softwareName = null;
private $_softwareVersion = null;
public function __Construct(array $options)
{
if(array_key_exists('uploaderID', $options))
$this->setUploaderID($options['uploaderID']);
else
throw new Exception('Option "uploaderID" is required.');
if(array_key_exists('softwareName', $options))
$this->setSoftwareName($options['softwareName']);
else
throw new Exception('Option "softwareName" is required.');
if(array_key_exists('softwareVersion', $options))
$this->setSoftwareVersion($options['softwareVersion']);
else
throw new Exception('Option "softwareVersion" is required.');
}
public function publishCommodityV1($systemName, $stationName, $timestamp, array $commodity)
{
$schema = self::$_schemas['commodity-v1'][((self::$_debug === true) ? 'test' : 'production')];
$message = array();
$message['systemName'] = $systemName;
$message['stationName'] = $stationName;
$message['timestamp'] = date('c', $timestamp);
foreach($commodity AS $key => $value)
$message[$key] = $value;
return $this->_postToEDDN($schema, $message);
}
public function publishCommodityV2($systemName, $stationName, $timestamp, array $commodities)
{
$schema = self::$_schemas['commodity-v2'][((self::$_debug === true) ? 'test' : 'production')];
$message = array();
$message['systemName'] = $systemName;
$message['stationName'] = $stationName;
$message['timestamp'] = date('c', $timestamp);
$message['commodities'] = $commodities;
return $this->_postToEDDN($schema, $message);
}
private function _generateHeader()
{
$header = array();
$header['uploaderID'] = $this->getUploaderID();
$header['softwareName'] = $this->getSoftwareName();
$header['softwareVersion'] = $this->getSoftwareVersion();
return $header;
}
private function _postToEDDN($schema, array $message)
{
$array = array();
$array['$schemaRef'] = $schema;
$array['header'] = $this->_generateHeader();
$array['message'] = $message;
$json = json_encode($array);
if(function_exists('curl_version'))
{
$gateway = self::$_gateways[array_rand(self::$_gateways)];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $gateway);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$result = curl_exec($ch);
curl_close($ch);
unset($ch);
if($result == 'OK')
return true;
else
return $result;
}
else
throw new Exception('You must have CURL extension in order to publish to EDDN');
}
public function setUploaderID($value)
{
$this->_uploaderID = $value;
}
public function getUploaderID()
{
return $this->_uploaderID;
}
public function setSoftwareName($value)
{
$this->_softwareName = $value;
}
public function getSoftwareName()
{
return $this->_softwareName;
}
public function setSoftwareVersion($value)
{
$this->_softwareVersion = $value;
}
public function getSoftwareVersion()
{
return $this->_softwareVersion;
}
}

View File

@ -11,8 +11,10 @@
"itemName": "Gold",
"buyPrice": 1024,
"stationStock": 7,
"supplyLevel": "Low",
"sellPrice": 1138,
"demand": 42,
"demandLevel": "Med"
"timestamp": "2014-11-17T12:34:56+00:00"
}
}

View File

@ -10,23 +10,23 @@
"stationName": "Azeban Orbital",
"timestamp": "2014-11-17T12:34:56+00:00"
"commodities": [
{
"name": "Gold",
"buyPrice": 1024,
"supply": 7,
"supplyLevel": "Low",
"sellPrice": 1138,
"demand": 42,
"demandLevel": "Med"
},
{
"name": "Explosives",
"buyPrice": 999,
"supply": 1500,
"supplyLevel": "Low",
"sellPrice": 0,
"demand": 0
}
{
"name": "Gold",
"buyPrice": 1024,
"supply": 7,
"supplyLevel": "Low",
"sellPrice": 1138,
"demand": 42,
"demandLevel": "Med"
},
{
"name": "Explosives",
"buyPrice": 999,
"supply": 1500,
"supplyLevel": "Low",
"sellPrice": 0,
"demand": 0
}
]
}
}