Пример #1
0
int main(int argc, char* argv[])
{

    if (4 < argc)
    {
        std::string strSrcIDBegin = argv[1];
        std::string strDstIDBegin = argv[2];
        unsigned int uiLinkNum = (unsigned int)atoi(argv[3]);


        std::string strMsgBodyLen = argv[4];
        transform(strMsgBodyLen.begin(), strMsgBodyLen.end(), strMsgBodyLen.begin(), tolower);
        boost::int32_t iPos = 0;
        std::string strLen;
        boost::uint32_t uiLen = 0;
        //int ipos = std::string::npos;
        //int ifind = strMsgBodyLen.rfind('k');
        if ((boost::int32_t)std::string::npos != (iPos = strMsgBodyLen.rfind('k')))
        {
            strLen = strMsgBodyLen.substr(0, iPos);
            uiLen = (boost::uint32_t)atoi(strLen.c_str());
            uiLen = uiLen * 1024;
        }
        else if ((boost::int32_t)std::string::npos != (iPos = strMsgBodyLen.rfind('m')))
        {
            strLen = strMsgBodyLen.substr(0, iPos);
            uiLen = (boost::uint32_t)atoi(strLen.c_str());
            uiLen = uiLen * 1024 * 1024;
        }
        else
        {
            uiLen = (boost::uint32_t)atoi(strMsgBodyLen.c_str());
        }

        char *pIPAddress = argv[5];
        char *pIPPort = argv[6];
        char *pIsOnlyOneDst = argv[7];
        bool IsOnlyOneDst = false;
        if (std::string("true") == std::string(pIsOnlyOneDst))
        {
            IsOnlyOneDst = true;
        }
        else if (std::string("false") == std::string(pIsOnlyOneDst))
        {
            IsOnlyOneDst = false;
        }
        else
        {
            IsOnlyOneDst = false;
        }

        boost::uint32_t uiRunModule = ProxyClient::SEND;
        if (std::string("send") == std::string(argv[8]))
        {
            uiRunModule = ProxyClient::SEND;
        }
        else if (std::string("receive") == std::string(argv[8]))
        {
            uiRunModule = ProxyClient::RECEIVE;
        }
        else if (std::string("both") == std::string(argv[8]))
        {
            uiRunModule = ProxyClient::SEND | ProxyClient::RECEIVE;
        }

        printf("src id is %s, dst id is %s, only one dst is %s, link num is %u, msg len is %u, remote ip is %s, port is %s\n",
               strSrcIDBegin.c_str(), strDstIDBegin.c_str(), IsOnlyOneDst ? "true" : "false", uiLinkNum, uiLen, pIPAddress, pIPPort);


        //ProxyClient clt("23", "25", false, 1, 100, "172.16.23.181", "6666");
        ProxyClient clt(strSrcIDBegin, strDstIDBegin, IsOnlyOneDst, uiLinkNum, uiLen, pIPAddress, pIPPort);

        clt.Run(1, uiRunModule);

        //boost::this_thread::sleep(boost::posix_time::seconds(20));
    }
    else
    {

        unsigned short usPort = 6789;
        if (2 <= argc)
        {
            usPort = (unsigned short)atoi(argv[1]);
        }

        unsigned int uiThreadNum = 2;
        if (3 <= argc)
        {
            uiThreadNum = (unsigned int)atoi(argv[2]);
        }

        bool IsNeedDaemonRun = false;
        if (4 <= argc && std::string("-d") == argv[3])
        {
            IsNeedDaemonRun = true;
        }

        if (IsNeedDaemonRun)
        {
            ForkChild();
        }

        InitLog();

        LOG_INFO_RLD("Proxy Server begin...");

        LOG_INFO_RLD("Proxy runing thread number is " << uiThreadNum << " port is " << usPort << ", daemon status is " << IsNeedDaemonRun);

        ProxyHub *phb = NULL;
        try
        {
            phb = new ProxyHub(usPort);
        }
        catch (...)
        {
            LOG_ERROR_RLD("Create proxy server failed, exit...");
            return 0;
        }

        const std::string &strSrcIDReplaceByIncSeq = GetConfig("General.SrcIDReplaceByIncSeq");
        bool blSrcIDReplaceByIncSeq = false;
        if (!strSrcIDReplaceByIncSeq.empty())
        {
            blSrcIDReplaceByIncSeq = boost::lexical_cast<bool>(strSrcIDReplaceByIncSeq);
        }
        phb->SetSrcIDReplaceByIncSeq(blSrcIDReplaceByIncSeq);

        bool blCreateSIDOnConnected = false;
        const std::string &strCreateSIDOnConnected = GetConfig("General.CreateSIDOnConnected");
        if (!strCreateSIDOnConnected.empty())
        {
            blCreateSIDOnConnected = boost::lexical_cast<bool>(strCreateSIDOnConnected);
        }
        phb->SetCreateSIDOnConnected(blCreateSIDOnConnected);

        unsigned int uiAsyncReadTimeOut = 10;
        const std::string &strAsyncReadTimeOut = GetConfig("General.AsyncReadTimeOut");
        if (!strAsyncReadTimeOut.empty())
        {
            uiAsyncReadTimeOut = boost::lexical_cast<unsigned int>(strAsyncReadTimeOut);
        }
        phb->SetAsyncReadTimeOut(uiAsyncReadTimeOut);

        const std::string &strAuth = GetConfig("Auth.Enable");
        bool blAuth = false;
        if (!strAuth.empty() && !blSrcIDReplaceByIncSeq) //不能同时开启序列号自增开关,一旦开始,则鉴权配置不起作用,默认关闭
        {
            blAuth = boost::lexical_cast<bool>(strAuth);

            if (blAuth) //若是启用了,则继续解析鉴权参数
            {
                const std::string &strAuthSrcIP = GetConfig("Auth.SrcIP");
                const std::string &strAuthSrcPort = GetConfig("Auth.SrcPort");

                if (strAuthSrcIP.empty() || strAuthSrcPort.empty())
                {
                    blAuth = false;
                }
                else
                {
                    phb->SetAuthSrcIP(strAuthSrcIP);
                    phb->SetAuthSrcPort(boost::lexical_cast<int>(strAuthSrcPort));
                }
            }
        }
        phb->SetAuthEnable(blAuth);


        LOG_INFO_RLD("Proxy begin running...");
        phb->Run(uiThreadNum);
    }

    return 0;
}
Пример #2
0
main(int argc, char *argv[])
{
  int pids[MAX_TERMINAL + 1];
  char * cmd_argv[MAX_ARGC];
  char numbuf[5];
  int i;
  int child_cnt;
  int status;


  cmd_argv[1] = numbuf;
  cmd_argv[2] = NULL;
  child_cnt = 0;
  for ( i = 0; i <= MAX_TERMINAL; i++) {
    sprintf(numbuf, "%d", i);
    if (i == CONSOLE) {
      cmd_argv[0] = "src/console";
    }
    else {
      cmd_argv[0] = "src/shell";
    }      


    switch (pids[i] = ForkChild(cmd_argv)) {
    case NOEXEC:
      TtyPrintf(CONSOLE," forking to terminal %d, %s %s\n", i, 
		cmd_argv[0], cmd_argv[1]);
      TtyPrintf(CONSOLE,
	"Control prog for terminal %d could not be started.\n", i);
      break;
    case NOFORK:
      TtyPrintf(CONSOLE," forking to terminal %d, %s %s\n", i, 
		cmd_argv[0], cmd_argv[1]);
      TtyPrintf(CONSOLE,
		"Error in Fork() for terminal %d.\n", i);  
      break;
    default:
      TtyPrintf(CONSOLE," forking to terminal %d, %s %s\n", i, 
		cmd_argv[0], cmd_argv[1]);
      TtyPrintf(CONSOLE, 
		"Starting pid %d in terminal %d\n", pids[i], i);
      child_cnt++;
    }
  }


  if (pids[CONSOLE] == -1) {
    TtyPrintf(CONSOLE, "Cannot start Console monitor, halting...\n");
    Exit(-1);
  }
  while (1) {
    int pid;
    if (!child_cnt) {
      TtyPrintf(CONSOLE, 
		"No terminals could be serviced, halting...\n");
      Exit(-2);
    }
    TtyPrintf(CONSOLE," waiting....\n");
    pid = Wait(&status);
    TtyPrintf(CONSOLE," returned from wait, pid %d\n", pid);

    if (pid == pids[CONSOLE]) {
      TtyPrintf(CONSOLE, "Halting Yalnix\n");
      Exit(0);
    }
    for (i = 0; i <= MAX_TERMINAL; i++) {
      if (pid == pids[i]) break;
    }
    TtyPrintf(CONSOLE, "Pid %d died at terminal %d.\n", pids[i], i);
    cmd_argv[0] = "shell";
    sprintf(numbuf, "%d", i);
    pids[i] = ForkChild(cmd_argv);
    switch (pids[i]) {
    case NOEXEC:
      TtyPrintf(CONSOLE,
		"Shell for terminal %d could not be started.\n", i);
      child_cnt--;
      break;
    case NOFORK:
      TtyPrintf(CONSOLE,
		"Error in Fork() for terminal shell %d.\n", i);  
      child_cnt--;
      break;
    default:
      TtyPrintf(CONSOLE,
		"Started shell with pid %d in terminal %d.\n",
		pids[i], i);
      break;
    }
  }
}