houxiao
2016-12-09 112c00ecb85e2f1ae16841d01949f009a709fd5e
ProxyServer/proxyserver.c
@@ -7,6 +7,9 @@
#include<dirent.h>  
#include"proxyserver.h"
#include <syslog.h>
int RequestChannel;
char* gAnalyzerIP;
char* gUser;
@@ -28,8 +31,8 @@
int ZipSerFileNum=0;
int ZipCountidx;
char NetInfo[128];
char EventString[5000 * 1024];
EVENT_LOG myEventLogs[1000 * 1024];
char EventString[1 * 1024 * 1024];
EVENT_LOG myEventLogs[2000];
int EventCount=0;
int EventCountInSearchRange=0;
@@ -44,7 +47,7 @@
#define fileread_buf 128
AlarmInfo RealAlarmEvent[16][1000];
//#define DBG_LOG
#define DBG_LOG
#define ZIP_HOME "/tmp"
/////////////////////////////////////////////////
@@ -70,7 +73,7 @@
}
char* GetDeviceInfo(char* AnalyserIP)
{
   memset(EventString,0,100000);
   memset(EventString,0,sizeof(EventString));
   gAnalyzerIP = AnalyserIP;
   if(AnalyserIP==NULL || AnalyserIP[0]==0) return "Param Error";
@@ -94,7 +97,7 @@
   length = sprintf(byte_array,"GET /PSIA/System/deviceInfo HTTP/1.1\r\nContent-Type: text/xml\r\nHost: %s\r\nContent-Length: 0\r\nConnection: Keep-Alive\r\n\r\n",AnalyserIP);       
   write(GSocket,byte_array,length);
   //   printf("%s",byte_array);
   memset(EventString,0,100000);
   memset(EventString,0,sizeof(EventString));
   while(1){
      memset(buff,0,sizeof(buff));
      length=read(GSocket,&buff,sizeof(buff));      
@@ -164,21 +167,21 @@
   return ret;
 
}
char* GetAnalyserAll()                                                                                    //9.查询设备   参数:无      返回:分析器的名称和ip
char* GetAnalyserAll()                                                                                    //9.鏌ヨ璁惧   鍙傛暟锛氭棤      杩斿洖锛氬垎鏋愬櫒鐨勫悕绉板拰ip
{
   memset(EventString,0,100000);
   memset(EventString,0,sizeof(EventString));
   int ret = Read_MysqlData();
   if(ret==0)   return    EventString;
   else  return "fail";   
}
char* GetAnalyserNet(void)                                                                                    //5.获取当前转发器的ip      参数:无。返回:当前的ip地址子网掩码和网关
char* GetAnalyserNet(void)                                                                                    //5.鑾峰彇褰撳墠杞彂鍣ㄧ殑ip      鍙傛暟锛氭棤銆傝繑鍥烇細褰撳墠鐨刬p鍦板潃瀛愮綉鎺╃爜鍜岀綉鍏?
{
   memset(NetInfo,0,128);
   /// function
   return NetInfo;
}
int SetAnalyserNet(char* NetInfo)                                                                              //6.手动设置转发器的ip      参数:ip,子网掩码,默认网关   。   返回:修改状态(成功或者失败)
int SetAnalyserNet(char* NetInfo)                                                                              //6.鎵嬪姩璁剧疆杞彂鍣ㄧ殑ip      鍙傛暟锛歩p锛屽瓙缃戞帺鐮侊紝榛樿缃戝叧   銆?杩斿洖锛氫慨鏀圭姸鎬?鎴愬姛鎴栬€呭け璐?
{
   int ret=0;
   /// function
@@ -416,7 +419,7 @@
char* GetRealEvent(char* AnalyserIP)                                             //3.查看实时监控         参数:分析器IP。返回:信道号及其对应的视频流地址
{
   int Idx=FindIdx(AnalyserIP);
   memset(EventString,0,100000);
   memset(EventString,0,sizeof(EventString));
   if(Idx>=0 && Idx<16 && RealAlarmEventNum[Idx]>0){      
      for (int i=0;i<RealAlarmEventNum[Idx];i++)
      {
@@ -457,6 +460,8 @@
//// 输出:查询结果。(包括信道号,报警时间,播放地址,等。)
char* GetPHPSearchMetaInfo(char* AnalyserIP,char* user,char* pass,int Channel,int s_year,int s_month,int s_day,int s_hour,int s_minute,int s_sec,int e_year,int e_month,int e_day,int e_hour,int e_minute,int e_sec)
{
   openlog("proxyserver", LOG_CONS | LOG_PID, 0);
   char* result;
   MyTime mstart_time;      
   mstart_time.Year=s_year;
@@ -607,7 +612,15 @@
}
void DownloadSerFile(const char* uri)
{
   return;
   char buff[1024];
   sprintf(buff, "cd " ZIP_HOME "/tmpZip/;" " curl -u supervisor:supervisor 'http://%s%s' -O" , gAnalyzerIP, uri);
   int ret = system(buff);
   printf("DownloadSerFile, ret=%d, %s\n", ret, buff);
}
//// 查询报警记录
@@ -615,6 +628,17 @@
//// 输出:查询结果。(包括信道号,报警时间,播放地址,等。)
char* GetSearchMetaInfo(char* AnalyserIP,char* user,char* pass,int Channel,lpMyTime StartTm,lpMyTime EndTm)   
{
   //reset
   memset(ZipSerFileName, 0, sizeof(ZipSerFileName));
   ZipSerFileNum=0;
   ZipCountidx;
   EventCount = 0;
   EventCountInSearchRange = 0;
   RequestChannel = -1;
   memset(myEventLogs, 0, sizeof(myEventLogs));
   memset(EventString, 0, sizeof(EventString));
   memset(byte_array, 0, sizeof(byte_array));
   RequestChannel = Channel;
   gAnalyzerIP = AnalyserIP;
   gUser = user;
@@ -623,8 +647,8 @@
   if(AnalyserIP==NULL || AnalyserIP[0]==0) return "-1";
//   if(gGettingStep>0) return -2;
   chmod(ZIP_HOME,0777);
   //delete_file(ZIP_HOME "/tmp/tmp.zip");
   //chmod(ZIP_HOME,0777);
   //delete_file(ZIP_HOME "/tmp.zip");
   //delete_file(ZIP_HOME "/tmpZip/*");
    
   struct tm ttm;
@@ -632,20 +656,20 @@
   int length;
   char* httpUri;
   char sss[24],eee[24];
   chmod("/home",0777);//#todo ZIP_HOME
   chmod(ZIP_HOME,0777);
   const char* folderr;
   const char* filename1;
   folderr="/home/tmpZip";
   filename1="/home/tmp.zip";
   folderr=ZIP_HOME "/tmpZip";
   filename1=ZIP_HOME "/tmp.zip";
   struct stat sb;
   if(stat(folderr,&sb)==0 && S_ISDIR(sb.st_mode))
      {
         delete_folder("/home/tmpZip");
      }
   if (doesFileExist(filename1))
      {
         delete_file("/home/tmp.zip");
      }
   //if(stat(folderr,&sb)==0 && S_ISDIR(sb.st_mode))
   //   {
         delete_folder(ZIP_HOME "/tmpZip");
   //   }
   //if (doesFileExist(filename1))
   //   {
         delete_file(ZIP_HOME "/tmp.zip");
   //   }
   if(RequestChannel==0) httpUri= "/PSIA/VIQ/Event/metadata/0/files";
   if(RequestChannel==1) httpUri= "/PSIA/VIQ/Event/metadata/1/files";
   if(RequestChannel==2) httpUri= "/PSIA/VIQ/Event/metadata/2/files";
@@ -670,7 +694,7 @@
   SearchEndTm -= 8 * 60 * 60;
   
#ifdef DBG_LOG
   printf("stm=%d,etm=%d\n",(int)SearchStartTm,(int)SearchEndTm);
   syslog(LOG_USER | LOG_INFO, "SearchStartTm=%d,SearchEndTm=%d\n",(int)SearchStartTm,(int)SearchEndTm);
#endif
   //for (StepTm=SearchEndTm; StepTm>SearchStartTm; StepTm-=1*3600)
@@ -680,7 +704,7 @@
      struct sockaddr_in servaddr;
      int strlen1=sizeof(servaddr);
      if((GSocket=socket(PF_INET,SOCK_STREAM,0))<0){
            perror("socket fail2");
            syslog(LOG_USER | LOG_INFO, "socket fail2");
            return "-3";
      }
      memset(&servaddr,0,strlen1);
@@ -689,7 +713,7 @@
      servaddr.sin_port=htons(80);
      if(connect(GSocket,(struct sockaddr*)&servaddr,strlen1)<0){
         printf("Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
         syslog(LOG_USER | LOG_INFO, "Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
         close(GSocket);
         return "-4";
      }
@@ -706,7 +730,7 @@
      sprintf(eee,"%d-%02d-%02dT%02d:%02d:%02d.000",t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);// = "2016-08-26T07:31:00.000";
      eee[23]=0;
#ifdef DBG_LOG
      printf("sss=%s, eee=%s\n", sss, eee);
      syslog(LOG_USER | LOG_INFO, "SearchStartTm=%s, SearchEndTm=%s\n", sss, eee);
#endif
      RequestMetaFiles(gAnalyzerIP,httpUri,sss,eee);
      write(GSocket,byte_array,strlen(byte_array));
@@ -750,7 +774,7 @@
               memcpy(buff,tmpp,SSize);
               free(tmpp);
               goto lpp123;
               printf("**********Special*******************************ZipSerFileNum = %d",ZipSerFileNum);
               syslog(LOG_USER | LOG_INFO, "**********Special*******************************ZipSerFileNum = %d",ZipSerFileNum);
            }
         }
         else{
@@ -767,7 +791,7 @@
                  free(memoryband[0]);
                  bandCount = 0;
                  bMetadataSaving = 0;
                  buff[eidx]=0;
                  buff[eidx]=0; //meta buff complete
                  int ptr=0;
                  ////// analyze buffer 
lpp123:               sidx = stringcmp((char*)&buff[ptr],"http://changeit:80/media",24);
@@ -791,6 +815,7 @@
                        char pathbuf[250] ;
                        strncpy(pathbuf,(char*)&buff[ptr+sidx+18],kkk-sidx-18+4);
                        pathbuf[kkk-sidx-18+4]=0;
                        syslog(LOG_USER | LOG_INFO, "pathbuf=%s\n", pathbuf);
                        int i;
                        for (i=0;i<ZipSerFileNum;i++)
                        {
@@ -811,15 +836,28 @@
   }//for 
   //////// get
#ifdef DBG_LOG
   syslog(LOG_USER | LOG_INFO, "ZipSerFileNum=%d\n", ZipSerFileNum);
#endif
   for(ZipCountidx = 0; ZipCountidx<ZipSerFileNum; ZipCountidx++)
   {
      if(stringcmp((char*)ZipSerFileName[ZipCountidx],".ser",4)>=0) continue;
#ifdef DBG_LOG
   syslog(LOG_USER | LOG_INFO, "ZipSerFileName[%d]=%s\n", ZipCountidx, (char*)ZipSerFileName[ZipCountidx]);
#endif
      if(stringcmp((char*)ZipSerFileName[ZipCountidx],".ser",4)>=0)
      {
         DownloadSerFile(ZipSerFileName[ZipCountidx]);
         continue;
      }
      zclock_sleep(200);
//      printf(" ==== Search loop function execution  =================%d,%d \n",(int)StepTm,(int)SearchStartTm);
      struct sockaddr_in servaddr;
      int strlen1=sizeof(servaddr);
      if((GSocket=socket(PF_INET,SOCK_STREAM,0))<0){
            perror("socket fail2");
            syslog(LOG_USER | LOG_INFO, "socket fail2");
            return "-3";
      }
      memset(&servaddr,0,strlen1);
@@ -828,13 +866,13 @@
      servaddr.sin_port=htons(80);
      if(connect(GSocket,(struct sockaddr*)&servaddr,strlen1)<0){
         printf("Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
         syslog(LOG_USER | LOG_INFO, "Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
         close(GSocket);
         return "-4";
      }
      httpUri = ZipSerFileName[ZipCountidx];//"/media/metadata/0/2016/8/25/1/2/s_5b4103706a6411e6855e574ec330e01d_20160825_013633_93003.zip";
      RequestZipSerFiles(gAnalyzerIP,httpUri);
      RequestZipSerFiles(gAnalyzerIP, httpUri);
      write(GSocket,byte_array,strlen(byte_array));
//      printf("==========================================================================================================================>>>>>>>>\n");
//      printf("%s",byte_array);
@@ -872,7 +910,10 @@
               bZipFileSaving = 0;
               close(GSocket);
               //Convert Zip file to Ser file
               system("unzip -o " ZIP_HOME "/tmp.zip -d " ZIP_HOME "/tmpZip");
               int ret=system("unzip -o " ZIP_HOME "/tmp.zip -d " ZIP_HOME "/tmpZip");
#ifdef DBG_LOG
               syslog(LOG_USER | LOG_INFO, "unzip1 ret=%d\n", ret);
#endif
            }
         }
         else{
@@ -894,7 +935,10 @@
                  bZipFileSaving = 0;
                  close(GSocket);
                  //Convert Zip file to Ser file
                  system("unzip -o " ZIP_HOME "/tmp.zip -d " ZIP_HOME "/tmpZip");
                  int ret=system("unzip -o " ZIP_HOME "/tmp.zip -d " ZIP_HOME "/tmpZip");
#ifdef DBG_LOG
               syslog(LOG_USER | LOG_INFO, "unzip0 ret=%d\n", ret);
#endif
               }
            }
         }
@@ -906,7 +950,7 @@
   char stringpath[1024];
   unsigned char p_save_buf[6144000];
   
   for(i=0;i<num;i++)
   for(int i=0;i<num;i++)
   {   
      if(filename[i].name[0]=='.') continue;
      memset(stringpath,0,1024);
@@ -922,13 +966,13 @@
   memset(EventString,0,sizeof(EventString));
   //for(i=0;i<1000;i++)
#ifdef DBG_LOG
   printf("EventCount===========================%d\n",EventCount);
   printf("EventCountInSearchRange==============%d\n",EventCountInSearchRange);
   syslog(LOG_USER | LOG_INFO, "EventCount===========================%d\n",EventCount);
   syslog(LOG_USER | LOG_INFO, "EventCountInSearchRange==============%d\n",EventCountInSearchRange);
#endif
   
   strcat(EventString,"[");
   
   for (i=0;i<EventCount;i++)
   for (int i=0;i<EventCount;i++)
   {
#ifndef DBG_LOG
      if (! myEventLogs[i].InSearchRange)
@@ -965,12 +1009,6 @@
   }
   
   strcat(EventString,"]");
   //reset
   //EventCount = 0;//#todo
   //EventCountInSearchRange = 0;
   //RequestChannel = -1;
   //memset(&myEventLogs, 0, sizeof(myEventLogs));
   return EventString;
   //return "333333333333333333333333333333333333333333333333333333333333333333789";
@@ -991,7 +1029,7 @@
   struct sockaddr_in servaddr;
   int strlen1=sizeof(servaddr);
   if((GSocket=socket(PF_INET,SOCK_STREAM,0))<0){
      perror("socket fail2");
      syslog(LOG_USER | LOG_INFO, "socket fail2");
      return -1;
   }
   memset(&servaddr,0,strlen1);
@@ -1000,7 +1038,7 @@
   servaddr.sin_port=htons(80);
   if(connect(GSocket,(struct sockaddr*)&servaddr,strlen1)<0){
      printf("Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
      syslog(LOG_USER | LOG_INFO, "Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
      close(GSocket);
      return -2;
   }
@@ -1029,10 +1067,14 @@
   }
   close(GSocket);
   if (bbbb==0) return -1;
   if (bbbb==0)
   {
      syslog(LOG_USER | LOG_INFO, "bbbb==0");
      return -1;
   }
//   zclock_sleep(200);
   if((GSocket=socket(PF_INET,SOCK_STREAM,0))<0){
      perror("socket fail2");
      syslog(LOG_USER | LOG_INFO, "socket fail2");
      return -1;
   }
   memset(&servaddr,0,strlen1);
@@ -1041,7 +1083,7 @@
   servaddr.sin_port=htons(80);
   if(connect(GSocket,(struct sockaddr*)&servaddr,strlen1)<0){
      printf("Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
      syslog(LOG_USER | LOG_INFO, "Equipment connect fail-%s:%d\n",gAnalyzerIP,80);
      close(GSocket);
      return -2;
   }
@@ -1063,9 +1105,13 @@
void RequestMetaFiles(char* AnaylyIP,char* httpUri,char* Tm1,char* Tm2)
{
#ifdef DBG_LOG
   syslog(LOG_USER | LOG_INFO, "RequestMetaFiles, httpUri=%s\n", httpUri);
#endif
   char byte_arrayxxx[1600];
   int lengthxxx = 0;
   memset(byte_array,0,1600);
   memset(byte_array,0,sizeof(byte_array));
   UrnStringToBytes(byte_arrayxxx,&lengthxxx,Tm1,Tm2);// xml  
   int iidx =0,llen=0;;
@@ -1075,11 +1121,14 @@
   llen = sprintf(&byte_array[iidx],"Host: %s:%d\r\n",AnaylyIP,80); iidx+=llen;
   llen = sprintf(&byte_array[iidx],"%s","Connection: Keep_Alive\r\n\r\n"); iidx+=llen;
   strncpy(&byte_array[iidx],byte_arrayxxx,lengthxxx); iidx+=lengthxxx;
}
void RequestZipSerFiles(char* AnaylyIP,char* httpUri)
{
   memset(byte_array,0,1600);
#ifdef DBG_LOG
   syslog(LOG_USER | LOG_INFO, "RequestZipSerFiles, httpUri=%s\n", httpUri);
#endif
   memset(byte_array,0,sizeof(byte_array));
   int iidx =0,llen=0;;
   llen = sprintf(byte_array,"GET %s %s",httpUri,"HTTP/1.1\r\n"); iidx+=llen;
@@ -1675,7 +1724,7 @@
   if((myEventLogs[EventCount].DebugMessageE[0]!=0|| myEventLogs[EventCount].DebugMessageS[0]!=0) && myEventLogs[EventCount].Rule[0]==0 && myEventLogs[EventCount].Region[0]==0){
      memcpy(ttmp,(unsigned char*)p_save_buf,5000);
   }
   if(somewhat && EventCount<200-1){///repair //#todo 12.7
   if(somewhat && EventCount<2000-1){///repair //#todo 12.7
      sprintf(myEventLogs[EventCount].Camera,"%d",RequestChannel+1);
      int i;
      for (i=0;i<EventCount;i++)
@@ -1978,14 +2027,20 @@
   char cmd[30];
   memset(cmd,0,30);
   sprintf(cmd,"rm -f %s",path);
   system(cmd);
   int ret = system(cmd);
#ifdef DBG_LOG
   syslog(LOG_USER | LOG_INFO, "delete_file, ret=%d, %s\n", ret, path);
#endif
}
void delete_folder(const char *path)
{
   char cmd[30];
   memset(cmd,0,30);
   sprintf(cmd,"rm -r %s",path);
   system(cmd);
   int ret = system(cmd);
#ifdef DBG_LOG
   syslog(LOG_USER | LOG_INFO, "delete_folder, ret=%d, %s\n", ret, path);
#endif
}
int doesFileExist(const char *filename)
{
@@ -2502,6 +2557,12 @@
  return output;
}
void zclock_sleep (int msecs)
{
    struct timespec t;
    t.tv_sec  =  msecs / 1000;
    t.tv_nsec = (msecs % 1000) * 1000000;
    nanosleep (&t, NULL);
}