public function onRequest($packet) { if (Daemon::$settings['logevents']) { Daemon::log(__METHOD__ . ': ' . Daemon::var_dump($packet)); } $request = isset($packet['request']) && is_array($packet['request']) ? $packet['request'] : array(); $opstr = isset($request['op']) ? (string) $request['op'] : ''; $e = explode('.', $opstr); $args = $e; $op = $e[0]; $out = array('id' => isset($packet['id']) ? $packet['id'] : NULL, 'response' => array('op' => $op), 'type' => 'response'); $response =& $out['response']; if ($op === 'ping') { $response['msg'] = 'pong'; } elseif (isset($this->appInstance->operations[$op])) { call_user_func_array($this->appInstance->operations[$op], array($this, $packet, $args, &$out['response'])); } elseif ($op === 'subscribeEvents') { if (!isset($request['events']) || !is_array($request['events'])) { $response['status'] = -1; } else { foreach ($request['events'] as &$evName) { $args = explode(':', $evName); $evName = $args[0]; if (isset($this->appInstance->eventGroups[$evName])) { call_user_func($this->appInstance->eventGroups[$evName][0], $this, $packet, $args); } } } $response['status'] = 1; } elseif ($op === 'unsubscribeEvents') { if (!isset($request['events']) || !is_array($request['events'])) { $response['status'] = -1; } else { foreach ($request['events'] as &$evName) { $args = explode(':', $evName); $evName = $args[0]; if (isset($this->appInstance->eventGroups[$evName])) { call_user_func($this->appInstance->eventGroups[$evName][1], $this, $packet, $args); } } } $response['status'] = 1; } elseif ($op === 'event') { $event = array('type' => 'event', 'event' => $request['event']); $this->appInstance->onEvent($event); $response['status'] = 1; } else { $reponse['error'] = 'Unrecognized operation.'; } $this->send($out); }
public function onReadEvent($stream, $arg) { $connId = is_int($arg) ? array_search($stream, Daemon::$worker->pool, TRUE) : $arg[0]; if (Daemon::$settings['logevents']) { Daemon::log('[WORKER ' . Daemon::$worker->pid . '] ' . get_class($this) . '::' . __METHOD__ . '(' . $connId . ') invoked. ' . Daemon::var_dump(Daemon::$worker->pool[$connId])); } if ($this->queuedReads) { Daemon::$worker->readPoolState[$connId] = TRUE; } $success = FALSE; if (isset($this->sessions[$connId])) { if ($this->sessions[$connId]->readLocked) { return; } while (($buf = $this->read($connId, $this->readPacketSize)) !== FALSE) { $success = TRUE; $this->sessions[$connId]->stdin($buf); } } }
public function run() { if (!$this->appInstance->cursor) { if (!$this->inited) { $req = $this; $this->inited = TRUE; $this->appInstance->cache->get('_rp', function ($answer) use($req) { $req->inited = FALSE; $e = explode(' ', $answer->result); if (isset($e[1])) { $ts = new MongoTimestamp((int) $e[0], (int) $e[1]); } else { $ts = new MongoTimestamp(0, 0); } if (Daemon::$settings['logevents']) { Daemon::log('MongoNode: replication point - ' . $answer->result . ' (' . Daemon::var_dump($ts) . ')'); } $req->appInstance->initSlave($ts); }); } } elseif (!$this->appInstance->cursor->session->busy) { if ($this->appInstance->cursor->lastOpId !== NULL) { $this->appInstance->cache->set('_rp', $this->appInstance->cursor->lastOpId); $this->appInstance->cursor->lastOpId = NULL; } try { $this->appInstance->cursor->getMore(); } catch (MongoClientSessionFinished $e) { $this->appInstance->cursor = FALSE; } } $this->sleep(0.3); }
public function onRequest($packet) { if (Daemon::$settings['logevents']) { Daemon::log(__METHOD__ . ': ' . Daemon::var_dump($packet)); } }
public function findPeers() { ++$this->jobTotal; $appInstance = $this->appInstance; $reqID = $this->idAppQueue; $req = $this; $where = array('hash' => $this->q['hash'], 'ltime' => array('$gt' => time() - 600)); if ($this->q['left'] == 0) { $where['left'] = array('$gt' => 0); } else { $where['peer'] = array('$ne' => new MongoBinData($this->q['peer'])); } // else we shall not connect this peer to himself. if (isset($_GET['debug'])) { var_dump($where); } $this->appInstance->db->btswarm->find(function ($cursor) use($appInstance, $reqID) { if (!isset($appInstance->queue[$reqID])) { return; } $req = $appInstance->queue[$reqID]; if (isset($req->attrs->request['debug'])) { Daemon::log('debug swarm - ' . Daemon::var_dump($cursor->items)); } foreach ($cursor->items as $k => $v) { Daemon::log('Peer ' . $req->q['ip'] . ':' . $req->q['port'] . ' (' . $req->q['client'] . ') recived peer ' . $v['ip'] . ':' . $v['port'] . ' (' . $v['client'] . ')'); if ($req->q['compact']) { $req->peers .= pack('Nn', ip2long($v['ip']), $v['port']); } else { $o = new stdClass(); if (!$req->q['no_peer_id']) { $o->peerid = $v['peer']; } $o->ip = $v['ip']; $o->port = $v['port']; $req->peers .= BitTorrentTracker::bencode($o); } unset($cursor->items[$k]); } if (!$cursor->finished) { $cursor->getMore(); } else { if ($req->q['compact']) { $req->peers = BitTorrentTracker::bencode($req->peers); } else { $req->peers = 'l' . $req->peers . 'e'; } ++$req->jobDone; if ($req->jobDone >= $req->jobTotal) { $req->eState = 'done'; $req->wakeup(); } $cursor->destroy(); } }, array('sort' => $req->q['superseed'] ? array('uploaded' => -1) : array('ltime' => -1), 'limit' => $req->q['numwant'] ?: 200, 'where' => $where)); }