Esempio n. 1
0
 /**
  * socket输出接口
  *	将内部数据同步到外部服务器
  */
 public static function soutput()
 {
     echo microtime() . "<br>";
     $loger = new Watt_Log_File('syncsocket');
     //访问权限判断
     if (!Watt_Sync_MessageListManage::interfaceAuth('InitiativeOutput')) {
         $loger->log('没有访问socket输出接口的权限');
         exit;
     }
     //判断是否锁定
     if (Watt_Sync_MessageListManage::isLock('soutput')) {
         //如果锁定
         $loger->log('socket输出接口已锁定');
         exit;
     }
     //锁定接口
     Watt_Sync_MessageListManage::AddLock('soutput');
     //------------外部unknown中的数据处理  start---------------//
     //------------外部unknown中的数据处理  end---------------//
     //------------外部消息序列的处理  start---------------//
     //获取所有的消息序列
     $msgListArray = Watt_Sync_MessageListManage::getAllMessageList();
     $msgList_execute = array();
     $num_del = Watt_Sync_MessageListManage::getNumOnceDel();
     //一次删除消息序列的个数
     if (is_array($msgListArray) && count($msgListArray)) {
         foreach ($msgListArray as $key => $val) {
             //--------------------- 数据过滤器  start---------------------//
             //根据创建消息序列的服务器类型获取过滤器
             $filterNameList = Watt_Sync_MessageListManage::getMsgListFilter($val['syncServerType']);
             //调用过滤器过滤数据
             if (is_array($filterNameList) && count($filterNameList)) {
                 foreach ($filterNameList as $filterName) {
                     $aFilter = Watt_Sync_Filter::filterFactory($filterName);
                     if ($aFilter) {
                         $val = $aFilter->filter($val);
                     }
                 }
             }
             //如果字段为空,删除消息序列
             if (($val['operate'] == 'UPDATE' || $val['operate'] == 'INSERT') && !count($val['cols']) || !count($val)) {
                 //删除序列
                 Watt_Sync_MessageListManage::delMessageList($key);
                 continue;
             }
             //--------------------- 数据过滤器  end---------------------//
             if (count($msgList_execute) < $num_del - 1) {
                 $msgList_execute[$key] = $val;
             } else {
                 //满足个数
                 $msgList_execute[$key] = $val;
                 $msgSignArr = array_keys($msgList_execute);
                 $data_str = '';
                 $data = array();
                 $data = array('msgList' => $msgList_execute, 'msgListDir' => 'unknown', 'validatePassword' => Watt_Sync_MessageListManage::getValidatePassword());
                 $data_str = urlencode(serialize($data));
                 //汇总文件内容
                 $msglistfilesize = strlen($data_str);
                 //汇总文件大小
                 //生成本地消息序列汇总文件
                 $re = Watt_Sync_MessageListManage::createdMsgListFileSync($data_str);
                 if ($re != '1') {
                     $loger->log('socket输出接口创建内部消息序列汇总文件失败');
                     //解除锁定
                     Watt_Sync_MessageListManage::DelLock('soutput');
                     exit;
                 }
                 $dd_chunk = str_split($data_str, 1000);
                 $count_arr = count($dd_chunk);
                 //总块数
                 foreach ($dd_chunk as $key => $val) {
                     //循环传输数据
                     $order_str = "send_" . ($key + 1) . "_" . $count_arr;
                     $count = strlen($order_str);
                     $order_str = str_split($order_str, 1);
                     $temp_arr = array_fill($count, 20 - $count, ' ');
                     $order_str_arr = array_merge($order_str, $temp_arr);
                     $order_str = '';
                     foreach ($order_str_arr as $k => $v) {
                         $order_str .= $v;
                     }
                     $data = $order_str . $val;
                     $result = Watt_Sync_SyncSocket::syncInterface('send', $data);
                     if ($result != '1') {
                         $loger->log('socket输出接口传送消息序列汇总文件失败');
                         //解除锁定
                         Watt_Sync_MessageListManage::DelLock('soutput');
                         exit;
                     }
                 }
                 //获取外部消息序列汇总文件的大小,比较内外汇总文件
                 //删除内部消息序列汇总文件
                 $result = Watt_Sync_MessageListManage::delFile('', 'msgListfile.msg');
                 if ($result != '1') {
                     $loger->log('socket输入接口删除本地sync目录消息序列汇总文件失败');
                     //解除锁定
                     Watt_Sync_MessageListManage::DelLock('soutput');
                     exit;
                 }
                 //解析外部消息序列汇总文件成消息序列
                 $msgsignstr = Watt_Sync_SyncSocket::syncInterface('execmsgfile', '');
                 if ($result == '0') {
                     $loger->log('socket输出接口解析外部消息序列汇总文件失败');
                     //解除锁定
                     Watt_Sync_MessageListManage::DelLock('soutput');
                     exit;
                 }
                 //删除内部消息序列
                 $msgsign = unserialize(urldecode($msgsignstr));
                 if (is_array($msgsign) && count($msgsign)) {
                     $re = Watt_Sync_MessageListManage::delMessageList($msgsign);
                     if ($re != '1') {
                         $loger->log('socket输出接口删除内部消息序列失败');
                         //解除锁定
                         Watt_Sync_MessageListManage::DelLock('soutput');
                         exit;
                     }
                 } else {
                     $loger->log('socket输出接口内部消息序列数据错误');
                     //解除锁定
                     Watt_Sync_MessageListManage::DelLock('soutput');
                     exit;
                 }
                 //移动外部消息序列到outsite目录
                 $result = Watt_Sync_SyncSocket::syncInterface('movemsglistoutsite', $msgsign);
                 if ($result != '1') {
                     $loger->log('socket输出接口移动外部消息序列到outsite目录失败');
                     //解除锁定
                     Watt_Sync_MessageListManage::DelLock('soutput');
                     exit;
                 }
                 //执行外部outsite目录下的消息序列
                 /* $result = Watt_Sync_SyncSocket::syncInterface('execmsglist','');
                 			if ($result!='1'){
                 				$loger->log( 'socket执行外部outsite目录下的消息序列' );
                 				//解除锁定
                 				Watt_Sync_MessageListManage::DelLock('soutput');
                 				exit;
                 			}*/
                 // exit;
                 $msgList_execute = array();
             }
         }
         //--- 未满个数end
         //$msgList_execute[$key] = $val;
         $msgSignArr = array_keys($msgList_execute);
         $data_str = '';
         $data = array();
         $data = array('msgList' => $msgList_execute, 'msgListDir' => 'unknown', 'validatePassword' => Watt_Sync_MessageListManage::getValidatePassword());
         $data_str = urlencode(serialize($data));
         //汇总文件内容
         $msglistfilesize = strlen($data_str);
         //汇总文件大小
         //生成本地消息序列汇总文件
         $re = Watt_Sync_MessageListManage::createdMsgListFileSync($data_str);
         if ($re != '1') {
             $loger->log('socket输出接口创建内部消息序列汇总文件失败');
             //解除锁定
             Watt_Sync_MessageListManage::DelLock('soutput');
             exit;
         }
         $dd_chunk = str_split($data_str, 1000);
         $count_arr = count($dd_chunk);
         //总块数
         foreach ($dd_chunk as $key => $val) {
             //循环传输数据
             $order_str = "send_" . ($key + 1) . "_" . $count_arr;
             $count = strlen($order_str);
             $order_str = str_split($order_str, 1);
             $temp_arr = array_fill($count, 20 - $count, ' ');
             $order_str_arr = array_merge($order_str, $temp_arr);
             $order_str = '';
             foreach ($order_str_arr as $k => $v) {
                 $order_str .= $v;
             }
             $data = $order_str . $val;
             $result = Watt_Sync_SyncSocket::syncInterface('send', $data);
             if ($result != '1') {
                 $loger->log('socket输出接口传送消息序列汇总文件失败');
                 //解除锁定
                 Watt_Sync_MessageListManage::DelLock('soutput');
                 exit;
             }
         }
         //获取外部消息序列汇总文件的大小,比较内外汇总文件
         //删除内部消息序列汇总文件
         $result = Watt_Sync_MessageListManage::delFile('', 'msgListfile.msg');
         if ($result != '1') {
             $loger->log('socket输入接口删除本地sync目录消息序列汇总文件失败');
             //解除锁定
             Watt_Sync_MessageListManage::DelLock('soutput');
             exit;
         }
         //解析外部消息序列汇总文件成消息序列
         $msgsignstr = Watt_Sync_SyncSocket::syncInterface('execmsgfile', '');
         if ($result == '0') {
             $loger->log('socket输出接口解析外部消息序列汇总文件失败');
             //解除锁定
             Watt_Sync_MessageListManage::DelLock('soutput');
             exit;
         }
         //删除内部消息序列
         $msgsign = unserialize(urldecode($msgsignstr));
         if (is_array($msgsign) && count($msgsign)) {
             $re = Watt_Sync_MessageListManage::delMessageList($msgsign);
             if ($re != '1') {
                 $loger->log('socket输出接口删除内部消息序列失败');
                 //解除锁定
                 Watt_Sync_MessageListManage::DelLock('soutput');
                 exit;
             }
         } else {
             $loger->log('socket输出接口内部消息序列数据错误');
             //解除锁定
             Watt_Sync_MessageListManage::DelLock('soutput');
             exit;
         }
         //移动外部消息序列到outsite目录
         $result = Watt_Sync_SyncSocket::syncInterface('movemsglistoutsite', $msgsign);
         if ($result != '1') {
             $loger->log('socket输出接口移动外部消息序列到outsite目录失败');
             //解除锁定
             Watt_Sync_MessageListManage::DelLock('soutput');
             exit;
         }
         //执行外部outsite目录下的消息序列
         /* $result = Watt_Sync_SyncSocket::syncInterface('execmsglist','');
         			if ($result!='1'){
         				$loger->log( 'socket执行外部outsite目录下的消息序列' );
         				//解除锁定
         				Watt_Sync_MessageListManage::DelLock('soutput');
         				exit;
         			}*/
         // exit;
         //---未满个数 end
     }
     //------------外部消息序列的处理  end---------------//
     //解除锁定
     Watt_Sync_MessageListManage::DelLock('soutput');
     //$end_time = time();
     echo '<br>' . microtime();
 }
Esempio n. 2
0
 public function ready1()
 {
     while (true) {
         $msg = socket_accept($this->sock);
         // 接受一个SOCKET
         if (false === $msg) {
             throw new Exception("socket_accept() failed: " . socket_strerror(socket_last_error($msg)));
             break;
         }
         $dd = parent::read();
         socket_write($msg, $dd, strlen($dd));
         $dd = trim(socket_read($msg, 1024));
         $order_str = substr($dd, 0, 20);
         $order_str = strtoupper(trim($order_str));
         $order_arr = split('_', $order_str);
         ob_start();
         print "<pre>\r\n";
         print_r($order_arr);
         print "</pre>\r\n";
         $fp = fopen('testzfb/ztorder.txt', 'wb');
         fwrite($fp, ob_get_contents());
         fclose($fp);
         ob_end_clean();
         if ($order_arr['0'] == 'SEND') {
             //写文件
             $data_str = substr($dd, 20);
             $re = Watt_Sync_MessageListManage::createdMsgListFile($data_str, $order_arr[1]);
             if ($re == '1') {
                 $writer = '1';
             } else {
                 $writer = '0';
             }
         } else {
             if ($order_arr['0'] == "EXECMSGFILE") {
                 //解析msgLIst
                 $msgsignarr = Watt_Sync_MessageListManage::execMsgListFile();
                 if (is_array($msgsignarr) && count($msgsignarr)) {
                     $writer = urlencode(serialize($msgsignarr));
                 } else {
                     $writer = '0';
                 }
             } else {
                 if ($order_arr['0'] == "MOVEMSGLIST") {
                     //移动msgLIst
                     $dir = $order_arr['1'];
                     if ($dir == 'OUTSITE') {
                         $msgsign_str = substr($dd, 20);
                         $msgsignarr = unserialize(urldecode($msgsign_str));
                         // 移动msgLIst从unknown到outsite
                         $result = Watt_Sync_MessageListManage::moveMsglistUnknown($msgsignarr);
                         if ($result == '1') {
                             $writer = '1';
                         } else {
                             $writer = '0';
                         }
                     }
                 } else {
                     if ($order_arr['0'] == "EXECMSGLIST") {
                         //执行外部outsite目录下的msgLIst
                         $result = Watt_Sync_MessageListManage::execMsgList();
                         if ($result == '1') {
                             $writer = '1';
                         } else {
                             $writer = '0';
                         }
                     } else {
                         if ($order_arr['0'] == "GETORDER") {
                             //向外部发送获取消息序列的命令(在外部sync目录下创建msgListfile.msg文件存放指定数目的消息序列)
                             $re = Watt_Sync_MessageListManage::createLocalMsgListFile();
                             if (!$re) {
                                 $writer = '0';
                             } else {
                                 $writer = $re;
                             }
                         } else {
                             if ($order_arr['0'] == "GET") {
                                 //向外部获取消息序列指定块的的文件内容
                                 $re = Watt_Sync_MessageListManage::getLocalMsgListFileCbyNum($order_arr['1'], $order_arr['2']);
                                 if (!$re) {
                                     $writer = '0';
                                 } else {
                                     $writer = $re;
                                 }
                             } else {
                                 if ($order_arr['0'] == "DEL") {
                                     //删除外部消息序列汇总文件
                                     $re = Watt_Sync_MessageListManage::delLocalMsgListFile();
                                     $writer = $re;
                                 } else {
                                     if ($order_arr['0'] == "DELMSGLIST") {
                                         //删除外部消息序列汇总文件msglist
                                         $dir = $order_arr['1'];
                                         $msgsign_str = substr($dd, 20);
                                         $msgsignarr = unserialize(urldecode($msgsign_str));
                                         if ($dir == 'SYNC') {
                                             $re = Watt_Sync_MessageListManage::delMessageList($msgsignarr);
                                             if ($re == '1') {
                                                 $writer = $re;
                                             } else {
                                                 $writer = '0';
                                             }
                                         } else {
                                             if ($dir == '') {
                                             }
                                         }
                                         $writer = $re;
                                     } else {
                                         if ($order_arr['0'] == 'QUIT') {
                                             $writer = $order_arr['0'];
                                             socket_write($msg, $writer, strlen($writer));
                                             socket_close($msg);
                                             break;
                                         } else {
                                             if ($order_arr['0'] == 'TEST') {
                                                 $str = substr($dd, 20);
                                                 $writer = urldecode($str);
                                             } else {
                                                 $writer = '0';
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         /* $dd = unserialize(urldecode($dd));		    
            if (is_array($dd) && count($dd)){
            	//读取数组中的
            	if ($dd['interfacetype'] =='PassiveInput')
            	{//被动输入
            	*/
         /*数据格式如下
         		Array
         		(
         		    [obj] => file
         		    [msgList] => Array
         		        (
         		            [118181050416491200] => Array
         		                (
         		                    [operate] => INSERT
         		                    [tableName] => tpm_xiangmu2gaojian
         		                    [cols] => Array
         		                        (
         		                            [XM_ID] => '75be3eaf-10a8-773b-1f74-4670b1a30499'
         		                            [GJ_ID] => 'afa8d118-b811-749f-36c0-4670ff1a2141'
         		                        )
         		                    [syncServerType] => INSIDE_TPM
         		                )
         		            [118181899652933200] => Array
         		                (
         		                    [operate] => INSERT
         		                    [tableName] => tpm_dingdan2gaojian
         		                    [cols] => Array
         		                        (
         		                            [DD_ID] => '23b3f204-1df7-65f3-aaa6-46711e610de9'
         		                            [GJ_ID] => '53d3045c-b45f-ea4b-39f3-4671204d681e'
         		                        )
         		                    [syncServerType] => INSIDE_TPM
         		                )
         		        )
         		    [msgListDir] => unknown
         		    [msgSign] => 
         		    [validatePassword] => 123456
         		)
         		*/
         /*	$result=Watt_Sync_MessageListManage::createAllMsgList($dd['msgList'],$dd['msgListDir']);
           		//$result=Watt_Sync_MessageListManage::createAllMsgList($dd['msgList'],'outside');
           		if ($result=='1'){
             	$writer = '1';
             }else {
             	$writer = '0';
             }
           	}else if ($dd['interfacetype'] =='PassiveOutput')
           	{//被动输出
           		*/
         /*
         		    		Array('interfacetype' => 'PassiveOutput',
         		 'validatePassword' => '123456'
         		);
         */
         /*$msgAll = Watt_Sync_MessageListManage::getAllMessage();
         		$writer = urlencode(serialize($msgAll)); 		    
            	}
            }else {
            	 $writer = 'failed';
            }*/
         socket_write($msg, $writer, strlen($writer));
         socket_close($msg);
         // $command=strtoupper("quit");
         // if ($command == "QUIT")
         // 	break;
     }
     parent::close();
 }