Пример #1
0
/**
 * Search all locations for argument defaults and parse them.
 * These locations may be environment variables to read argument varues
 * from, or files to read. 
 * @sa addDefaultArgumentLocation
 *
 * @note If you use this function your normal argc (passed into main()) will
 * have been modified, and won't reflect reality anymore. You'll have to use 
 * getArgc() to get the actual original argument count.  This is a little wierd but is 
 * this way so lots of people don't have to change lots of code.   
 */
AREXPORT void ArArgumentParser::loadDefaultArguments(int position)
{
  std::list<std::string>::iterator it;
  std::list<bool>::iterator bIt;
  const char *str;
  char *argumentsPtr;
  char arguments[100000];

  if (!myUsingBuilder)
    {
      myBuilder = new ArArgumentBuilder;
      myBuilder->addStringsAsIs(*myArgc, myArgv);
      myOwnBuilder = true;
      myUsingBuilder = true;
    }

  for (it = ourDefaultArgumentLocs.begin(), 
        bIt = ourDefaultArgumentLocIsFile.begin();
       it != ourDefaultArgumentLocs.end(); 
       it++, bIt++)
  {
    str = (*it).c_str();
    // see if its an environmental variable
    if (!(*bIt) && (argumentsPtr = getenv(str)) != NULL)
    {
      ArArgumentBuilder compressed;
      compressed.addPlain(argumentsPtr);
      compressed.compressQuoted(true);
      myBuilder->addStringsAsIs(compressed.getArgc(), compressed.getArgv(), 
                              position);
      ArLog::log(ArLog::Normal, 
		 "Added arguments from environmental variable '%s'", str);
    }
    // see if we have a file
    else if ((*bIt) && 
	     ArUtil::getStringFromFile(str, arguments, sizeof(arguments)))
    {
      ArArgumentBuilder compressed;
      compressed.addPlain(arguments);
      compressed.compressQuoted(true);
      myBuilder->addStringsAsIs(compressed.getArgc(), compressed.getArgv(), 
                              position);
      ArLog::log(ArLog::Normal, "Added arguments from file '%s'", 
		 str);
    }
    // the file or env didn't exit
    // this'll return true otherwise it'll return false)
    else
    {
      ArLog::log(ArLog::Verbose, 
		 "Could not load from environmental variable or file '%s'", 
		 str);
    }
  }
}
Пример #2
0
AREXPORT void ArNetServer::runOnce(void)
{

  ArSocket acceptingSocket;
  ArSocket *socket;
  char *str;
  std::list<ArSocket *> removeList;
  std::list<ArSocket *>::iterator it;
  ArArgumentBuilder *args = NULL;
  std::string command;

  if (!myOpened)
  {
    return;
  }

  lock();
  // get any new sockets that want to connect
  while (myServerSocket.accept(&acceptingSocket) && acceptingSocket.getFD() >= 0)
  {
    acceptingSocket.setNonBlock();
    // see if we want more sockets
    if (!myMultipleClients && (myConns.size() > 0 ||
			       myConnectingConns.size() > 0))
    {
      // we didn't want it, so politely tell it to go away
      acceptingSocket.writeString("Conn refused.");
      acceptingSocket.writeString(
	      "Only client allowed and it is already connected.");
      acceptingSocket.close();
      ArLog::log(ArLog::Terse, "ArNetServer not taking multiple clients and another client tried to connect from %s.", acceptingSocket.getIPString());
    }
    else 
    {
      // we want the client so we put it in our list of connecting
      // sockets, which means that it is waiting to give its password
      socket = new ArSocket;
      socket->setLogWriteStrings(myLoggingDataSent);
      socket->transfer(&acceptingSocket);
      socket->writeString("Enter password:"******"Client connecting from %s.",
		 socket->getIPString());
    }
  }

  // now we read in data from our connecting sockets and see if
  // they've given us the password
  for (it = myConnectingConns.begin(); it != myConnectingConns.end(); ++it)
  {
    socket = (*it);
    // read in what the client has to say
    if ((str = socket->readString()) != NULL)
    {
      if (str[0] == '\0')
	continue;
      // now see if the word matchs the password
      if (myPassword == str)
      {
	ArLog::log(ArLog::Normal, 
		   "Client from %s gave password and connected.",
		   socket->getIPString());
	myConns.push_front(socket);
	removeList.push_front(socket);
	internalGreeting(socket);
      }
      else
      {
	socket->close();
	myDeleteList.push_front(socket);
	ArLog::log(ArLog::Terse, 
		   "Client from %s gave wrong password and is being disconnected.", 
		   socket->getIPString());
      }
    }
    // if we couldn't read a string it means we lost a connection
    else
    {
      ArLog::log(ArLog::Normal, 
		 "Connection to %s lost.", socket->getIPString());
      socket->close();
      myDeleteList.push_front(socket);
    }
  }
  // now we clear out the ones we want to remove from our connecting
  // clients list
  while ((it = removeList.begin()) != removeList.end())
  {
    socket = (*it);
    myConnectingConns.remove(socket);
    removeList.pop_front();
  }


  // now we read in data from our connecting sockets and see if
  // they've given us the password
  for (it = myConns.begin(); it != myConns.end() && myOpened; ++it)
  {
    socket = (*it);
    // read in what the client has to say
    while ((str = socket->readString()) != NULL)
    {
      // if this is null then there wasn't anything said
      if (str[0] == '\0')
	break;
      // make sure we read something
      // set up the arguments and then call the function for the
      // argument
      args = new ArArgumentBuilder;
      args->addPlain(str);
      //args->log();
      parseCommandOnSocket(args, socket);
      delete args;
      args = NULL;
    }
    // if str was NULL we lost connection
    if (str == NULL)
    {
      ArLog::log(ArLog::Normal, 
		 "Connection to %s lost.", socket->getIPString());
      socket->close();
      myDeleteList.push_front(socket);
    }
  }

  // now we delete the ones we want to delete (we could do this above
  // but then it wouldn't be symetrical with above)
  while ((it = myDeleteList.begin()) != myDeleteList.end())
  {
    socket = (*it);
    myConnectingConns.remove(socket);
    myConns.remove(socket);
    socket->close();
    delete socket;
    myDeleteList.pop_front();
  }

  if (myWantToClose)
  {
    close();
  }
  unlock();
}
Пример #3
0
bool ArSoundPlayer::playWavFile(const char* filename, const char* params)
{
  ArArgumentBuilder builder;
  //builder.addPlain("sleep .35; play");
  builder.addPlain("play");
  builder.add("-v %.2f", ourVolume);
  builder.addPlain(filename);
  builder.addPlain(params);
  ArLog::log(ArLog::Normal, "ArSoundPlayer: Playing file \"%s\" with \"%s\"", 
	     filename, builder.getFullString());
  
  int ret;
  if ((ret = system(builder.getFullString())) != -1)
  {
    ArLog::log(ArLog::Normal, "ArSoundPlayer: Played file \"%s\" with \"%s\" (got %d)", 
	       filename, builder.getFullString(), ret);
    return true;
  }
  else
  {
    ArLog::logErrorFromOS(ArLog::Normal, 
			  "ArSoundPlayer::playWaveFile: system call failed");
    return false;
  }


  /*
    This was an old mechanism for doing a fork then exec in order to
    mimic a system call, so that it could have the child PID for
    killing... however in our embedded linux killing the play command
    doesn't also kill the sox command, so the kill doesn't work at
    all... and the fork() would also reserve a lot of memory for the new process
    ...
   so it was replaced with the above system call

  const char* prog = NULL;
  prog = getenv("PLAY_WAV");
  if(prog == NULL)
    prog = "play";
  char volstr[4];
  if(strcmp(prog, "play") == 0)
  {
    snprintf(volstr, 4, "%f", ourVolume);
  }  

  ArLog::log(ArLog::Normal, "ArSoundPlayer: Playing file \"%s\" using playback program \"%s\" with argument: -v %s", filename, prog, volstr);
  ourPlayChildPID = fork();

  //ourPlayChildPID = vfork(); // XXX rh experimental, avoids the memory copy cost of fork()
  // NOTE: after vfork() you can ONLY safely use an exec function or _exit() in the
  // child process. Any other call, if it modifies memory still shared by the
  // parent, will result in problems.
  if(ourPlayChildPID == -1) 
  {
    ArLog::log(ArLog::Terse, "ArSoundPlayer: error forking! (%d: %s)", errno, 
      (errno == EAGAIN) ? "EAGAIN reached process limit, or insufficient memory to copy page tables" : 
        ( (errno == ENOMEM) ? "ENOMEM out of kernel memory" : "unknown error" ) );

    ArLog::logErrorFromOS(ArLog::Normal, 
			  "ArSoundPlayer::playWaveFile: fork failed");
    return false;
  }
  if(ourPlayChildPID == 0)
  {
    // child process: execute sox
    int r = -1;
    r = execlp(prog, prog, "-v", volstr, filename, (char*)0);
    if(r < 0)
    {
      int err = errno;
      const char *errstr = strerror(err);
      printf("ArSoundPlayer (child process): Error executing Wav file playback program \"%s %s\" (%d: %s)\n", prog, filename, err, errstr);
      //_exit(-1);   // need to use _exit with vfork
      exit(-1);
    }
  } 
  // parent process: wait for child to finish
  ArLog::log(ArLog::Verbose, "ArSoundPlayer: created child process %d to play wav file \"%s\".", 
      ourPlayChildPID, filename);
  int status;
  waitpid(ourPlayChildPID, &status, 0);
  if(WEXITSTATUS(status) != 0) {
    ArLog::log(ArLog::Terse, "ArSoundPlayer: Error: Wav file playback program \"%s\" with file \"%s\" exited with error code %d.", prog, filename, WEXITSTATUS(status));
    ourPlayChildPID = -1;
    return false;
  }
  ArLog::log(ArLog::Verbose, "ArSoundPlayer: child process %d finished.", ourPlayChildPID);
  ourPlayChildPID = -1;
  return true;
  */
}