This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
Add TCP driver alongiside UDP one #124
Open
Steveb-p
wants to merge
3
commits into
influxdata:master
Choose a base branch
from
Steveb-p:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* @author Stephen "TheCodeAssassin" Hoogendijk | ||
*/ | ||
|
||
namespace InfluxDB\Driver; | ||
|
||
/** | ||
* Class UDP | ||
* | ||
* @package InfluxDB\Driver | ||
*/ | ||
abstract class AbstractSocketDriver implements DriverInterface | ||
{ | ||
/** | ||
* Parameters | ||
* | ||
* @var array | ||
*/ | ||
private $parameters; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $config; | ||
|
||
/** | ||
* | ||
* @var resource | ||
*/ | ||
protected $stream; | ||
|
||
/** | ||
* @param string $host IP/hostname of the InfluxDB host | ||
* @param int $port Port of the InfluxDB process | ||
*/ | ||
public function __construct($host, $port) | ||
{ | ||
$this->config['host'] = $host; | ||
$this->config['port'] = $port; | ||
} | ||
|
||
/** | ||
* Close the stream (if created) | ||
*/ | ||
public function __destruct() | ||
{ | ||
if (isset($this->stream) && is_resource($this->stream)) { | ||
fclose($this->stream); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setParameters(array $parameters) | ||
{ | ||
$this->parameters = $parameters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParameters() | ||
{ | ||
return $this->parameters; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($data = null) | ||
{ | ||
if (isset($this->stream) === false) { | ||
$this->createStream(); | ||
} | ||
|
||
if ($data !== null) { | ||
$this->doWrite($data); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
abstract public function isSuccess(); | ||
|
||
/** | ||
* Perform write to socket | ||
* @param mixed $data | ||
*/ | ||
abstract protected function doWrite($data); | ||
|
||
/** | ||
* Create the resource stream | ||
*/ | ||
abstract protected function createStream(); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* @author Stephen "TheCodeAssassin" Hoogendijk | ||
*/ | ||
|
||
namespace InfluxDB\Driver; | ||
|
||
/** | ||
* Class UDP | ||
* | ||
* @package InfluxDB\Driver | ||
*/ | ||
class TCP extends AbstractSocketDriver implements DriverInterface | ||
{ | ||
|
||
private $result; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isSuccess() | ||
{ | ||
return (bool) $this->result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function createStream() | ||
{ | ||
$host = sprintf('tcp://%s:%d', $this->config['host'], $this->config['port']); | ||
|
||
// stream the data using TCP and suppress any errors | ||
$this->stream = @stream_socket_client($host); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function doWrite($data) | ||
{ | ||
$this->result = false !== @fwrite($this->stream, "$data\n"); | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An issue can occur when multiple messages are written into TCP socket:
Each message has to end with newline.