Skip to content
Permalink
Browse files

Adding in a beta UPnP mythweb branch

(cherry picked from commit e1dfaab5a10b8fdd3d9c52aaeb296ee67764c05e)
  • Loading branch information
kormoc committed Dec 21, 2010
1 parent d0671d3 commit 0133b7e235e74eda20fc2c5c5a579e8614252a4a
Showing with 78 additions and 0 deletions.
  1. +4 −0 classes/MythBackend.php
  2. +58 −0 classes/UPnP/Client.php
  3. +16 −0 classes/UPnP/Device.php
@@ -30,6 +30,10 @@ class MythBackend {
private $port_http = 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();
// Looking for the master backend?
@@ -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;
}
}
@@ -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.
You can’t perform that action at this time.