[Added library for displaying media sessions for MediaProxy 2.0 Adrian Georgescu **20080529180006 ] addfile ./status/media_sessions_lib.phtml hunk ./status/media_sessions_lib.phtml 1 +userAgentImages = $userAgentImages; + + $this->filters = $filters; + $this->allowedDomains = $allowedDomains; + + list($ip,$port) = explode(":",$dispatcher); + + $this->dispatcher_ip = $ip; + + if ($port) $this->dispatcher_port = $port; + + return $this->getSessions(); + + } + + function getSessions () { + + if (!$this->dispatcher_ip) return false; + if (!$this->dispatcher_port) return false; + + if ($fp = fsockopen ($this->dispatcher_ip, $this->dispatcher_port, $errno, $errstr, $this->timeout)) { + + if (!count($this->allowedDomains)) { + fputs($fp, "summary\r\n"); + + while (!feof($fp)) { + $line = fgets($fp); + + if (preg_match("/^\r\n/",$line)) { + break; + } + + $this->relays[] = json_decode($line); + } + } + + fputs($fp, "sessions\r\n"); + + while (!feof($fp)) { + $line = fgets($fp); + + if (preg_match("/^\r\n/",$line)) { + break; + } + + $line=json_decode($line); + + if (count($this->allowedDomains)) { + list($user1,$domain1)=explode("@",$line->from_uri); + list($user2,$domain2)=explode("@",$line->to_uri); + if (!in_array($domain1,$this->allowedDomains) && !in_array($domain2,$this->allowedDomains)) { + continue; + } + } + + if (strlen($this->filters['user'])) { + $user=$this->filters['user']; + if (preg_match("/$user/",$line->from_uri) || + preg_match("/$user/",$line->to_uri) + ) { + $this->sessions[] = $line; + } + + } else { + $this->sessions[] = $line; + } + + } + + fclose($fp); + return true; + + } else { + printf ("

Error connecting to %s:%s: %s (%s) \n",$this->dispatcher_ip,$this->dispatcher_port,$errstr,$errno); + return false; + } + } + + function showSearch() { + printf ("

+ + +

+ ", + $_SERVER['PHP_SELF'], + $_REQUEST['user'] + ); + } + + function showHeader() { + print " + + + Media sessions + + + + + "; + } + + function showFooter() { + print ""; + print " + + + "; + } + + function show() { + + $this->showHeader(); + + print "

Media sessions

"; + + $this->showSearch(); + + if (!count($this->allowedDomains)) { + $this->showRelays(); + } + + $this->showSessions(); + + $this->showFooter(); + } + + function showRelays() { + + print " + + + + + + + + + + + + + + + + + + "; + + $i = 1; + + foreach ($this->relays as $relay) { + + unset($media_types); + + foreach ($relay->stream_count as $key => $value) { + $media_types++; + } + + if ($media_types > 1) { + $streams = "
AddressVersionUptimeRelayed trafficSessionsStreamsStatus
"; + + foreach ($relay->stream_count as $key => $value) { + $streams .= sprintf("",$key,$value); + } + + $streams .= "
%s%s
"; + } else { + foreach ($relay->stream_count as $key => $value) { + $streams=sprintf("%s %s",$key,$value); + } + } + + printf (" + + %d + + %s + + %s + + %s + + %s + + %d + + %s + + %s + ", + $i, + $relay->ip, + $relay->version, + $this->normalizeTime($relay->uptime), + $this->normalizeTraffic($relay->bps_relayed), + $relay->session_count, + $streams, + ucfirst($relay->status) + ); + + $i++; + } + + print " + +
+ "; + } + + function showSessions () { + print " + + + + + + + + + + + + + + + + + + + "; + + $i = 1; + foreach ($this->sessions as $session) { + $from = $session->from_uri; + $to = $session->to_uri; + $fromAgent = $session->caller_ua; + $toAgent = $session->callee_ua; + $fromImage = $this->getImageForUserAgent($fromAgent); + $toImage = $this->getImageForUserAgent($toAgent); + $sc = count($session->streams); + + print " + + + + + "; + + $duration = $this->normalizeTime($session->duration); + + foreach ($session->streams as $streamInfo) { + $status = $streamInfo->status; + + if ($status=="idle" || $status=='hold') { + $idletime = $this->normalizeTime($streamInfo->timeout_wait); + $status = sprintf("%s %s", $status, $idletime); + } + + $caller = $streamInfo->caller_remote; + $callee = $streamInfo->callee_remote; + $relay_caller = $streamInfo->caller_local; + $relay_callee = $streamInfo->callee_local; + + $codec = $streamInfo->caller_codec; + $type = $streamInfo->media_type; + + if ($caller == '?.?.?.?:?') { + $caller = '–'; // a dash + $align1 = 'center'; + } else { + $align1 = 'left'; + } + if ($callee == '?.?.?.?:?') { + $callee = '–'; // a dash + $align2 = 'center'; + } else { + $align2 = 'left'; + } + if ($codec == 'Unknown') + $codec = '–'; // a dash + if ($type == 'Unknown') + $type = '–'; // a dash + $bytes_in1 = $this->normalizeBytes($streamInfo->caller_bytes); + $bytes_in2 = $this->normalizeBytes($streamInfo->callee_bytes); + print " + + + + + + + + + + + + "; + } + $i++; + } + print " +
 CallersPhonesMedia Streams
Caller addressRelay callerRelay calleeCallee addressStatusCodecTypeDurationBytes
Caller
Bytes
Called
$i + From: $from
+ To: $to
+
+ \"$fromAgent\" + + \"$toAgent\" + $caller$relay_caller$relay_callee$callee$status$codec$type$duration$bytes_in1$bytes_in2
+
"; + + } + + function normalizeBytes($bytes) { + $mb = $bytes/1024/1024.0; + $kb = $bytes/1024.0; + if ($mb >= 0.95) { + return sprintf("%.2fM", $mb); + } else if ($kb >= 1) { + return sprintf("%.2fk", $kb); + } else { + return sprintf("%d", $bytes); + } + } + + function normalizeTime($period) { + $sec = $period % 60; + $min = floor($period/60); + $h = floor($min/60); + $min = $min % 60; + + if ($h >= 1) { + return sprintf('%dh%02d\'%02d"', $h, $min, $sec); + } else { + return sprintf('%d\'%02d"', $min, $sec); + } + } + + function normalizeTraffic($traffic) { + // input is in bytes/second + $mb = $traffic/1024/1024.0; + $kb = $traffic/1024.0; + if ($mb >= 0.95) { + return sprintf("%.2fMbps", $mb); + } else if ($kb >= 1) { + return sprintf("%.2fkbps",$kb); + } else { + return sprintf("%dbps",$traffic); + } + } + + function getImageForUserAgent($agent) { + + foreach ($this->userAgentImages as $agentRegexp => $image) { + if (preg_match("/$agentRegexp/i", $agent)) { + return $image; + } + } + + return "unknown.png"; + } +} +?>