Browse Source

提交统计

walkor 11 years ago
parent
commit
89a9fcab00

+ 28 - 0
applications/Statistics/Modules/admin.php

@@ -0,0 +1,28 @@
+<?php
+namespace Statistics\Modules;
+function admin()
+{
+    $act = isset($_GET['act'])? $_GET['act'] : 'home';
+    switch($act)
+    {
+        case 'detect_server':
+            $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+            socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
+            $buffer = json_encode(array('cmd'=>'REPORT_IP'))."\n";
+            socket_sendto($socket, $buffer, strlen($buffer), 0, '255.255.255.255', \Statistics\Web\Config::$ProviderPort);
+            $time_start = microtime(true);
+            $time_out = 2;
+            $ip_list = array();
+            while(microtime(true) - $time_start < $time_out)
+            {
+                $buf = $host = $port = '';
+                if(socket_recvfrom($socket, $buf, 65535, 0, $host, $port))
+                {
+                    $ip_list[$host] = $host;
+                    echo $buf;
+                    var_export($ip_list);
+                }
+            }
+            break;
+    }
+}

+ 1 - 1
applications/Statistics/Modules/statistic.php

@@ -104,7 +104,7 @@ function statistic($module, $interface, $date, $start_time, $offset)
         $the_time = strtotime("-$i day");
         $the_date = date('Y-m-d',$the_time);
         $html_the_date = $date == $the_date ? "<b>$the_date</b>" : $the_date;
-        $date_btn_str .= '<a href="/?date='."$the_date&$query".'" class="btn '.$html_class.'" type="button">'.$html_the_date.'</a>';
+        $date_btn_str .= '<a href="/?fn=statistic&date='."$the_date&$query".'" class="btn '.$html_class.'" type="button">'.$html_the_date.'</a>';
         if($i == 7)
         {
             $date_btn_str .= '</br>';

+ 2 - 2
applications/Statistics/Views/log.tpl.php

@@ -18,10 +18,10 @@
 					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
 					<ul class="dropdown-menu">
 						<li>
-							<a href="#">探测节点</a>
+							<a href="/?fn=admin&act=detect_server">探测节点</a>
 						</li>
 						<li>
-							<a href="#">节点管理</a>
+							<a href="/?fn=admin">节点管理</a>
 						</li>
 					</ul>
 				</li>

+ 2 - 2
applications/Statistics/Views/main.tpl.php

@@ -18,10 +18,10 @@
 					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
 					<ul class="dropdown-menu">
 						<li>
-							<a href="#">探测节点</a>
+							<a href="/?fn=admin&act=detect_server">探测节点</a>
 						</li>
 						<li>
-							<a href="#">节点管理</a>
+							<a href="/?fn=admin">节点管理</a>
 						</li>
 					</ul>
 				</li>

+ 2 - 2
applications/Statistics/Views/statistic.tpl.php

@@ -18,10 +18,10 @@
 					 <a href="#" data-toggle="dropdown" class="dropdown-toggle">其它<strong class="caret"></strong></a>
 					<ul class="dropdown-menu">
 						<li>
-							<a href="#">探测节点</a>
+							<a href="/?fn=admin&act=detect_server">探测节点</a>
 						</li>
 						<li>
-							<a href="#">节点管理</a>
+							<a href="/?fn=admin">节点管理</a>
 						</li>
 					</ul>
 				</li>

+ 4 - 1
applications/Statistics/Web/_init.php

@@ -1,2 +1,5 @@
 <?php
-define('ST_ROOT', realpath(dirname(__FILE__) . '/..'));
+define('ST_ROOT', realpath(dirname(__FILE__) . '/..'));
+require_once ST_ROOT .'Lib/functions.php';
+require_once ST_ROOT .'Lib/Cache.php';
+require_once ST_ROOT .'Web/config.php';

+ 7 - 0
applications/Statistics/Web/config.php

@@ -0,0 +1,7 @@
+<?php
+namespace Statistics\Web;
+class Config
+{
+    // StatisticProvider 监听的端口,会向这个端口发送udp广播获取ip,然后从这个端口以tcp协议获取统计信息
+    public static $ProviderPort = 55858;
+}

+ 0 - 3
applications/Statistics/Web/index.php

@@ -1,8 +1,5 @@
 <?php
 require_once WORKERMAN_ROOT_DIR .'applications/Statistics/Web/_init.php';
-require_once WORKERMAN_ROOT_DIR .'applications/Statistics/Lib/functions.php';
-require_once WORKERMAN_ROOT_DIR .'applications/Statistics/Lib/Cache.php';
-// fn = main/statistic/log/admin
 
 $func = isset($_GET['fn']) ? $_GET['fn'] : 'main';
 $func = "\\Statistics\\Modules\\".$func;

+ 44 - 2
workers/StatisticProvider.php

@@ -56,10 +56,52 @@ class StatisticProvider extends Man\Core\SocketWorker
     protected $logDir = 'statistic/log/';
     
     /**
-     * 提供统计查询的socket
+     * 用于接收广播的udp socket
      * @var resource
      */
-    protected $providerSocket = null;
+    protected $broadcastSocket = null;
+    
+    public function onStart()
+    {
+        $listen = \Man\Core\Lib\Config::get($this->workerName . '.listen');
+        $udp_address = str_replace('tcp', 'udp', $listen);
+        $this->broadcastSocket = stream_socket_server($udp_address, $error_no, $error_msg, STREAM_SERVER_BIND);
+        $this->event->add($this->broadcastSocket,  \Man\Core\Events\BaseEvent::EV_READ, array($this, 'dealBroadcastUdp'));
+    }
+    
+    
+    /**
+     * 接收Udp数据
+     * 如果数据超过一个udp包长,需要业务自己解析包体,判断数据是否全部到达
+     * @param resource $socket
+     * @param $null_one $flag
+     * @param $null_two $base
+     * @return void
+     */
+    public function dealBroadcastUdp($socket, $null_one = null, $null_two = null)
+    {
+        $data = stream_socket_recvfrom($socket , self::MAX_UDP_PACKEG_SIZE, 0, $address);
+        // 可能是惊群效应
+        if(false === $data || empty($address))
+        {
+            return false;
+        }
+        // 解析包体
+        $data = json_decode(trim($data), true);
+        if(empty($data))
+        {
+            return false;
+        }
+        
+        // 无法解析的包
+        if(empty($data['cmd']) || $data['cmd'] != 'REPORT_IP' )
+        {
+            return false;
+        }
+        
+        // 回应
+        return stream_socket_sendto($this->broadcastSocket, json_encode(array('result'=>ok)), 0, $address);
+    }
     
     /**
      * udp 默认全部接收完毕