public function onFileChanged($path) { if (!Daemon::lintFile($path)) { Daemon::log(__METHOD__ . ': Detected parse error in ' . $path); return; } foreach ($this->files[$path] as $k => $subscriber) { if (is_callable($subscriber) || is_array($subscriber)) { call_user_func($subscriber, $path); continue; } if (!isset(Daemon::$process->workers->threads[$subscriber])) { unset($this->files[$path][$k]); continue; } $worker = Daemon::$process->workers->threads[$subscriber]; if (Daemon::$config->autoreimport->value) { if ($worker->connection) { $worker->connection->sendPacket(array('op' => 'importFile', 'path' => $path)); } } else { $worker->signal(SIGUSR2); } } }
/** * Called when the worker is ready to go * * @return void */ public function onReady() { // Adding listener // ComplexJob - STATE_WAITING $job = new ComplexJob(function ($job) { // ComplexJob - STATE_DONE /*array ( 'bar' => array ( 'job' => 'bar', 'success' => false, 'line' => 63, ), 'foo' => array ( 'job' => 'foo', 'success' => true, 'line' => 84, 'arg' => array ( 'param' => 'value', ), ), 'baz' => array ( 'job' => 'baz', 'success' => false, 'line' => 94, ), )*/ Daemon::log($job->results); }); // Adding listener // ComplexJob - STATE_WAITING $job->addListener(function ($job) { // ComplexJob - STATE_DONE }); // Incapsulate some property in job $job->appInstance = $this; // Adding async job foo $job('foo', $this->foo(array('param' => 'value'))); // Adding with 1 sec delay Timer::add(function ($event) use($job) { // Adding async job bar $job('bar', function ($jobname, $job) { Timer::add(function ($event) use($jobname, $job) { // Job done $job->setResult($jobname, array('job' => 'bar', 'success' => false, 'line' => __LINE__)); $event->finish(); }, 1000.0 * 50); }); // Adding async job baz. Equal $job('baz', $job->appInstance->baz()); $job->addJob('baz', $job->appInstance->baz()); // Run jobs. All listeners will be called when the jobs done // ComplexJob - STATE_RUNNING $job(); $event->finish(); }, 1000000.0 * 1); }
/** * Called when new frame received. * @param string Frame's contents. * @param integer Frame's type. * @return void */ public function onFrame($data, $type) { if ($data === 'ping') { $this->client->sendFrame('pong', WebSocketSERVER::STRING, function ($client) { Daemon::log('ExampleWebSocket: \'pong\' received by client.'); }); } }
public function stdin($buf) { // from mysqld to client. if (Daemon::$settings['mod' . $this->appInstance->modname . 'protologging']) { Daemon::log('MysqlProxy: Server --> Client: ' . Daemon::exportBytes($buf) . "\n\n"); } $this->downstream->write($buf); }
/** * Called when new data received * @param string New data * @return void */ public function stdin($buf) { Daemon::log(Debug::exportBytes($buf)); while ($c = array_pop($this->callbacks)) { list($cb, $st) = $c; $cb(microtime(true) - $st); } }
public function init() { Daemon::$settings += array('mod' . $this->modname . 'listen' => 'tcp://0.0.0.0', 'mod' . $this->modname . 'listenport' => 23, 'mod' . $this->modname . 'enable' => 0); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $this->bindSockets(Daemon::$settings['mod' . $this->modname . 'listen'], Daemon::$settings['mod' . $this->modname . 'listenport']); } }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'listen' => 'tcp://0.0.0.0', 'mod' . $this->modname . 'listenport' => 8818, 'mod' . $this->modname . 'passphrase' => 'secret', 'mod' . $this->modname . 'enable' => 0)); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $this->bindSockets(Daemon::$settings['mod' . $this->modname . 'listen'], Daemon::$settings['mod' . $this->modname . 'listenport']); } }
public function init() { Daemon::$settings += array('mod' . $this->modname . 'listen' => 'tcp://0.0.0.0', 'mod' . $this->modname . 'listenport' => 1080, 'mod' . $this->modname . 'auth' => 0, 'mod' . $this->modname . 'username' => 'User', 'mod' . $this->modname . 'password' => 'Password', 'mod' . $this->modname . 'enable' => 0); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $this->bindSockets(Daemon::$settings['mod' . $this->modname . 'listen'], Daemon::$settings['mod' . $this->modname . 'listenport'], TRUE); } }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'listen' => 'tcp://0.0.0.0', 'mod' . $this->modname . 'listenport' => 843, 'mod' . $this->modname . 'file' => Daemon::$dir . '/conf/crossdomain.xml', 'mod' . $this->modname . 'enable' => 0)); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $this->bindSockets(Daemon::$settings['mod' . $this->modname . 'listen'], Daemon::$settings['mod' . $this->modname . 'listenport']); $this->update(); } }
/** * Called when new frame received. * @param string Frame's contents. * @param integer Frame's type. * @return void */ public function onFrame($data, $type) { if ($data === 'ping') { $this->client->sendFrame('pong', 'STRING', function ($client) { // optional. called when the frame is transmitted to the client Daemon::log('ExampleWebSocket: \'pong\' received by client.'); }); } }
public static function init() { if (!(self::$supported = extension_loaded('eio'))) { Daemon::log('FS: missing pecl-eio, Filesystem I/O performance compromised. Consider installing pecl-eio.'); return; } self::$fdCache = new CappedCacheStorageHits(self::$fdCacheSize); eio_init(); }
/** * Adds a route if it doesn't exist already. * @param string Route name. * @param mixed Route's callback. * @return boolean Success. */ public function addRoute($route, $cb) { if (isset($this->routes[$route])) { Daemon::log(__METHOD__ . ' Route \'' . $route . '\' is already defined.'); return false; } $this->routes[$route] = $cb; return true; }
/** * Returns a proxy callback function with logging for debugging purposes * @param callable $cb Callback * @param mixed $name Data * @return callable */ public static function proxy($cb, $name = null) { static $i = 0; $n = ++$i; Daemon::log('Debug::proxy #' . $n . ': SPAWNED (' . json_encode($name) . ')'); return function (...$args) use($cb, $name, $n) { Daemon::log('Debug::proxy #' . $n . ': CALLED (' . json_encode($name) . ')'); $cb(...$args); }; }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'dbname' => 'chat', 'mod' . $this->modname . 'adminpassword' => '', 'mod' . $this->modname . 'enable' => 0)); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $this->db = Daemon::$appResolver->getInstanceByAppName('MongoClient'); $this->dbname =& Daemon::$settings[$k = 'mod' . $this->modname . 'dbname']; $this->tags = array(); $this->minMsgInterval = 1; } }
/** * Constructor. * @return void */ public function init() { if ($this->isEnabled()) { list($class, $name) = explode(':', $this->name . ':'); if (!class_exists($class)) { Daemon::log($class . ' class not exists.'); return; } $this->pool = call_user_func(array($class, 'getInstance'), $name); } }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'listen' => 'tcpstream://127.0.0.1:844', 'mod' . $this->modname . 'enable' => 0)); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); require_once Daemon::$dir . '/lib/asyncRTEPclient.class.php'; $this->client = new AsyncRTEPclient(); $this->client->addServer(Daemon::$settings[$k = 'mod' . $this->modname . 'addr']); $this->client->trace = TRUE; } }
/** * Called when the worker is ready to go. * @return void */ public function onReady() { $this->redis = \PHPDaemon\Clients\Redis\Pool::getInstance(); /*$this->redis->eval("return {'a','b','c', {'d','e','f', {'g','h','i'}} }",0, function($redis) { Daemon::log(Debug::dump($redis->result)); });*/ $this->redis->subscribe('te3st', function ($redis) { Daemon::log(Debug::dump($redis->result)); }); $this->redis->psubscribe('test*', function ($redis) { Daemon::log(Debug::dump($redis->result)); }); }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'servers' => '127.0.0.1', 'mod' . $this->modname . 'port' => 11211, 'mod' . $this->modname . 'prefix' => '', 'mod' . $this->modname . 'enable' => 0)); $this->prefix =& Daemon::$settings['mod' . $this->modname . 'prefix']; if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $servers = explode(',', Daemon::$settings['mod' . $this->modname . 'servers']); foreach ($servers as $s) { $e = explode(':', $s); $this->addServer($e[0], isset($e[1]) ? $e[1] : NULL); } } }
public function init() { // fetching the last offer stamp $cursor = Database::jobs()->find(); $cursor->sort(array('stamp' => -1))->limit(1); while ($item = $cursor->getNext()) { $this->laststamp = $item['stamp']; } // initial time of last check is now $this->lastcheck = time(); // initialized. let's say about it Daemon::log('Workbreeze notifier up'); }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'servers' => '127.0.0.1', 'mod' . $this->modname . 'port' => 27017, 'mod' . $this->modname . 'enable' => 0)); $this->cache = Daemon::$appResolver->getInstanceByAppName('MemcacheClient'); if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $servers = explode(',', Daemon::$settings['mod' . $this->modname . 'servers']); foreach ($servers as $s) { $e = explode(':', $s); $this->addServer($e[0], isset($e[1]) ? $e[1] : NULL); } } }
public static function eventCall($fd, $flags, $args) { $id = $args[0]; if (!isset(Daemon::$process->timeouts[$id])) { Daemon::log(__METHOD__ . ': bad event call.'); return; } $obj = Daemon::$process->timeouts[$id]; call_user_func($obj->cb, $obj); if ($obj->finished) { unset(Daemon::$process->timeouts[$id]); } }
public function getFrameType($type) { if (is_int($type)) { return $type; } if ($type === NULL) { $type = 'STRING'; } $frametype = @constant($a = get_class($this) . '::' . $type); if ($frametype === NULL) { Daemon::log(__METHOD__ . ' : Undefined frametype "' . $type . '"'); } return $frametype; }
public function onConnected($cb) { $conn = $this; if ($conn->user !== NULL) { $this->pool->getNonce(array('dbname' => $conn->dbname), function ($result) use($conn) { $conn->appInstance->auth(array('user' => $conn->user, 'password' => $conn->password, 'nonce' => $result['nonce'], 'dbname' => $conn->dbname), function ($result) use($conn) { if (!$result['ok']) { Daemon::log('MongoClient: authentication error with ' . $conn->url . ': ' . $result['errmsg']); } }, $conn); }, $conn); } else { parent::onConnected(); } }
public function init() { Daemon::addDefaultSettings(array('mod' . $this->modname . 'servers' => '127.0.0.1', 'mod' . $this->modname . 'port' => 4730, 'mod' . $this->modname . 'enable' => 0)); if (!isset(Daemon::$settings[$k = 'mod' . $this->modname . 'enable'])) { Daemon::$settings[$k] = 0; } if (Daemon::$settings['mod' . $this->modname . 'enable']) { Daemon::log(__CLASS__ . ' up.'); $this->client = new GearmanClient(); $this->worker = new GearmanWorker(); foreach (explode(',', Daemon::$settings['mod' . $this->modname . 'servers']) as $address) { $e = explode(':', $address, 2); $this->client->addServer($e[0], isset($e[1]) ? $e[1] : Daemon::$settings['mod' . $this->modname . 'port']); $this->worker->addServer($e[0], isset($e[1]) ? $e[1] : Daemon::$settings['mod' . $this->modname . 'port']); } $this->interval = $this->pushRequest(new GearmanNodeInterval($this, $this)); } }
public function init() { $o = $this; $this->RTEPClient = Daemon::$appResolver->getInstanceByAppName('RTEPClient'); $RTEP = Daemon::$appResolver->getInstanceByAppName('RTEP'); if ($RTEP) { $RTEP->eventGroups['visitorHit'] = array(function ($session, $packet, $args = array()) { $session->addEvent('visitorHit'); }, function ($session, $packet, $args = array()) { $session->removeEvent('visitorHit'); }); } if ($this->RTEPClient && $this->RTEPClient->client) { $this->RTEPClient->client->addEventCallback('visitorHit', function ($event) use($o) { if (Daemon::$settings['logevents']) { Daemon::log('Caught event ' . $event['name'] . '.'); } ++$o->sharedcounter; }); } }
/** * @todo description is missing */ public static function eventCall($fd, $flags, $arg) { $k = $arg[0]; if (!isset(Daemon::$process->queue[$k])) { Daemon::log('Bad event call.'); return; } $r = Daemon::$process->queue[$k]; if ($r->state === Request::STATE_SLEEPING) { $r->state = Request::STATE_ALIVE; } if (Daemon::$config->logqueue->value) { Daemon::$process->log('event ' . get_class($r) . '::call() invoked.'); } $ret = $r->call(); if (Daemon::$config->logqueue->value) { Daemon::$process->log('event runQueue(): (' . $k . ') -> ' . get_class($r) . '::call() returned ' . $ret . '.'); } if ($ret === Request::STATE_FINISHED) { if (is_resource($r->ev)) { event_del($r->ev); event_free($r->ev); } unset(Daemon::$process->queue[$k]); if (isset($r->idAppQueue)) { if (Daemon::$config->logqueue->value) { Daemon::$process->log('request removed from ' . get_class($r->appInstance) . '->queue.'); } unset($r->appInstance->queue[$r->idAppQueue]); } else { if (Daemon::$config->logqueue->value) { Daemon::$process->log('request can\'t be removed from AppInstance->queue.'); } } } elseif ($ret === REQUEST::STATE_SLEEPING) { event_add($r->ev, $r->sleepTime); } }
/** * Find class * @param string $class Class * @param string $namespace Namespace * @param string $rootns Root namespace * @return string */ public static function find($class, $namespace = null, $rootns = null) { $e = explode('\\', $class); if ($e[0] === '') { return $class; } if ('Pool' === $class || 'TransportContext' === $class) { return '\\PHPDaemon\\Core\\' . $class; } if (mb_orig_strpos($class, '\\') === false && $namespace === null) { if ('Example' === substr($class, 0, 7)) { array_unshift($e, 'Examples'); } if ('Server' === substr($class, -6)) { $path = '\\PHPDaemon\\Servers\\' . substr($class, 0, -6) . '\\Pool'; $r = str_replace('\\Servers\\Servers', '\\Servers', $path); Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.'); return $r; } if ('Client' === substr($class, -6)) { $path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -6) . '\\Pool'; $r = str_replace('\\Clients\\Clients', '\\Clients', $path); Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.'); return $r; } if ('ClientAsync' === substr($class, -11)) { $path = '\\PHPDaemon\\Clients\\' . substr($class, 0, -11) . '\\Pool'; $r = str_replace('\\Client\\Clients', '\\Clients', $path); Daemon::log('ClassFinder: \'' . $class . '\' -> \'' . $r . '\', you should change your code.'); return $r; } } if ($namespace !== null && sizeof($e) < 2) { array_unshift($e, $namespace); } array_unshift($e, '\\' . ($rootns !== null ? $rootns : Daemon::$config->defaultns->value)); return implode('\\', $e); }
/** * Called when request iterated. * @return integer Status. */ public function run() { if (!$this->started) { $this->started = true; $LockClient = Daemon::$appResolver->getInstanceByAppName('LockClient'); $req = $this; $LockClient->job('ExampleJobName', false, function ($command, $jobname, $client) use($req) { if ($command === 'RUN') { Daemon_TimedEvent::add(function ($event) use($req, $jobname, $client) { Daemon::log('done'); $client->done($jobname); $req->out(':-)'); $req->wakeup(); $event->finish(); }, pow(10, 6) * 1); } else { $req->out(':-('); $req->wakeup(); } }); $this->sleep(5); //timeout } }
/** * Called when new data received * @param string New data * @return void */ public function stdin($buf) { $this->buf .= $buf; while (($l = $this->gets()) !== FALSE) { $e = explode(' ', rtrim($l, "\r\n")); if ($e[0] === 'RUN') { if (isset($this->appInstance->jobs[$e[1]])) { call_user_func($this->appInstance->jobs[$e[1]][0], $e[0], $e[1], $this->appInstance); } } elseif ($e[0] === 'DONE') { if (isset($this->appInstance->jobs[$e[1]][1])) { call_user_func($this->appInstance->jobs[$e[1]][1], $e[0], $e[1], $this->appInstance); } } elseif ($e[0] === 'FAILED') { if (isset($this->appInstance->jobs[$e[1]][2])) { call_user_func($this->appInstance->jobs[$e[1]][2], $e[0], $e[1], $this->appInstance); } } if ($this->appInstance->config->protologging->value) { Daemon::log('Lock client <-- Lock server: ' . Debug::exportBytes(implode(' ', $e)) . "\n"); } } }
public function run() { if ($this->type === 'push') { $ret = array(); $e = explode('.', self::getString($_REQUEST['_id']), 2); if (sizeof($e) != 2) { $ret['error'] = 'Bad cookie.'; } elseif (!isset($_REQUEST['data']) || !is_string($_REQUEST['data'])) { $ret['error'] = 'No data.'; } elseif ($connId = $this->appInstance->connectIPC(basename($e[0]))) { $this->appInstance->sessions[$connId]->write(pack('CCN', WebSocketOverCOMET::IPCPacketType_C2S, strlen($e[1]), strlen($_REQUEST['data'])) . $e[1]); $this->appInstance->sessions[$connId]->write($_REQUEST['data']); } else { $ret['error'] = 'IPC error.'; } echo json_encode($ret); return Request::DONE; } elseif ($this->type === 'pull') { if (!$this->inited) { $this->authKey = sprintf('%x', crc32(microtime() . "" . $this->attrs->server['REMOTE_ADDR'])); $this->header('Content-Type: text/html; charset=utf-8'); $this->inited = TRUE; $this->out('<!--' . str_repeat('-', 1024) . '->'); // Padding $this->out('<script type="text/javascript"> WebSocket.onopen("' . $this->appInstance->ipcId . '.' . $this->idAppQueue . '.' . $this->authKey . '"); </script>' . "\n"); $appName = self::getString($_REQUEST['_route']); if (!isset($this->appInstance->wss->routes[$appName])) { if (isset(Daemon::$settings['logerrors']) && Daemon::$settings['logerrors']) { Daemon::log(__METHOD__ . ': undefined route \'' . $appName . '\'.'); } return Request::DONE; } if (!($this->downstream = call_user_func($this->appInstance->wss->routes[$appName], $this))) { return Request::DONE; } } $this->sleep(1); } elseif ($this->type === 'pollInit') { if (!$this->inited) { $this->authKey = sprintf('%x', crc32(microtime() . "" . $this->attrs->server['REMOTE_ADDR'])); $this->header('Content-Type: text/plain; charset=utf-8'); $this->inited = TRUE; $appName = self::getString($_REQUEST['_route']); if (!isset($this->appInstance->wss->routes[$appName])) { if (isset(Daemon::$settings['logerrors']) && Daemon::$settings['logerrors']) { Daemon::log(__METHOD__ . ': undefined route \'' . $appName . '\'.'); } echo json_encode(array('error' => 404)); return Request::DONE; } if (!($this->downstream = call_user_func($this->appInstance->wss->routes[$appName], $this))) { echo json_encode(array('error' => 403)); return Request::DONE; } $id = $this->appInstance->ipcId . '.' . $this->idAppQueue . '.' . $this->authKey; if (isset($_REQUEST['_script'])) { $q = self::getString($_GET['q']); if (ctype_digit($q)) { $this->out('var Response' . $q . ' = ' . json_encode(array('id' => $id)) . ";\n"); } } else { echo json_encode(array('id' => $id)); } $this->atime = time(); $this->finish(0, TRUE); } if ($this->atime < time() - 30) { if (isset($this->downstream)) { $this->downstream->onFinish(); unset($this->downstream); } return 1; } $this->sleep(2); } elseif ($this->type === 'poll') { if (!$this->inited) { $this->header('Content-Type: text/plain; charset=utf-8'); $this->inited = TRUE; $ret = array(); $e = explode('.', self::getString($_REQUEST['_id']), 2); if (sizeof($e) != 2) { $ret['error'] = 'Bad cookie.'; } elseif ($connId = $this->appInstance->connectIPC(basename($e[0]))) { $body = self::getString($_REQUEST['ts']); $this->appInstance->sessions[$connId]->write(pack('CCN', WebSocketOverCOMET::IPCPacketType_POLL, strlen($e[1]), strlen($body)) . $e[1] . $body); } else { $ret['error'] = 'IPC error.'; } if (isset($req->attrs->get['_script'])) { $q = self::getString($req->attrs->get['q']); if (!ctype_digit($q)) { $ret['error'] = 'Bad q.'; } } if (sizeof($ret)) { echo json_encode($ret); return Request::DONE; } $this->reqIdAuthKey = $e[1]; $a =& $this->appInstance->polling[$this->reqIdAuthKey]; if (!isset($a)) { $a = array(); } $a[] = $this->idAppQueue; unset($a); $this->out("\n"); $this->sleep(15); } return Request::DONE; } }