Skip to content

Commit

Permalink
Merge pull request #6 from exodus4d/develop
Browse files Browse the repository at this point in the history
v1.0.0
  • Loading branch information
exodus4d authored Oct 22, 2017
2 parents 7a8a138 + 0f492ee commit 5aaa98e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 17 deletions.
72 changes: 72 additions & 0 deletions app/Main/Handler/LogFileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Created by PhpStorm.
* User: exodu
* Date: 03.09.2017
* Time: 17:02
*/

namespace Exodus4D\Socket\Main\Handler;


class LogFileHandler {

const ERROR_DIR_CREATE = 'There is no existing directory at "%s" and its not buildable.';

/**
* steam uri
* @var string
*/
private $stream = '';

/**
* stream dir
* @var string
*/
private $dir = '.';

/**
* file base dir already created
* @var bool
*/
private $dirCreated = false;

public function __construct(string $stream){
$this->stream = $stream;
$this->dir = dirname($this->stream);
$this->createDir();
}

/**
* write log data into to file
* @param array $log
*/
public function write(array $log){
$log = (string)json_encode($log, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if( !empty($log) ){
$stream = fopen($this->stream, 'a');
flock($stream, LOCK_EX);
fwrite($stream, $log . PHP_EOL);
flock($stream, LOCK_UN);
fclose($stream);
}
}

/**
* create directory
*/
private function createDir(){
// Do not try to create dir if it has already been tried.
if ($this->dirCreated){
return;
}

if ($this->dir && !is_dir($this->dir)){
$status = mkdir($this->dir, 0777, true);
if (false === $status) {
throw new \UnexpectedValueException(sprintf(self::ERROR_DIR_CREATE, $this->dir));
}
}
$this->dirCreated = true;
}
}
39 changes: 29 additions & 10 deletions app/Main/MapUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Exodus4D\Socket\Main;

use Exodus4D\Socket\Main\Handler\LogFileHandler;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

Expand Down Expand Up @@ -58,14 +59,7 @@ class MapUpdate implements MessageComponentInterface {
*/
protected $debug = false;

/**
* internal socket for response calls
* @var null | \React\ZMQ\SocketWrapper
*/
protected $internalSocket = null;

public function __construct($socket) {
$this->internalSocket = $socket;
public function __construct() {
$this->characterAccessData = [];
$this->mapAccessData = [];
$this->characters = [];
Expand Down Expand Up @@ -229,6 +223,19 @@ private function unSubscribeCharacterId($characterId, $conn = null){
return true;
}

/**
* unSubscribe $characterIds from ALL maps
* @param array $characterIds
* @return bool
*/
private function unSubscribeCharacterIds(array $characterIds): bool{
$response = false;
foreach($characterIds as $characterId){
$response = $this->unSubscribeCharacterId($characterId);
}
return $response;
}

/**
* delete mapId from subscriptions and broadcast "delete msg" to clients
* @param string $task
Expand Down Expand Up @@ -410,7 +417,7 @@ public function receiveData($data){

switch($data['task']){
case 'characterLogout':
$response = $this->unSubscribeCharacterId($load);
$response = $this->unSubscribeCharacterIds($load);
break;
case 'mapConnectionAccess':
$response = $this->setConnectionAccess($load);
Expand All @@ -428,9 +435,11 @@ public function receiveData($data){
$this->healthCheckToken = (float)$load;
$response = 'OK';
break;
case 'logData':
$this->handleLogData((array)$load['meta'], (array)$load['log']);
break;
}

// $this->internalSocket->send($response);
}

/**
Expand Down Expand Up @@ -536,6 +545,16 @@ private function setConnectionAccess($connectionAccessData) {
return $response;
}

/**
* dispatch log writing to a LogFileHandler
* @param array $meta
* @param array $log
*/
private function handleLogData(array $meta, array $log){
$logHandler = new LogFileHandler((string)$meta['stream']);
$logHandler->write($log);
}


// logging ====================================================================================

Expand Down
16 changes: 11 additions & 5 deletions app/WebSockets.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,26 @@ private function startMapSocket(){
// Listen for the web server to make a ZeroMQ push after an ajax request
$context = new React\ZMQ\Context($loop);

//$pull = $context->getSocket(\ZMQ::SOCKET_REP);
// Socket for map update data -------------------------------------------------------------
$pull = $context->getSocket(\ZMQ::SOCKET_PULL);
// Binding to 127.0.0.1 means, the only client that can connect is itself
$pull->bind( $this->dns );

// main app -> inject socket for response
$mapUpdate = new Main\MapUpdate($pull);
$mapUpdate = new Main\MapUpdate();
// "onMessage" listener
$pull->on('message', [$mapUpdate, 'receiveData']);

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($loop);
// Socket for log data --------------------------------------------------------------------
//$pullSocketLogs = $context->getSocket(\ZMQ::SOCKET_PULL);
//$pullSocketLogs->bind( "tcp://127.0.0.1:5555" );
//$pullSocketLogs->on('message', [$mapUpdate, 'receiveLogData']) ;

// Binding to 0.0.0.0 means remotes can connect (Web Clients)
$webSock->listen($this->wsListenPort, $this->wsListenHost);
$webSocketURI = $this->wsListenHost . ':' . $this->wsListenPort;

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server($webSocketURI, $loop);
new IoServer(
new HttpServer(
new WsServer(
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php-64bit": ">=7.0",
"ext-zmq": "1.1.*",
"cboden/ratchet": "0.3.*",
"react/zmq": "0.3.*"
"cboden/ratchet": "0.4.x-dev",
"react/zmq": "dev-master"
}
}

0 comments on commit 5aaa98e

Please sign in to comment.