示例#1
0
int
main(int argc, char **argv)
{
  // Initialization:
  ros::init(argc, argv, "ros2udp_converter");
  ros::NodeHandle nh("~");

  // Read input parameters:
  nh.param("udp/port", udp_port, 21234);
  nh.param("udp/hostip", hostip, std::string("127.0.0.1"));
  nh.param("udp/buffer_length", udp_buffer_length, 2048);
  nh.param("json/indent_size", json_indent_size, 0);
  nh.param("json/newline", json_newline, false);
  nh.param("json/spacing", json_spacing, false);
  nh.param("json/use_tabs", json_use_tabs, false);
  nh.param("json/heartbeat_interval", heartbeat_interval, 0.25);

  // ROS subscriber:
  ros::Subscriber tracking_sub = nh.subscribe<opt_msgs::TrackArray>("input_topic", 1, trackingCallback);
  ros::Subscriber alive_ids_sub = nh.subscribe<opt_msgs::IDArray>("alive_ids_topic", 1, aliveIDsCallback);

  // Initialize UDP parameters:
  char buf[0];
  udp_data.si_port_ = udp_port;      // port
  udp_data.si_retry_ = 1;
  udp_data.si_num_byte_ = udp_buffer_length; // number of bytes to write (2048 -> about 30 tracks)
  udp_data.pc_pck_ = buf;         // buffer where the message is written
  udp_data.si_timeout_ = 4;
  udp_data.sj_addr_ = Inet_AtoN(hostip.c_str());

  /// Create object for UDP messaging:
  udp_messaging = open_ptrack::opt_utils::UDPMessaging(udp_data);

  /// Create client socket:
  udp_messaging.createSocketClientUDP(&udp_data);

  // Execute callbacks:
  ros::spin();

  // Close socket:
  udp_messaging.closeSocketUDP(&udp_data);

  return 0;
}
示例#2
0
void
App::ArgvReceived(int32 argc, char **argv)
{
	printf("ArgvReceived\n");
	
	Message args; 
	(void) ParseArgs(argc, argv, args);
	HandleStandardDaemonArgs(args);

	const char * value;
	if (args.HasName("help")) {
		Log(MUSCLE_LOG_INFO, "Usage:  muscled [port=%u] [listen=ip:port] [displaylevel=lvl] [filelevel=lvl] [logfile=filename]\n", DEFAULT_MUSCLED_PORT); 
#ifdef MUSCLE_ENABLE_MEMORY_TRACKING
		Log(MUSCLE_LOG_INFO, "					 [maxmem=megs]\n");
#endif
		Log(MUSCLE_LOG_INFO, "					 [maxnodespersession=num] [remap=oldip=newip]\n");
		Log(MUSCLE_LOG_INFO, "					 [ban=ippattern] [require=ippattern]\n");
		Log(MUSCLE_LOG_INFO, "					 [privban=ippattern] [privunban=ippattern]\n");
		Log(MUSCLE_LOG_INFO, "					 [privkick=ippattern] [privall=ippattern]\n");
		Log(MUSCLE_LOG_INFO, "					 [maxsendrate=kBps] [maxreceiverate=kBps]\n");
		Log(MUSCLE_LOG_INFO, "					 [maxcombinedrate=kBps] [maxmessagesize=k]\n");
		Log(MUSCLE_LOG_INFO, "					 [maxsessions=num] [maxsessionsperhost=num]\n");
		Log(MUSCLE_LOG_INFO, "					 [localhost=ipaddress] [daemon]\n");
		Log(MUSCLE_LOG_INFO, " - port may be any number between 1 and 65536\n");
		Log(MUSCLE_LOG_INFO, " - listen is like port, except it includes a local interface IP as well.\n");
		Log(MUSCLE_LOG_INFO, " - lvl is: none, critical, errors, warnings, info, debug, or trace.\n");
#ifdef MUSCLE_ENABLE_MEMORY_TRACKING
		Log(MUSCLE_LOG_INFO, " - maxmem is the max megabytes of memory the server may use (default=unlimited)\n");
#endif
		Log(MUSCLE_LOG_INFO, " - You may also put one or more ban=<pattern> arguments in.\n");
		Log(MUSCLE_LOG_INFO, "	Each pattern specifies one or more IP addresses to\n");
		Log(MUSCLE_LOG_INFO, "	disallow connections from, e.g. ban=192.168.*.*\n");
		Log(MUSCLE_LOG_INFO, " - You may put one or more require=<pattern> arguments in.\n");
		Log(MUSCLE_LOG_INFO, "	If any of these are present, then only IP addresses that match\n");
		Log(MUSCLE_LOG_INFO, "	at least one of them will be allowed to connect.\n");
		Log(MUSCLE_LOG_INFO, " - To assign privileges, specify one of the following:\n");
		Log(MUSCLE_LOG_INFO, "	privban=<pattern>, privunban=<pattern>,\n");
		Log(MUSCLE_LOG_INFO, "	privkick=<pattern> or privall=<pattern>.\n");
		Log(MUSCLE_LOG_INFO, "	privall assigns all privileges to the matching IP addresses.\n");
		Log(MUSCLE_LOG_INFO, " - remap tells muscled to treat connections from a given IP address\n");
		Log(MUSCLE_LOG_INFO, "	as if they are coming from another (for stupid NAT tricks, etc)\n");
		Log(MUSCLE_LOG_INFO, " - If daemon is specified, muscled will run as a background process.\n");
	}

	{
		for (int32 i = 0; (args.FindString("port", i, &value) == B_NO_ERROR); i++) {
			int16 port = atoi(value);
			
			if (port >= 0)
				listenPorts.PutWithDefault(IPAddressAndPort(invalidIP, port));
		}

		for (int32 i = 0; (args.FindString("listen", i, &value) == B_NO_ERROR); i++) {
			IPAddressAndPort iap(value, DEFAULT_MUSCLED_PORT, false);
			
			if (iap.GetPort() > 0)
				listenPorts.PutWithDefault(iap);
			else
				LogTime(MUSCLE_LOG_ERROR, "Unable to parse IP/port string [%s]\n", value);
		}
	}

	{
		for (int32 i = 0; (args.FindString("remap", i, &value) == B_NO_ERROR); i++) {
			StringTokenizer tok(value, ",=");
			const char * from = tok();
			const char * to = tok();
			ip_address fromIP = from ? Inet_AtoN(from) : 0;
			
			if ((fromIP != invalidIP)&&(to)) {
				char ipbuf[64]; Inet_NtoA(fromIP, ipbuf);
				LogTime(MUSCLE_LOG_INFO, "Will treat connections coming from [%s] as if they were from [%s].\n", ipbuf, to);
				tempRemaps.Put(fromIP, to);
			} else
				LogTime(MUSCLE_LOG_ERROR, "Error parsing remap argument (it should look something like remap=192.168.0.1,132.239.50.8).\n");
		}
	}

#ifdef MUSCLE_ENABLE_MEMORY_TRACKING
	if (args.FindString("maxmem", &value) == B_NO_ERROR) {
		int megs = muscleMax(1, atoi(value));
		LogTime(MUSCLE_LOG_INFO, "Limiting memory usage to %i megabyte%s.\n", megs, (megs==1)?"":"s");
		maxBytes = megs*1024L*1024L;
	}
#endif

	if (args.FindString("maxmessagesize", &value) == B_NO_ERROR) {
		int k = muscleMax(1, atoi(value));
		LogTime(MUSCLE_LOG_INFO, "Limiting message sizes to %i kilobyte%s.\n", k, (k==1)?"":"s");
		maxMessageSize = k*1024L;
	}

	if (args.FindString("maxsendrate", &value) == B_NO_ERROR) {
		float k = (float) atof(value);
		maxSendRate = muscleMax((uint32)0, (uint32)(k * 1024.0f));
	}

	if (args.FindString("maxreceiverate", &value) == B_NO_ERROR) {
		float k = (float) atof(value);
		maxReceiveRate = muscleMax((uint32)0, (uint32)(k * 1024.0f));
	}

	if (args.FindString("maxcombinedrate", &value) == B_NO_ERROR) {
		float k = (float) atof(value);
		maxCombinedRate = muscleMax((uint32)0, (uint32)(k * 1024.0f));
	}

	if (args.FindString("maxnodespersession", &value) == B_NO_ERROR) {
		maxNodesPerSession = atoi(value);
		LogTime(MUSCLE_LOG_INFO, "Limiting nodes-per-session to "UINT32_FORMAT_SPEC".\n", maxNodesPerSession);
	}

	if (args.FindString("maxsessions", &value) == B_NO_ERROR) {
		maxSessions = atoi(value);
		LogTime(MUSCLE_LOG_INFO, "Limiting total session count to "UINT32_FORMAT_SPEC".\n", maxSessions);
	}

	if (args.FindString("maxsessionsperhost", &value) == B_NO_ERROR) {
		maxSessionsPerHost = atoi(value);
		LogTime(MUSCLE_LOG_INFO, "Limiting session count for any given host to "UINT32_FORMAT_SPEC".\n", maxSessionsPerHost);
	}
	
	if (args.FindString("privatekey", &value) == B_NO_ERROR) {
		fprivateKeyFilePath = new String(value);	
		//const String * fprivateKeyFilePath = args.GetStringPointer("privatekey");
		//LogTime(MUSCLE_LOG_INFO, "Limiting session count for any given host to "UINT32_FORMAT_SPEC".\n", fprivateKeyFilePath);
	}

	{
		for (int32 i = 0; (args.FindString("ban", i, &value) == B_NO_ERROR); i++) {
			LogTime(MUSCLE_LOG_INFO, "Banning all clients whose IP addresses match [%s].\n", value);	
			bans.AddTail(value);
		}
	}

	{
		for (int32 i = 0; (args.FindString("require", i, &value) == B_NO_ERROR); i++) {
			LogTime(MUSCLE_LOG_INFO, "Allowing only clients whose IP addresses match [%s].\n", value);	
			requires.AddTail(value);
		}
	}
	
	{
		const char * privNames[] = {"privkick", "privban", "privunban", "privall"};
		for (int p = 0; p <= PR_NUM_PRIVILEGES; p++) {  // if (p == PR_NUM_PRIVILEGES), that means all privileges
			for (int32 q=0; (args.FindString(privNames[p], q, &value) == B_NO_ERROR); q++) {
				LogTime(MUSCLE_LOG_INFO, "Clients whose IP addresses match [%s] get %s privileges.\n", value, privNames[p]+4);
				char tt[32]; 
				muscleSprintf(tt, "priv%i", p);
				tempPrivs.AddString(tt, value);
			}
		}
	}
}
示例#3
0
void HandleStandardDaemonArgs(const Message & args)
{
   TCHECKPOINT;

#ifndef WIN32
   if (args.HasName("disablestderr"))
   {
      LogTime(MUSCLE_LOG_INFO, "Suppressing all further output to stderr!\n");
      close(STDERR_FILENO);
   }
   if (args.HasName("disablestdout"))
   {
      LogTime(MUSCLE_LOG_INFO, "Suppressing all further output to stdout!\n");
      close(STDOUT_FILENO);
   }
#endif

   // Do this first, so that the stuff below will affect the right process.
   const char * n;
   if (args.FindString("daemon", &n) == B_NO_ERROR)
   {
      LogTime(MUSCLE_LOG_INFO, "Spawning off a daemon-child...\n");
      if (BecomeDaemonProcess(NULL, n[0] ? n : "/dev/null") != B_NO_ERROR)
      {
         LogTime(MUSCLE_LOG_CRITICALERROR, "Could not spawn daemon-child process!\n");
         ExitWithoutCleanup(10);
      }
   }

#ifdef WIN32
   const String * consoleStr = args.GetStringPointer("console");
   if (consoleStr) Win32AllocateStdioConsole(consoleStr->Cstr());
#endif

#ifdef MUSCLE_ENABLE_DEADLOCK_FINDER
   {
      const char * df = args.GetCstr("deadlockfinder");
      if (df) _enableDeadlockFinderPrints = ParseBool(df, true);
   }
#endif

   const char * value;
   if (args.FindString("displaylevel", &value) == B_NO_ERROR)
   {
      int ll = ParseLogLevelKeyword(value);
      if (ll >= 0) SetConsoleLogLevel(ll);
              else LogTime(MUSCLE_LOG_INFO, "Error, unknown display log level type [%s]\n", value);
   }

   if ((args.FindString("oldlogfilespattern", &value) == B_NO_ERROR)&&(*value != '\0')) SetOldLogFilesPattern(value);

   if ((args.FindString("maxlogfiles", &value) == B_NO_ERROR)||(args.FindString("maxnumlogfiles", &value) == B_NO_ERROR))
   {
      const uint32 maxNumFiles = (uint32) atol(value);
      if (maxNumFiles > 0) SetMaxNumLogFiles(maxNumFiles);
                      else LogTime(MUSCLE_LOG_ERROR, "Please specify a maxnumlogfiles value that is greater than zero.\n");
   }

   if (args.FindString("logfile", &value) == B_NO_ERROR)
   {
      SetFileLogName(value);
      if (GetFileLogLevel() == MUSCLE_LOG_NONE) SetFileLogLevel(MUSCLE_LOG_INFO); // no sense specifying a name and then not logging anything!
   }

   if (args.FindString("filelevel", &value) == B_NO_ERROR)
   {
      const int ll = ParseLogLevelKeyword(value);
      if (ll >= 0) SetFileLogLevel(ll);
              else LogTime(MUSCLE_LOG_INFO, "Error, unknown file log level type [%s]\n", value);
   }

   if (args.FindString("maxlogfilesize", &value) == B_NO_ERROR)
   {
      const uint32 maxSizeKB = (uint32) atol(value);
      if (maxSizeKB > 0) SetFileLogMaximumSize(maxSizeKB*1024);
                    else LogTime(MUSCLE_LOG_ERROR, "Please specify a maxlogfilesize in kilobytes, that is greater than zero.\n");
   }

   if ((args.HasName("compresslogfile"))||(args.HasName("compresslogfiles"))) SetFileLogCompressionEnabled(true);

   if (args.FindString("localhost", &value) == B_NO_ERROR)
   {
      const IPAddress ip = Inet_AtoN(value);
      if (ip != invalidIP)
      {
         char ipbuf[64]; Inet_NtoA(ip, ipbuf);
         LogTime(MUSCLE_LOG_INFO, "IP address [%s] will be used as the localhost address.\n", ipbuf);
         SetLocalHostIPOverride(ip);
      }
      else LogTime(MUSCLE_LOG_ERROR, "Error parsing localhost IP address [%s]!\n", value);
   }

   if (args.FindString("dnscache", &value) == B_NO_ERROR)
   {
      const uint64 micros = ParseHumanReadableTimeIntervalString(value);
      if (micros > 0)
      {
         uint32 maxCacheSize = 1024;
         if (args.FindString("dnscachesize", &value) == B_NO_ERROR) maxCacheSize = (uint32) atol(value);
         LogTime(MUSCLE_LOG_INFO, "Setting DNS cache parameters to " UINT32_FORMAT_SPEC " entries, expiration period is %s\n", maxCacheSize, GetHumanReadableTimeIntervalString(micros)());
         SetHostNameCacheSettings(maxCacheSize, micros);
      }
      else LogTime(MUSCLE_LOG_ERROR, "Unable to parse time interval string [%s] for dnscache argument!\n", value);
   }

   if ((args.HasName("debugcrashes"))||(args.HasName("debugcrash")))
   {
#if defined(__linux__) || defined(__APPLE__)
      LogTime(MUSCLE_LOG_INFO, "Enabling stack-trace printing when a crash occurs.\n");
      signal(SIGSEGV, CrashSignalHandler);
      signal(SIGBUS,  CrashSignalHandler);
      signal(SIGILL,  CrashSignalHandler);
      signal(SIGABRT, CrashSignalHandler);
      signal(SIGFPE,  CrashSignalHandler);
#elif MUSCLE_USE_MSVC_STACKWALKER
# ifndef MUSCLE_INLINE_LOGGING
      LogTime(MUSCLE_LOG_INFO, "Enabling stack-trace printing when a crash occurs.\n");
      SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER) Win32FaultHandler);
# endif
#else
      LogTime(MUSCLE_LOG_ERROR, "Can't enable stack-trace printing when a crash occurs, that feature isn't supported on this platform!\n");
#endif
   }

#if defined(__linux__) || defined(__APPLE__)
   {
      const char * niceStr = NULL; (void) args.FindString("nice", &niceStr);
      const char * meanStr = NULL; (void) args.FindString("mean", &meanStr);

      const int32 niceLevel = niceStr ? ((strlen(niceStr) > 0) ? atoi(niceStr) : 5) : 0;
      const int32 meanLevel = meanStr ? ((strlen(meanStr) > 0) ? atoi(meanStr) : 5) : 0;
      const int32 effectiveLevel = niceLevel-meanLevel;

      if (effectiveLevel)
      {
         errno = 0;  // the only reliable way to check for an error here :^P
         const int ret = nice(effectiveLevel);  // I'm only looking at the return value to shut gcc 4.4.3 up
         if (errno != 0) LogTime(MUSCLE_LOG_WARNING, "Could not change process execution priority to " INT32_FORMAT_SPEC " (ret=%i).\n", effectiveLevel, ret);
                    else LogTime(MUSCLE_LOG_INFO, "Process is now %s (niceLevel=%i)\n", (effectiveLevel<0)?"mean":"nice", effectiveLevel);
      }
   }
#endif

#ifdef __linux__
   const char * priStr;
        if (args.FindString("realtime",      &priStr) == B_NO_ERROR) SetRealTimePriority(priStr, false);
   else if (args.FindString("realtime_rr",   &priStr) == B_NO_ERROR) SetRealTimePriority(priStr, false);
   else if (args.FindString("realtime_fifo", &priStr) == B_NO_ERROR) SetRealTimePriority(priStr, true);
#endif

#ifdef MUSCLE_CATCH_SIGNALS_BY_DEFAULT
# ifdef MUSCLE_AVOID_SIGNAL_HANDLING
#  error "MUSCLE_CATCH_SIGNALS_BY_DEFAULT and MUSCLE_AVOID_SIGNAL_HANDLING are mutually exclusive compiler flags... you can not specify both!"
# endif
   if (args.HasName("dontcatchsignals"))
   {
      _mainReflectServerCatchSignals = false;
      LogTime(MUSCLE_LOG_DEBUG, "Controlled shutdowns (via Control-C) disabled in the main thread.\n");
   }
#else
   if (args.HasName("catchsignals"))
   {
# ifdef MUSCLE_AVOID_SIGNAL_HANDLING
      LogTime(MUSCLE_LOG_ERROR, "Can not enable controlled shutdowns, MUSCLE_AVOID_SIGNAL_HANDLING was specified during compilation!\n");
# else
      _mainReflectServerCatchSignals = true;
      LogTime(MUSCLE_LOG_DEBUG, "Controlled shutdowns (via Control-C) enabled in the main thread.\n");
# endif
   }
#endif

   if (args.HasName("printnetworkinterfaces"))
   {
      Queue<NetworkInterfaceInfo> infos;
      if (GetNetworkInterfaceInfos(infos) == B_NO_ERROR)
      {
         printf("--- Network interfaces on this machine are as follows: ---\n");
         for (uint32 i=0; i<infos.GetNumItems(); i++) printf("  %s\n", infos[i].ToString()());
         printf("--- (end of list) ---\n");
      }
   }
}