Skip to content

Commit

Permalink
Finalizes http connection
Browse files Browse the repository at this point in the history
  • Loading branch information
moay committed Aug 28, 2019
1 parent e5682a6 commit 54c32cf
Show file tree
Hide file tree
Showing 6 changed files with 319 additions and 37 deletions.
22 changes: 17 additions & 5 deletions src/Communication/AbstractCachingCommunicator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setFullPushInterval(int $fullPushInterval): void
*
* @return array
*/
public function handleCaching(MiniserverInformation $miniserver, $data): array
protected function handleCaching(MiniserverInformation $miniserver, $data): array
{
if (!$this->fullPushRequired($miniserver) && !$this->miniserverRebooted($miniserver)) {
$data = $this->filterChangedValues($miniserver, $data);
Expand All @@ -61,7 +61,7 @@ private function filterChangedValues(MiniserverInformation $miniserver, array $d
{
$filteredData = [];
foreach ($data as $key => $value) {
if (!$this->cache->valueDiffersFromStored($key, $value, $miniserver->getIpAddress(), $this->getCommunicatorPort())) {
if ($this->valueMatchesCache($miniserver, $key, $value)) {
continue;
}

Expand All @@ -74,10 +74,22 @@ private function filterChangedValues(MiniserverInformation $miniserver, array $d

/**
* @param MiniserverInformation $miniserver
* @param string $key
* @param $value
*
* @return bool
*/
private function fullPushRequired(MiniserverInformation $miniserver): bool
protected function valueMatchesCache(MiniserverInformation $miniserver, string $key, $value): bool
{
return !$this->cache->valueDiffersFromStored($key, $value, $miniserver->getIpAddress(), $this->getCommunicatorPort());
}

/**
* @param MiniserverInformation $miniserver
*
* @return bool
*/
protected function fullPushRequired(MiniserverInformation $miniserver): bool
{
$lastFullPush = $this->cache->get(self::CACHE_KEY_LAST_FULL_PUSH, $miniserver->getIpAddress(), $this->getCommunicatorPort());

Expand All @@ -93,7 +105,7 @@ private function fullPushRequired(MiniserverInformation $miniserver): bool
*
* @return bool
*/
private function miniserverRebooted(MiniserverInformation $miniserver): bool
protected function miniserverRebooted(MiniserverInformation $miniserver): bool
{
$lastRebootCheck = $this->cache->get(self::CACHE_KEY_LAST_REBOOT_CHECK, $miniserver->getIpAddress(), $this->getCommunicatorPort());

Expand Down Expand Up @@ -132,7 +144,7 @@ private function getCurrentLanPackages(MiniserverInformation $miniserver): int
return 0;
}

return (int) $status->getContent();
return (int) $status->getValue();
}

/**
Expand Down
122 changes: 107 additions & 15 deletions src/Communication/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace LoxBerry\Communication;

use LoxBerry\Communication\ValueCache\HttpValueCache;
use LoxBerry\Communication\ValueCache\UdpValueCache;
use LoxBerry\ConfigurationParser\MiniserverInformation;
use LoxBerry\System\LowLevelExecutor;

Expand All @@ -12,39 +11,132 @@
*/
class Http extends AbstractCachingCommunicator
{
/** @var LowLevelExecutor */
private $lowLevel;

/**
* Udp constructor.
* Http constructor.
*
* @param UdpValueCache $cache
* @param HttpValueCache $cache
* @param LowLevelExecutor $lowLevelExecutor
*/
public function __construct(HttpValueCache $cache, LowLevelExecutor $lowLevelExecutor)
public function __construct(HttpValueCache $cache, LowLevelExecutor $lowLevel)
{
$this->cache = $cache;
$this->lowLevelExecutor = $lowLevelExecutor;
$this->lowLevel = $lowLevel;
}

/**
* @param MiniserverInformation $miniserver
* @param string $block
* @param $value
* @param bool $ifChanged
*
* @return bool
*/
public function setBlockValue(MiniserverInformation $miniserver, string $block, $value, bool $ifChanged = true): bool
{
if (
$ifChanged
&& !$this->fullPushRequired($miniserver)
&& !$this->miniserverRebooted($miniserver)
&& $this->valueMatchesCache($miniserver, $block, $value)
) {
return true;
}

$response = $this->call($miniserver, sprintf(
'/dev/sps/io/%s/%s',
$block,
$value
));

$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$this->cache->put($block, $value, $miniserver->getIpAddress(), $miniserver->getPort());

return 200 === $response->getResponseCode() && (string) $value == $response->getValue();
}

public function blockAction(MiniserverInformation $miniserver, array $blocks): array
/**
* @param MiniserverInformation $miniserver
* @param array $data
* @param bool $ifChanged
*
* @return array
*/
public function setBlockValues(MiniserverInformation $miniserver, array $data, bool $ifChanged = true): array
{
// Todo: test and implement
if ($ifChanged) {
$data = $this->handleCaching($miniserver, $data);
}

$responseArray = [];
foreach ($data as $key => $value) {
$responseArray[$key] = $this->setBlockValue($miniserver, $key, $value, false);
}

return $responseArray;
}

public function call(MiniserverInformation $miniserver, string $command): HttpResponse
/**
* @param MiniserverInformation $miniserver
* @param string $block
*
* @return mixed
*/
public function getBlockValue(MiniserverInformation $miniserver, string $block)
{
// Todo: test and implement
$response = $this->call($miniserver, sprintf(
'/dev/sps/io/%s',
$block
));

if (200 !== $response->getResponseCode()) {
return null;
}

if (is_numeric($response->getValue())) {
return $response->getValue() + 0;
}

return $response->getValue();
}

public function send(MiniserverInformation $miniserver, array $data)
/**
* @param MiniserverInformation $miniserver
* @param string $command
*
* @return HttpResponse
*/
public function call(MiniserverInformation $miniserver, string $command): HttpResponse
{
// Todo: test and implement
$baseUrl = sprintf(
'http://%s@%s:%s',
$miniserver->getCredentials(),
$miniserver->getIpAddress(),
$miniserver->getPort() ?? 80
);

$response = $this->lowLevel->fileGetContents($baseUrl.$command);
if (!$response) {
throw new \RuntimeException('Webservices http call to miniserver failed');
}

return $this->parseRawXmlResponse($response);
}

public function sendChanged(MiniserverInformation $miniserver, array $data)
/**
* @param string $response
*
* @return HttpResponse
*/
private function parseRawXmlResponse(string $response): HttpResponse
{
$data = $this->handleCaching($miniserver, $data);
preg_match('/value\=\"(.*?)\"/', $response, $matches);
$value = $matches[1];

preg_match('/Code\=\"(.*?)\"/', $response, $matches);
$code = $matches[1];

$this->send($miniserver, $data);
return new HttpResponse($value, $code, $response);
}
}
12 changes: 6 additions & 6 deletions src/Communication/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class HttpResponse
{
/** @var string|null */
private $content;
private $value;

/** @var int */
private $responseCode;
Expand All @@ -19,23 +19,23 @@ class HttpResponse
/**
* HttpResponse constructor.
*
* @param string|null $content
* @param string|null $value
* @param int $responseCode
* @param string|null $rawResponse
*/
public function __construct(?string $content = null, int $responseCode = 500, ?string $rawResponse = null)
public function __construct(?string $value = null, int $responseCode = 500, ?string $rawResponse = null)
{
$this->content = $content;
$this->value = $value;
$this->responseCode = $responseCode;
$this->rawResponse = $rawResponse;
}

/**
* @return string|null
*/
public function getContent(): ?string
public function getValue(): ?string
{
return $this->content;
return $this->value;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Communication/Udp.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Udp extends AbstractCachingCommunicator
private $udpPort;

/** @var LowLevelExecutor */
private $lowLevelExecutor;
private $lowLevel;

/** @var resource */
private $socket;
Expand All @@ -27,12 +27,12 @@ class Udp extends AbstractCachingCommunicator
* Udp constructor.
*
* @param UdpValueCache $cache
* @param LowLevelExecutor $lowLevelExecutor
* @param LowLevelExecutor $lowLevel
*/
public function __construct(UdpValueCache $cache, LowLevelExecutor $lowLevelExecutor, Http $http)
public function __construct(UdpValueCache $cache, LowLevelExecutor $lowLevel, Http $http)
{
$this->cache = $cache;
$this->lowLevelExecutor = $lowLevelExecutor;
$this->lowLevel = $lowLevel;
$this->http = $http;

$this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
Expand Down Expand Up @@ -139,7 +139,7 @@ private function prepareMessages(array $data, ?string $prefix = null): array
*/
private function sendViaUdp(MiniserverInformation $miniserver, string $message)
{
$this->lowLevelExecutor->sendToSocket(
$this->lowLevel->sendToSocket(
$this->socket,
$message,
strlen($message),
Expand Down

0 comments on commit 54c32cf

Please sign in to comment.