Skip to content

Commit

Permalink
Adding in a beta UPnP mythweb branch
Browse files Browse the repository at this point in the history
(cherry picked from commit e1dfaab5a10b8fdd3d9c52aaeb296ee67764c05e)
  • Loading branch information
kormoc committed Dec 23, 2010
1 parent d0671d3 commit 0133b7e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions classes/MythBackend.php
Expand Up @@ -30,6 +30,10 @@ class MythBackend {
private $port_http = null; private $port_http = null;


static function find($host = null, $port = null) { static function find($host = null, $port = null) {
$ip = UPnP_Client::discoverIps('urn:schemas-mythtv-org:device:MasterMediaServer:1');
var_dump($ip);
die();

static $Backends = array(); static $Backends = array();


// Looking for the master backend? // Looking for the master backend?
Expand Down
58 changes: 58 additions & 0 deletions classes/UPnP/Client.php
@@ -0,0 +1,58 @@
<?php

class UPnP_Client {
static $ip = '239.255.255.250';
static $port = '1900';
static $timeout = 3;
static $maxPacketSize = 10240;

static function discover($schema = null) {
}

static function discoverIps($schema = null, $ipCount = 1) {
$ips = self::discoverRaw($schema, $ipCount);
var_dump($ips);
if ($ipCount == 1)
return substr($ips[0]['peer'], 0, strpos($ips[0]['peer'], ':'));
$ret = array();
return $ret;
}

static function discoverRaw ($schema = null, $count = null) {
// Prep to receive UPnP data
$socket = stream_socket_server('udp://0.0.0.0:1900', $errno, $errstr, STREAM_SERVER_BIND);
if (!$socket) die("$errstr ($errno)");
$write = stream_socket_client('udp://239.255.255.250:1900', $errno, $errstr);
if (!$write) die("$errstr ($errno)");

// Send a discovery
$out = "M-SEARCH * HTTP/1.1\r\n";
$out .= "Host: 239.255.255.250:1900\r\n";
$out .= "ST:$schema\r\n";
$out .= "Man:\"ssdp:discover\"\r\n";
$out .= "MX:".self::$timeout."\r\n";
$out .= "\r\n";
fwrite($write, $out);

// Await replies
$devices = array();
$startTime = time();
while (time() - $startTime < self::$timeout) {
$read = array($socket);
$write = array();
$except = array();
$pending = stream_select($read, $write, $except, 0, 0);
if ( $pending > 0 ) {
$pkt = stream_socket_recvfrom($socket, self::$maxPacketSize, 0, $peer);
if ($pkt !== false) {
$devices[] = array('peer' => $peer, 'pkt' => $pkt);
if (!is_null($count) && $count >= count($devices))
break;
}
}
usleep(100000);
}
stream_socket_shutdown($socket, STREAM_SHUT_RDWR);
return $devices;
}
}
16 changes: 16 additions & 0 deletions classes/UPnP/Device.php
@@ -0,0 +1,16 @@
<?php

class UPnP_Device {
private $data = '';


function __construct($connection) {
while (!feof($connection))
$this->data .= fgets($connection);
fclose($connection);
}

function getData() {
return $this->data;
}
}

0 comments on commit 0133b7e

Please sign in to comment.