⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ua_main.cpp

📁 改进后的SIP SERVICE API
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    if(!strcmp(argv[argi], "-mp")) {
      L_manual_provisionning = 1;
    }

    if(!strcmp(argv[argi], "-cls")) {
      G_server_close = 1;
    }

    if(!strcmp(argv[argi], "-mp2ta")) {
      L_manual_provisionning = 1;
      G_multiple_ta = 1;
    }

#ifdef __hpux
    if(!strcmp(argv[argi], "-cpu")) {
      if((++argi) < argc) {
        if( mpctl(MPC_SETPROCESS_FORCE, atol(argv[argi]), MPC_SELFPID) == -1 )
          {
            printf("ERROR: mpctl() returned error: [%d]\n", errno);
            exit(1);
          }
	
        printf("mpctl(): process was properly locked on processor [%i]\n",
               atol(argv[argi]));
      } else {
        usage(argv[0],sip_local_ip);
        exit(1);
      }
    }
#endif //__hpux
    
    if(!strcmp(argv[argi], "-mgt")) {
      if((++argi) < argc) {
        strcpy(L_className,argv[argi]);
      } else {
        usage(argv[0],sip_local_ip);
        exit(1);
      }
    }
    if(!strcmp(argv[argi], "-lb")) {
      if((++argi) < argc) {
        L_numberOfUasCopies = atol(argv[argi]);
      } else {
        usage(argv[0],sip_local_ip);
        exit(1);
      }
    }

  }
  //得到本地的server,形式为"sip:service_name@IP_address:port"
  if(!strlen(LocalServer)) {
    sprintf(LocalServer, 
            "sip:%s@%s:%d", 
            G_service_username, 
            sip_local_ip, 
            G_portNumber);
  }
  //初始化ProbeManagerName
  if(!L_ProbeManagerName) {
    L_ProbeManagerName = L_className;
  }

  // Print info to screen.
  if (G_trace == 1) {
    printf("Using management classname '%s'.\n", L_className);
  }
  //初始化LogicalEntity
  if (std::string(L_role) == "UA" )  
    L_logicalEntity = SIP_LE_UA;
  if (std::string(L_role) == "B2BProxy" )  
    L_logicalEntity = SIP_LE_B2BPROXY;
  if (std::string(L_role) == "B2BController" ) 
    L_logicalEntity = SIP_LE_B2BCONTROLLER;

  // Parse local server and overides default if necessary
  //得到本地的URI
  sip_msg_uri_t* LocalServer_Uri = sip_msg_parse_uri(LocalServer, &L_error);
  if(!LocalServer_Uri) {
    std::cout <<"Syntax error near token \'"
              <<L_error.unexpected_token 
              << "\' in request URI\n" 
              << std::endl;
    usage(argv[0],sip_local_ip) ;
  } 
  //得到连接类型
  if ((LocalServer_Uri->content.sip_url.transport_param != 0) && 
      (strcmp(LocalServer_Uri->content.sip_url.transport_param,"tcp")==0)) {
    L_trpType = SIP_TRANSPORT_TCP;
  } else {
    L_trpType = SIP_TRANSPORT_UDP;
  }

  // Convert delays to nanoseconds
  G_sleep = G_sleep * 1000000000;
  L_delayBeforeActivate = L_delayBeforeActivate * 1000000000;

  // Start service
  //建立SIP_AppProbeManager对象,参数为L_ProbeManagerName
  std::auto_ptr<SIP_AppProbeManager>  L_appProbeManager
    (new SIP_AppProbeManager((const char*)L_ProbeManagerName));
  //建立SIP_ProbeManager对象,参数为SIP_AppProbeManager对象
  std::auto_ptr<SIP_ProbeManager>     
    L_ProbeManager(new SIP_ProbeManager(*L_appProbeManager));
  std::auto_ptr<SIP_Scheduler>        
    L_sipScheduler (new SIP_Scheduler);
  
  G_jamMachineTimeout.tv_sec = 0;
  G_jamMachineTimeout.tv_usec = 100000 ;
  
  for  (int k = 0; k<NUM_OF_TH; k++)
    pthread_id[k]=0;
  
  for (int i=0;i<L_nbProbe;i++)
    {
      //定义service  名字
      std::string L_serviceName = "Service_";
      snprintf(buffer,64,"%u",i);
      L_serviceName += buffer;
      //定义Probe  的名字
      std::string L_probeName = "Server_";
      L_probeName += buffer;
      
      std::string L_ffName;
      //定义SIP_Probe  与UA_AppProbe  的对象指针
      SIP_Probe*    L_probe = 0;
      UA_AppProbe* L_appProbe = 0;
      //初始化FF  的名字
      if (!(i % 1)) {
        L_ffName=param_ff;
      } else {
        L_ffName="ua_ff";
      }

      //建立UA_Service对象,参数为L_serviceName and L_ffName
      UA_Service* L_service = new 
        UA_Service(L_serviceName.c_str(),L_ffName.c_str());
      
      misc_provision(i,
                     L_className,
                     sip_appli_name,
                     (char *)L_probeName.c_str(),
                     G_stateless_messages,
                     L_destructive_mode,
                     L_trans_deleted_mode);
      
      G_service_username = LocalServer_Uri->content.sip_url.user;

      if (L_manual_provisionning) {
	  printf("in L_manual_provisionning");
	
        char probe_name[30]; 
        
        lp_provision(i,
                     L_className,
                     sip_appli_name,
                     (char *)L_probeName.c_str(),
                     LocalServer_Uri->content.sip_url.user);

        ta_provision(i,
                     L_className,
                     sip_appli_name,
                     (char *)L_probeName.c_str(),
                     LocalServer_Uri->content.sip_url.host,
                     LocalServer_Uri->content.sip_url.port + i,
                     L_trpType);

        if(G_multiple_ta) {
          ta_provision(i,
                       L_className,
                       sip_appli_name,
                       (char *)L_probeName.c_str(),
                       LocalServer_Uri->content.sip_url.host,
                       LocalServer_Uri->content.sip_url.port + i + 10,
                       L_trpType);
        }

        L_appProbe = new UA_AppProbe(L_probeName.c_str());

      } else {
       //建立UA_AppProbe对象
       printf("sipUserInfo = %s\n",LocalServer_Uri->content.sip_url.user);
        L_appProbe = new UA_AppProbe(L_probeName.c_str(),    //AppProbe name
                                     LocalServer_Uri->content.sip_url.user,//userInfo
                                     LocalServer_Uri->content.sip_url.host,
                                     LocalServer_Uri->content.sip_url.port + i,
                                     L_trpType,//tcp or udp
                                     L_logicalEntity);//UA or B2B proxy or B2B Controller
      }
      
      printf("Trying to bind probe [%s]... ", (L_appProbe->getName()).c_str());
      fflush(stdout);
      //绑定SIP_Probe 到SAP,SAP由UA_AppProbe指定
      L_probe = L_ProbeManager->bindProbe(*L_appProbe);
      if (L_probe == 0) {
        printf("Unable to bind Probe, please check port "
               "number and error log.\n");
        return -1;
      } else {
        printf("OK\n");
      }
      
      //L_appProbe->setProbe(*L_probe);
      //SIP_Service被定义为UA_AppProbe的成员
      L_appProbe->setService(*L_service);
      //把L_probe加入序列中,select进行管理
      L_sipScheduler->addProbeToBeScheduled(*L_probe); 
      //建立多个UA_AppProbe对象
      for (int j = 1; j<L_numberOfUasCopies;j++) {
        snprintf(buffer,64,"%u",j);
        L_appProbe = new UA_AppProbe(L_probeName.c_str(),
                                     (L_probeName + buffer).c_str());

        //L_appProbe->setProbe(*L_probe);
        L_appProbe->setService(*L_service);
	
        if (L_ProbeManager->bindProbe(*L_appProbe) == 0)
          {
            std::cout << "Unable to bind additional Probe a probe  ["
                      << L_appProbe->getName()<<"]"<< std::endl;
            return -1;
          }
      }
      
      if (L_noService == false)
        {
          L_probe->serviceAttach(*L_service);
        }

      if  (L_startbusy == true)
        {
          L_probe->stateChange(SIP_PROBE_BUSY);
        }
      else
        {
          L_probe->stateChange(SIP_PROBE_INACTIVE);
	   printf("stateChange SIP_PROBE_INACTIVE\n");
        }

      L_probeTab.push_back(L_appProbe);

    }
  //select系列函数
  time_v = gethrtime();
  hrtime_t L_startTime = gethrtime();
  hrtime_t L_timeBeforeSelect = 0;
  hrtime_t L_timeAfterSelect = 0;
  //主循环
  while (G_nfinishedCalls < L_nbcall)
    {
      int GI_maxFds;
      fd_set E_fdsread;
      fd_set E_fdswrite;
      fd_set E_fdsexcept; 
      timeval L_timeout;
	  
      int L_nb =0;

      L_timeout.tv_sec = 5;
      L_timeout.tv_usec = 0;
      FD_ZERO(&E_fdsread);
      FD_ZERO(&E_fdswrite);
      FD_ZERO(&E_fdsexcept);
      GI_maxFds = 0;
      GI_maxFds = L_sipScheduler->selectMasks(GI_maxFds, 
                                              &E_fdsread,
                                              &E_fdswrite,
                                              &E_fdsexcept,
                                              &L_timeout);

      if (GI_maxFds < 0)
        {
          printf("SelectMaks Failure\n");
          return -1;
        } 
        
      L_timeAfterSelect = gethrtime();
      if ((L_timeAfterSelect - L_timeBeforeSelect)< L_selectMinimalDelay)
        {
          L_timeBeforeSelect = gethrtime();
          select(0,0,0,0,&L_selectMinimalDelayForSelect);
        }
      else
        {
          L_timeBeforeSelect = gethrtime();  
          L_nb = select(GI_maxFds+1, 
                        &E_fdsread, 
                        &E_fdswrite,
                        &E_fdsexcept, 
                        &L_timeout);

          if (L_nb < 0) {
            printf( "Select Failure, errno: %d\n", errno);
            return -1;
          }
          L_sipScheduler->connectionHandler(L_nb, 
                                            &E_fdsread,
                                            &E_fdswrite,
                                            &E_fdsexcept,
                                            &L_timeout);
            
        }
        
      if ((gethrtime() - L_startTime) >= L_delayBeforeActivate)
        {
          UA_AppProbe* L_appProbe = L_probeTab[0];
          if (L_appProbe->getState() != SIP_PROBE_ACTIVE)
            L_appProbe->getProbe().stateChange(SIP_PROBE_ACTIVE);
            
            
          if (VB_client_mode) 
            {
              VB_client_mode = 0;
              if(G_cps) {
                // Multithreaded client mode
                if (pthread_id[0] == 0 )
                  for (int l = 0; l< L_nbSenderThread; l++)
                    {
                      if (pthread_create
                          (pthread_id + l,
                           NULL,
                           (void *(*)(void *)) jamMachine,
                           (void*)&G_blockingMode) 
                          == -1)
                        std::cout <<"Creation of the thread ["
                                  << pthread_id[l] 
                                  <<"] has failed\n"
                                  <<std::endl;
                      else
                        std::cout <<"Creation of the thread ["
                                  << pthread_id[l] 
                                  <<"] succeded\n"
                                  <<std::endl;
                    }
			
              } else {
          
                // Single client mode with one call
          
                if (L_appProbe->getState() != SIP_PROBE_ACTIVE)
                  L_appProbe->getProbe().stateChange(SIP_PROBE_ACTIVE);
          
                if(!G_stateless_bye) {
		    //发起呼叫
                  L_appProbe->initOrReinitSession(L_target,//called URI
                                                  &L_appProbe->getService(), //SIP_Service对象
                                                  NULL);
                } else {
                  L_appProbe->statelessRequest(L_target,
                                               &L_appProbe->getService(),
                                               SIP_MSG_BYE,
                                               NULL,
                                               (char *)"stateless_request");
                }
          
                if (G_trace == 1) {
                  std::cout << "New call initiated "
                            << pthread_self() <<std::endl;
                }
              }
            }
        }
	
      //Unbind at mid session
      if ( (L_unbindService == 1) && ( G_nfinishedCalls >= (L_nbcall/2) ) )
        {
          printf("Service mid session reach\n");
          SIP_Service& L_service = L_probeTab[0]->getService();
          SIP_Probe& L_probe = L_probeTab[0]->getProbe();
	  
          if (L_service.getState() == SIP_SERVICE_ACTIVE)
            {
              //detach the service
              L_probe.serviceDetach(L_service,SIP_SERVICE_GRACEFUL_DETACH);
              std::cout <<"Service   ["<<L_service.getName()<<
                "] is detaching"<< std::endl;

            }
#define NO_REBIND
#ifndef NO_REBIND
          if(L_service.getState() == SIP_SERVICE_IDLE) {
            L_probe.serviceAttach(L_service);
            std::cout << "Service   ["      << L_service.getName()
                      << "] is re-attached" << std::endl;
            L_unbindService = 0;
          }
#else
            L_unbindService = 0;
#endif
        }
      
      L_probeTabIterator = L_probeTab.begin();

      for (int i = 0; i < L_nbProbe;i++)	
        {

          while ( (!(*L_probeTabIterator)->callQueue.empty()) && 
                  ( (gethrtime() - 
                     (*((*L_probeTabIterator)->callQueue.begin()))
                     ->getConfirmedTime() ) > G_sleep ) )
            {
              if (G_trace >= 1) std::cerr << "== Entering while ( (!(*L_probeTabIterator)->callQueue.empty()) && " << std::endl;
              if(!G_reinvite) {
                printf("Closing Session...\n");
                (*L_probeTabIterator)->closeSession(**((*L_probeTabIterator)
                                                       ->callQueue.begin()));
                (*L_probeTabIterator)->callQueue.pop_front();
                G_nfinishedCalls++;
              } else {
                if (G_trace >= 1) std::cerr << "== Entering G_reinvite " << std::endl;
                std::string dummy_string;

                UA_Session * L_session = 
                  *((*L_probeTabIterator)->callQueue.begin());

                (*L_probeTabIterator)->initOrReinitSession
                  (dummy_string,
                   NULL,
                   *((*L_probeTabIterator)->callQueue.begin()));

                (*L_probeTabIterator)->callQueue.pop_front();

                G_reinvite = 0;
              }
            }
          L_probeTabIterator++;
        }
    }
  if (L_unbindAtTheEnd == 0)
    {
      printf("Undbinding probes...\n");
      while (!L_probeTab.empty())
        {
          L_ProbeManager->unbindProbe((*L_probeTab.begin())->getProbe());
          L_probeTab.erase(L_probeTab.begin());
        }
      
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -