Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit 83c78c3

Browse files
author
Paweł Niedzielski
committed
Add TCP driver
1 parent 4a1efb4 commit 83c78c3

File tree

3 files changed

+150
-70
lines changed

3 files changed

+150
-70
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* @author Stephen "TheCodeAssassin" Hoogendijk
4+
*/
5+
6+
namespace InfluxDB\Driver;
7+
8+
/**
9+
* Class UDP
10+
*
11+
* @package InfluxDB\Driver
12+
*/
13+
abstract class AbstractSocketDriver implements DriverInterface
14+
{
15+
/**
16+
* Parameters
17+
*
18+
* @var array
19+
*/
20+
private $parameters;
21+
22+
/**
23+
* @var array
24+
*/
25+
protected $config;
26+
27+
/**
28+
*
29+
* @var resource
30+
*/
31+
protected $stream;
32+
33+
/**
34+
* @param string $host IP/hostname of the InfluxDB host
35+
* @param int $port Port of the InfluxDB process
36+
*/
37+
public function __construct($host, $port)
38+
{
39+
$this->config['host'] = $host;
40+
$this->config['port'] = $port;
41+
}
42+
43+
/**
44+
* Close the stream (if created)
45+
*/
46+
public function __destruct()
47+
{
48+
if (isset($this->stream) && is_resource($this->stream)) {
49+
fclose($this->stream);
50+
}
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function setParameters(array $parameters)
57+
{
58+
$this->parameters = $parameters;
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
public function getParameters()
65+
{
66+
return $this->parameters;
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
public function write($data = null)
73+
{
74+
if (isset($this->stream) === false) {
75+
$this->createStream();
76+
}
77+
78+
$this->doWrite($data);
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
abstract public function isSuccess();
85+
86+
/**
87+
* Perform write to socket
88+
* @param mixed|null $data
89+
*/
90+
abstract protected function doWrite($data = null);
91+
92+
/**
93+
* Create the resource stream
94+
*/
95+
abstract protected function createStream();
96+
97+
}

src/InfluxDB/Driver/TCP.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
* @author Stephen "TheCodeAssassin" Hoogendijk
4+
*/
5+
6+
namespace InfluxDB\Driver;
7+
8+
/**
9+
* Class UDP
10+
*
11+
* @package InfluxDB\Driver
12+
*/
13+
class TCP extends AbstractSocketDriver implements DriverInterface
14+
{
15+
16+
private $result;
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function isSuccess()
22+
{
23+
return (bool) $this->result;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
protected function createStream()
30+
{
31+
$host = sprintf('tcp://%s:%d', $this->config['host'], $this->config['port']);
32+
33+
// stream the data using UDP and suppress any errors
34+
$this->stream = @stream_socket_client($host);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function doWrite($data = null)
41+
{
42+
$this->result = false !== fwrite($this->stream, $data);
43+
}
44+
}

src/InfluxDB/Driver/UDP.php

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,8 @@
1010
*
1111
* @package InfluxDB\Driver
1212
*/
13-
class UDP implements DriverInterface
13+
class UDP extends AbstractSocketDriver implements DriverInterface
1414
{
15-
/**
16-
* Parameters
17-
*
18-
* @var array
19-
*/
20-
private $parameters;
21-
22-
/**
23-
* @var array
24-
*/
25-
private $config;
26-
27-
/**
28-
*
29-
* @var resource
30-
*/
31-
private $stream;
32-
33-
/**
34-
* @param string $host IP/hostname of the InfluxDB host
35-
* @param int $port Port of the InfluxDB process
36-
*/
37-
public function __construct($host, $port)
38-
{
39-
$this->config['host'] = $host;
40-
$this->config['port'] = $port;
41-
}
42-
43-
/**
44-
* Close the stream (if created)
45-
*/
46-
public function __destruct()
47-
{
48-
if (isset($this->stream) && is_resource($this->stream)) {
49-
fclose($this->stream);
50-
}
51-
}
52-
53-
/**
54-
* {@inheritdoc}
55-
*/
56-
public function setParameters(array $parameters)
57-
{
58-
$this->parameters = $parameters;
59-
}
60-
61-
/**
62-
* {@inheritdoc}
63-
*/
64-
public function getParameters()
65-
{
66-
return $this->parameters;
67-
}
68-
69-
/**
70-
* {@inheritdoc}
71-
*/
72-
public function write($data = null)
73-
{
74-
if (isset($this->stream) === false) {
75-
$this->createStream();
76-
}
77-
78-
@stream_socket_sendto($this->stream, $data);
79-
80-
return true;
81-
}
82-
8315
/**
8416
* {@inheritdoc}
8517
*/
@@ -89,7 +21,7 @@ public function isSuccess()
8921
}
9022

9123
/**
92-
* Create the resource stream
24+
* {@inheritdoc}
9325
*/
9426
protected function createStream()
9527
{
@@ -99,4 +31,11 @@ protected function createStream()
9931
$this->stream = @stream_socket_client($host);
10032
}
10133

34+
/**
35+
* {@inheritdoc}
36+
*/
37+
protected function doWrite($data = null)
38+
{
39+
@stream_socket_sendto($this->stream, $data);
40+
}
10241
}

0 commit comments

Comments
 (0)