Esempio n. 1
0
 /**
  * Opens a connection to a Tokyo Tyrant server
  * <code>
  * try {
  *     $tt = Tyrant::connect('localhost', 1978);
  * } catch (Tyrant_Exception $e) {
  *     echo $e->getMessage();
  * }
  * </code>
  *
  * @param    string  Server hostname or IP address
  * @param    string  Server port
  * @param    string  Optional existing connection id
  * @return   object  Database connection
  */
 public static function connect($host = 'localhost', $port = '1978', $id = 0)
 {
     $id = implode(':', array($host, $port, $id));
     // Check if connection already exists
     if (isset(self::$connections[$id])) {
         $connection =& self::$connections[$id];
         return $connection;
     }
     // Start a new connection
     if ($port == 0) {
         $addr = $host;
         $socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
     } else {
         $ip = gethostbyname($host);
         $addr = ip2long($ip);
         if (empty($addr)) {
             throw new Tyrant_Exception("Host not found");
         }
         $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
     }
     if (!is_resource($socket)) {
         throw new Tyrant_Exception("Connection refused");
     }
     if (!socket_connect($socket, $addr, $port)) {
         throw new Tyrant_Exception("Connection refused");
     }
     if ($port > 0) {
         if (defined('TCP_NODELAY')) {
             socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
         } else {
             // See http://bugs.php.net/bug.php?id=46360
             socket_set_option($socket, SOL_TCP, 1, 1);
         }
     }
     // Determine the database type
     if (socket_write($socket, pack('CC', 0xc8, 0x88)) === false) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     $str = '';
     if (socket_recv($socket, $str, 1, 0) === false) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     $c = unpack("C", $str);
     if (!isset($c[1]) || $c[1] !== 0) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     $str = '';
     if (socket_recv($socket, $str, 4, 0) === false) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     $num = unpack("N", $str);
     if (!isset($num[1])) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     $size = unpack("l", pack("l", $num[1]));
     $len = $size[1];
     $str = '';
     if (socket_recv($socket, $str, $len, 0) === false) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     $value = explode("\n", trim($str));
     $stats = array();
     foreach ($value as $v) {
         $v = explode("\t", $v);
         $stats[$v[0]] = $v[1];
     }
     if (!isset($stats['type'])) {
         throw new Tyrant_Exception("Unable to get database type");
     }
     // Get the right interface for the database type
     if ($stats['type'] == 'table') {
         include_once dirname(__FILE__) . '/Tyrant/RDBTable.php';
         $conn = new Tyrant_RDBTable($socket);
     } else {
         include_once dirname(__FILE__) . '/Tyrant/RDB.php';
         $conn = new Tyrant_RDB($socket);
     }
     self::$connections[$id] =& $conn;
     self::$connection = $conn;
     return $conn;
 }
Esempio n. 2
0
    forbidden();
}
$db = preg_replace('/[^a-z0-9]/i', '', $db);
try {
    $tt = @Tyrant::connect("/tmp/{$db}.sock", 0);
} catch (Tyrant_Exception $e) {
    include "../lib/spyc.php";
    if (!file_exists("../{$db}.yaml")) {
        forbidden('genome does not exist');
    }
    $genome_config = Spyc::YAMLLoad("../{$db}.yaml");
    try {
        if (!is_array($genome_config['output_tch'])) {
            throw new Exception("ttserver should be local");
        }
        $tt = @Tyrant::connect($genome_config['output_tch'][0], $genome_config['output_tch'][1]);
    } catch (Exception $e) {
        forbidden('Could not connect to ttserver');
    }
}
header('Content-Type: ' . (substr($f, -4) == ".png" ? 'image/png' : 'application/json'));
$value = $tt[$f];
if ($value === null) {
    header('HTTP/1.1 404 Not Found');
} else {
    header('Expires: ' . gmdate("D, d M Y H:i:s", time() + 60 * 60 * 3) . " GMT");
    header('Cache-Control: public, max-age=10800');
    if ($value == '-') {
        echo $tt['.null'];
    } else {
        echo $value;
Esempio n. 3
0
$genome_config = Spyc::YAMLLoad("../{$db}.yaml");
$ucsc_config = Spyc::YAMLLoad("../ucsc.yaml");
$serve_tracks = array();
foreach ($genome_config['serve_tracks'] as $trk) {
    $serve_tracks[$trk['n']] = true;
}
$query = "db={$db}&position={$pos}";
$tt = NULL;
if (isset($genome_config['search_tch'])) {
    include '../lib/Tyrant.php';
    try {
        if (is_array($genome_config['search_tch'])) {
            $tt = @Tyrant::connect($genome_config['search_tch'][0], $genome_config['search_tch'][1]);
        } else {
            $sock = preg_replace('/[^a-z0-9.]|\\.tch$/i', '', $genome_config['search_tch']);
            $tt = @Tyrant::connect("/tmp/{$sock}.sock", 0);
        }
        $value = $tt[$query];
        if ($value !== NULL) {
            echo gzinflate($value);
            exit;
        }
    } catch (Exception $e) {
        $tt = NULL;
    }
}
$url = "{$ucsc_config['browser_hosts']['local']}{$ucsc_config['browser_urls']['tracks']}?{$query}";
if (function_exists('curl_init')) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);