Permalink
Browse files

Docs, refactoring

  • Loading branch information...
1 parent 96afff7 commit 32855fe77b9c2aef0849fdd2d6e6169da56d6f00 @kakserpom committed Mar 19, 2013
@@ -57,7 +57,7 @@ public function init() {
try {$this->header('Content-Type: text/html');} catch (Exception $e) {}
$this->appInstance->httpclient->post(
- ['http://phpdaemon.net/Example/', 'foo' => 'bar'], ['postField' => 'value'],
+ ['https://phpdaemon.net/Example/', 'foo' => 'bar'], ['postField' => 'value'],
function($conn, $success) {
echo $conn->body;
Daemon::$req->finish();
View
@@ -63,7 +63,7 @@ public function __construct($name = '') {
$defaults = $this->getConfigDefaults();
if ($defaults) {
- $this->processDefaultConfig($defaults);
+ $this->config->imposeDefault($defaults);
}
$this->init();
@@ -166,46 +166,11 @@ public function directCall($workerId, $method, $args = [], $cb = NULL) {
);
}
-
- /**
- * Process default config
- * @todo move it to Daemon_Config class
- * @param array {"setting": "value"}
- * @return void
- */
- protected function processDefaultConfig($settings = []) {
- foreach ($settings as $k => $v) {
- $k = strtolower(str_replace('-', '', $k));
-
- if (!isset($this->config->{$k})) {
- if (is_scalar($v)) {
- $this->config->{$k} = new Daemon_ConfigEntry($v);
- } else {
- $this->config->{$k} = $v;
- }
- } elseif ($v instanceof Daemon_ConfigSection) {
- // @todo
- } else {
- $current = $this->config->{$k};
- if (is_scalar($v)) {
- $this->config->{$k} = new Daemon_ConfigEntry($v);
- } else {
- $this->config->{$k} = $v;
- }
-
- $this->config->{$k}->setHumanValue($current->value);
- $this->config->{$k}->source = $current->source;
- $this->config->{$k}->revision = $current->revision;
- }
- }
- }
-
/**
* Called when the worker is ready to go
- * @todo -> protected?
* @return void
*/
- public function onReady() { }
+ protected function onReady() {}
/**
* Called when creates instance of the application
View
@@ -56,7 +56,7 @@ public function onConfigUpdated() {
}
}
if ($defaults = $this->getConfigDefaults()) {
- $this->processDefaultConfig($defaults);
+ $this->config->imposeDefault($defaults);
}
$this->applyConfig();
}
@@ -119,40 +119,6 @@ public static function getInstance($arg = '', $spawn = true) {
}
}
-
- /**
- * Process default config
- * @todo move it to Daemon_Config class
- * @param ["setting" => "value", ...]
- * @return void
- */
- public function processDefaultConfig($settings = []) {
- foreach ($settings as $k => $v) {
- $k = strtolower(str_replace('-', '', $k));
-
- if (!isset($this->config->{$k})) {
- if (is_scalar($v)) {
- $this->config->{$k} = new Daemon_ConfigEntry($v);
- } else {
- $this->config->{$k} = $v;
- }
- } elseif ($v instanceof Daemon_ConfigSection) {
- // @todo
- } else {
- $current = $this->config->{$k};
- if (is_scalar($v)) {
- $this->config->{$k} = new Daemon_ConfigEntry($v);
- } else {
- $this->config->{$k} = $v;
- }
-
- $this->config->{$k}->setHumanValue($current->value);
- $this->config->{$k}->source = $current->source;
- $this->config->{$k}->revision = $current->revision;
- }
- }
- }
-
public function setConnectionClass($class) {
$this->connectionClass = $class;
}
View
@@ -70,8 +70,8 @@ public function applyConfig() {
if ($file) {
preg_match_all('~nameserver ([^\r\n;]+)~', $data, $m);
foreach ($m[1] as $s) {
- $pool->addServer('dns://[udp:' . $s . ']');
- //$pool->addServer('dns://[' . $s . ']');
+ $pool->addServer('udp://' . $s);
+ //$pool->addServer('tcp://' . $s);
}
}
$job->setResult($jobname);
@@ -66,4 +66,34 @@ public function offsetUnset($offset) {
unset($this->{$this->getRealOffsetName($offset)});
}
+ /**
+ * Impose default config
+ * @param array {"setting": "value"}
+ * @return void
+ */
+ public function imposeDefault($settings = []) {
+ foreach ($settings as $name => $value) {
+ $name = strtolower(str_replace('-', '', $name));
+ if (!isset($this->{$name})) {
+ if (is_scalar($value)) {
+ $this->{$name} = new Daemon_ConfigEntry($value);
+ } else {
+ $this->{$name} = $value;
+ }
+ } elseif ($value instanceof Daemon_ConfigSection) {
+ $value->imposeDefault($value);
+ } else {
+ $current = $this->{$name};
+ if (is_scalar($value)) {
+ $this->{$name} = new Daemon_ConfigEntry($value);
+ } else {
+ $this->{$name} = $value;
+ }
+
+ $this->{$name}->setHumanValue($current->value);
+ $this->{$name}->source = $current->source;
+ $this->{$name}->revision = $current->revision;
+ }
+ }
+ }
}
View
@@ -24,10 +24,11 @@ public function get($url, $params) {
$params = ['resultcb' => $params];
}
if (!isset($params['uri']) || !isset($params['host'])) {
- list ($params['host'], $params['uri'], $params['port']) = HTTPClient::prepareUrl($url);
+ list ($params['scheme'], $params['host'], $params['uri'], $params['port']) = self::prepareUrl($url);
}
+ $ssl = $params['scheme'] === 'https';
$this->getConnection(
- $params['host'] . (isset($params['port']) ? $params['port'] : null),
+ 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null),
function($conn) use ($url, $params) {
$conn->get($url, $params);
}
@@ -39,10 +40,11 @@ public function post($url, $data = [], $params) {
$params = ['resultcb' => $params];
}
if (!isset($params['uri']) || !isset($params['host'])) {
- list ($params['host'], $params['uri'], $params['port']) = HTTPClient::prepareUrl($url);
+ list ($params['scheme'], $params['host'], $params['uri'], $params['port']) = self::prepareUrl($url);
}
+ $ssl = $params['scheme'] === 'https';
$this->getConnection(
- $params['host'] . (isset($params['port']) ? $params['port'] : null),
+ 'tcp://' . $params['host'] . (isset($params['port']) ? ':' . $params['port'] : null) . ($ssl ? '#ssl' : ''),
function($conn) use ($url, $data, $params) {
$conn->post($url, $data, $params);
}
@@ -83,7 +85,7 @@ public static function prepareUrl($mixed) {
$uri .= '?'.$u['query'];
}
}
- return [$u['host'], $uri, isset($u['port']) ? $u['port'] : null];
+ return [$u['scheme'], $u['host'], $uri, isset($u['port']) ? $u['port'] : null];
}
}
class HTTPClientConnection extends NetworkClientConnection {
View
@@ -157,7 +157,6 @@ public function setWatermark($low = null, $high = null) {
/**
* Called when the session constructed
- * @todo +on & -> protected?
* @return void
*/
protected function init() {}
@@ -326,10 +325,9 @@ public function unfreezeOutput($at_front = true) {
/**
* Called when the connection is ready to accept new data
- * @todo protected?
* @return void
*/
- public function onWrite() {}
+ protected function onWrite() {}
/**
* Send data to the connection. Note that it just writes to buffer that flushes at every baseloop
@@ -399,19 +397,17 @@ public function finish() {
/**
* Called when the session finished
- * @todo protected?
* @return void
*/
- public function onFinish() {
+ protected function onFinish() {
}
/**
* Called when new data received
- * @todo +on & -> protected?
* @param string New received data
* @return void
*/
- public function stdin($buf) {} // @TODO: deprecate
+ protected function stdin($buf) {} // @TODO: deprecate
/**
* Close the connection
View
@@ -428,7 +428,6 @@ public function finish($status = 0, $zombie = FALSE) {
if ($status !== -1) {
$this->postFinishHandler();
- // $status: 0 - FCGI_REQUEST_COMPLETE, 1 - FCGI_CANT_MPX_CONN, 2 - FCGI_OVERLOADED, 3 - FCGI_UNKNOWN_ROLE @todo what is -1 ? where is the constant for it?
$appStatus = 0;
if (isset($this->upstream)) {
$this->upstream->endRequest($this, $appStatus, $status);
View
@@ -18,32 +18,42 @@
protected $pid;
/**
- * @todo Add a description
+ * Is this thread shutdown?
* @var boolean
*/
protected $shutdown = false;
/**
- * @todo Add a description
+ * Is this thread terminated?
* @var boolean
*/
protected $terminated = false;
/**
- * @todo Add a description
+ * Collections of childrens
* @var array
*/
protected $collections = [];
- protected $timeouts = [];
-
+ /**
+ * Array of known signal numbers
+ * @var array
+ */
protected static $signalsno = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
];
- public $sigEvents = [];
+ /**
+ * Storage of signal handler events
+ * @var array
+ */
+ protected $sigEvents = [];
+ /**
+ * Hash of known signal [no => name, ...]
+ * @var array
+ */
public static $signals = [
SIGHUP => 'SIGHUP',
SIGSYS => 'SIGSYS',
@@ -64,22 +74,45 @@
SIGUSR2 => 'SIGUSR2',
];
+ /**
+ * Get PID of this Thread
+ * @return integer
+ */
public function getPid() {
return $this->pid;
}
+
+ /**
+ * Set ID of this Thread
+ * @param integer Id
+ * @return void
+ */
public function setId($id) {
$this->id = $id;
}
+ /**
+ * Get ID of this Thread
+ * @return integer
+ */
public function getId() {
return $this->id;
}
+ /**
+ * Is this thread terminated?
+ * @return boolean
+ */
public function isTerminated() {
return $this->terminated;
}
+ /**
+ * Invoke magic method
+ * @return void
+ */
+
public function __invoke() {
$this->run();
$this->shutdown();
@@ -108,6 +141,10 @@ protected function registerEventSignals() {
}
}
+ /**
+ * Unregister signals.
+ * @return void
+ */
protected function unregisterSignals() {
foreach ($this->sigEvents as $no => $ev) {
$ev->free();
@@ -133,10 +170,10 @@ public function eventSighandler($fd, $arg) {
protected function run() { }
/**
- * @todo Add a description
+ * If true, we do not register signals automatically at start
* @var boolean
*/
- public $delayedSigReg = FALSE;
+ protected $delayedSigReg = false;
/**
* Registers signals
@@ -388,7 +425,6 @@ protected function sigwait($sec = 0, $nano = 0.3e9) {
}
if (!function_exists('pcntl_sigtimedwait')) {
- // @todo $signals or Thread::$signals?
function pcntl_sigtimedwait($signals, $siginfo, $sec, $nano) {
pcntl_signal_dispatch();

0 comments on commit 32855fe

Please sign in to comment.