nvqrReturn_t nvqr_connect(NVQRConnection *connection, pid_t pid)
{
    memset(connection, 0, sizeof(*connection));
    connection->pid = pid;
    connection->process_name = process_name_from_pid(pid);

    if (!create_client(connection)) {
        return NVQR_ERROR_UNKNOWN;
    }

    if (!open_server_connection(&(connection->server_handle), pid)) {
        destroy_client(*connection);
        return NVQR_ERROR_NOT_SUPPORTED;
    }

    if (!connect_to_server(connection)) {
        destroy_client(*connection);
        close_server_connection(connection->server_handle);
        return NVQR_ERROR_UNKNOWN;
    }

    return NVQR_SUCCESS;
}
Exemplo n.º 2
0
bool playGame(AI& fp,AI& sp,char* host,int&results)
{
  //cout<<"PlayGame Function Called"<<endl;
  int socket = open_server_connection(host, "19000");
  if(socket == -1)
  {
    cerr << "Unable to connect to server" << endl;
    return false;
  }
  if(!serverLogin(socket, fp.username(), fp.password()))
  {
    cerr << "Login Failed" << endl;
    return false;
  }
  socket = createGame();
  int game = getGameNumber();
  cout<<"TOP Game: "<<game<<endl;
  //cout<<"Above Fork"<<endl;
  pid_t pID = fork();
  if(pID ==0)//PLAYER 2
  {
    //cout<<"Fork Happened for Player 2"<<endl;
    socket = open_server_connection(host, "19000");
    if(socket == -1)
    {
      cerr << "Unable to connect to server" << endl;
      return false;
    }
    //cout<<"Attempting to log in"<<endl;
    if(!serverLogin(socket, sp.username(), sp.password()))
    {
      return false;
    }
    //cout<<"Joining Game"<<endl;
    socket = joinGame(game);
    //cout<<"Above while loop"<<endl;
    while(networkLoop(socket))
    {
      if(sp.startTurn())
      {
        endTurn();
      }
      else
      {
        getStatus();
      }
    }
  }
  else
  {
    //cout<<"P1 above while"<<endl;
    while(networkLoop(socket))
    {
      if(fp.startTurn())
      {
        endTurn();
      }
      else
      {
        getStatus();
      }
    }
  }

  //Wait for log
  while (networkLoop(socket)){}
  //Get the scores
  results=(fp.getWinValue());
  if(pID == 0)
  {
    close(socket);
       
    exit(0);
    //TODO Hack to kill the spawned copy
  }
  close(socket);
  cout<<"Game["<<game<<"] "<<fp.aiNum<<" VS "<<sp.aiNum<<" final score = "<<results<<endl;
  return true;
}
Exemplo n.º 3
0
int main(int argc, char** argv)
{
	//connect to server, given as the first argument
	string usage = string(argv[0]) + " <server> <port> [game #]";
	if(argc < 3) {
		cerr <<  usage << endl;
		return 0;
	}
	
	bool end_game = false;
	bool start_game = true;
	if(argc == 4) {
		// they specified a game number,
		// do something different?
		start_game = false;
		//cout << "Trying to joing game #" << argv[3] << endl;
	}
	
	// we got enough command-line arguments,
	// start tying to connect:
	int sock_server = open_server_connection(argv[1], argv[2]);
	if(sock_server == -1) {
		return 0;
	}

	// create an instance of the player's AI class
	myAI gameAI;
	
	string comm_buffer;
	sexp_t* sexp;

	// log into the server, using the AI's team name
	string whos_turn;
	my_user_id = login_to_server(sock_server, gameAI.PlayerName() );
	
	// setup the logging filename
	ostringstream stream;
    tm* myTime;
    time_t theTime = time(NULL);
    myTime = localtime(&theTime);
	stream << myTime->tm_hour << "." << myTime->tm_min;
	
    string name = string(argv[0]);
    for(int i=name.size()-1; i >=0; i--)
    {
        if(name[i] == ' ' || name[i] == '/' || name[i] == '\\' || name[i] == '.' || name[i] == ':')
        {
            name.erase(i,1);
        }
    }
    string log_filename = stream.str() + "-" + name + string(".gamelog");
	cout << "Filename: " << log_filename << endl;
	
	#ifdef DEBUG
	cout << "DEBUG: logged in as user #" << my_user_id << endl;
	#endif
	
	// okay, we've succesfully logged in,
	// now start or join a game

	if(start_game) {
		// create a new game:
		string game_number = create_game(sock_server);
		
		gameAI.playerNumber = 1;
		gameAI.myBase = coordinate(0,0);
		gameAI.enemyBase = coordinate(25,25);
		
	} else {
	
		gameAI.playerNumber = 2;
		gameAI.myBase = coordinate(0,0);
		gameAI.enemyBase = coordinate(25,25);
		
		// join an existing game
		cout << "Trying to join game " << argv[3] << "..." << endl;
		string join_string = "(join-game " + string(argv[3]) + ")";
		send_string(sock_server, join_string);
		sexp = rec_sexp(sock_server);
		if(!check_sexp(sexp, "join-accepted")) {
			cout << "Unabled to join game " << argv[3] << endl;
			goto GAME_SHUTDOWN;
		}

		// we should only try to start the game if we're the
		// player that just joined; otherwise, the server
		// will sorta freak out...
		send_string(sock_server, "(game-start)");
	}
	
	// the server is going to spit a ton of SEXP's at us;
	// wade through them until we get a "new-turn" msg.
	sexp = rec_sexp(sock_server);
	while(!check_sexp(sexp, "new-turn")) {
		// okay, what kind of data have we gotten from the server,
		// and what are we supposed to do about it?
		
		if(check_sexp(sexp, "game-start-accepted")) {
			// the server accepted our reqeust to begin the game;
			// we don't care, because earlier we just assumed
			// that it worked :|		
		}
		
		if(check_sexp(sexp, "unit-types")){
			// this should be sent only during this initialization:
			// it contains the details of each type of unit
			#ifdef DEBUG
			cout << "DEBUG: updating unit types" << endl;
			#endif
			update_unit_types((myAI*)&gameAI, sexp);
		}
		
		if(check_sexp(sexp, "status")) {
			// An actual game-status message: this might not
			// mean much right now, but try to parse it anyway.
			// Don't bail if it fails, but warn us.

			if(!game_status(gameAI, sexp, my_user_id)) {
				cout << "WARN: error parsing game status" << endl;
			}
		}
		
		// get another S-expression to test:
		sexp = rec_sexp(sock_server);
	}
	
	// okay, now sexp should contain a "new-turn" message
	// pull out who's turn it is, then start the main game loop
	whos_turn = string(sexp->list->next->val);
	
	#ifdef DEBUG
	cout << "DEBUG: got all our pregame stuff, starting main loop!" << endl;
	#endif
				
	while(!end_game) {
		// ask the server for an update:
		//send_string(sock_server, "(game-status)");
		//sexp = rec_sexp(sock_server);
		
		// we're not guaranteed to get a "status" message,
		// so handle anything unexpected before we get to
		// the main game loop
		while(!check_sexp(sexp, "status")) {

			#ifdef DEBUG
			cout << "DEBUG: waiting for game status, got \"" << sexp->list->val << "\" message" << endl;
			#endif

			if(check_sexp(sexp, "game-over")) {
				cout << "Game Over: ";
				string winner = string( sexp->list->next->val );
				if( winner == my_user_id )
					cout << "You Win!";
				else
					cout << "You Lose...";
				cout << endl;
				
				end_game = true;
				goto GAME_SHUTDOWN;
			}
			
			if(check_sexp(sexp, "new-turn")) {
				whos_turn = string(sexp->list->next->val);
				#ifdef DEBUG
				cout << "DEBUG: It's player " << whos_turn << "'s turn" << endl;
				#endif
			}
			
			sexp = rec_sexp(sock_server);	
		}
		
		if(!check_sexp(sexp, "status")) {
			cerr << "WARN: expected 'status', got '" << sexp->list->val << "'" << endl;
		}
		
		if(!game_status(gameAI, sexp, my_user_id)) {
			cerr << "WARN: couldn't parse game status!" << endl;
		}
		
		// update the turn number
		gameAI.turnNumber = atoi( sexp->list->next->list->val);
		
		if(whos_turn == my_user_id) {
		
			//cout << endl << "Starting my turn... ";
		
			// 4a. Run their custom 'Play()' function
			gameAI.Play();
			
			// 4b. Send their orders to the server
			while( gameAI.orders.size() != 0){
			
				string myOrder = gameAI.orders.front().s_expression;
				gameAI.orders.pop();
				
				// send this command to the server
				send_string(sock_server, myOrder);
				// there's no need to listen for an incoming message;
				// we assume errors will be handled at the beginning
				// of the next loop.
			}

			// let the server know we've finished out turn
			send_string(sock_server, "(end-turn)");
			//cout << "done with my turn" << endl;

		} else {
			// it's not your turn, so take it easy for a second
			//cout << "Not my turn, waiting...." << endl;
			#ifndef WIN32 // the usleep function insn't avaible in Windows
			//usleep(100000); // this helps prevent climbing to 100% CPU usage
			#endif
		}
		
		// get another sexp to jump-start the next iteration of the loop
		sexp = rec_sexp(sock_server);
		
	}
     
	GAME_SHUTDOWN:   
	cout << "Shutting down..." << endl;
	send_string(sock_server, "(leave-game)");
	sexp = rec_sexp(sock_server);
	send_string(sock_server, "(logout)");
	sexp = rec_sexp(sock_server);	
   
	//End of game
	#ifdef WIN32
	closesocket(sock_server);
	#else
	close(sock_server);
	#endif
   
   log_file.open(log_filename.c_str());
   if(log_file.fail())
   {
      cout << "Error writing to log file" << endl;
   }
   else
   {
      log_file << log_stream.str();
      log_file.close();
   }
   
	return 0;
}