示例#1
0
// Handle the MDF message. Store the map and pass on
void BaseBot::process_mdf( TokenMessage &incoming_message )
{
	int set_map_result;
	
	set_map_result = m_map_and_units->set_map( incoming_message );

	if ( set_map_result != ADJUDICATOR_NO_ERROR )
	{
		log_error("Failed to set map - Error location %d", set_map_result );
	}

	process_mdf_message( incoming_message );

	// If the map wasn't sent by request, then reply to accept the map
	if ( m_map_requested == false )
	{
		send_message_to_server( TOKEN_COMMAND_YES & m_map_message );
	}
	else
	{
		// The map was requested following an IAM, so also request a HLO, SCO and NOW.
		send_message_to_server( TokenMessage( TOKEN_COMMAND_HLO ) );
		send_message_to_server( TokenMessage( TOKEN_COMMAND_ORD ) );
		send_message_to_server( TokenMessage( TOKEN_COMMAND_SCO ) );
		send_message_to_server( TokenMessage( TOKEN_COMMAND_NOW ) );

		m_map_requested = false;
	}
}
示例#2
0
void BaseBot::send_broadcast_to_server( TokenMessage broadcast_message )
{
	TokenMessage receiving_powers;
	int power_counter;
	SentPressInfo sent_press_record;

	for ( power_counter = 0; power_counter < m_map_and_units->number_of_powers; power_counter++ )
	{
		if ( ( m_map_and_units->power_played.get_subtoken() != power_counter )
		  && ( m_cd_powers.find( Token( CATEGORY_POWER, power_counter ) ) == m_cd_powers.end() ) )
		//#pragma message("**** Also check for eliminated powers")
		{
			receiving_powers = receiving_powers + Token( CATEGORY_POWER, power_counter );
		}
	}

	sent_press_record.original_receiving_powers = receiving_powers;
	sent_press_record.receiving_powers = receiving_powers;
	sent_press_record.press_message = broadcast_message;
	sent_press_record.resend_partial = true;
	sent_press_record.is_broadcast = true;

	m_sent_press.push_back( sent_press_record );

	send_message_to_server( TOKEN_COMMAND_SND & receiving_powers & broadcast_message );
}
示例#3
0
int main(int argc, char *argv[])
{
    parse_arguments_and_flags(argc, argv);
    uint16_t port = get_port_from_args();
    struct in_addr serverIP = get_IP_from_args();
    handle_ctrl_c();

    printf("started with IP: %s, port: %d... \n", args[1], port);
    create_socket();

    connect_to_server(port, serverIP);
    printf("Connected...\nEnter messages, max size: %d, ctrl + d to quit, *QUIT* to kill server \n", BUFSIZ);

    int exitv = 0;
    do
    {
        char buffer[BUFSIZ];
        get_input_from_user(buffer);

        exitv = send_message_to_server(buffer);
        if (0 != exitv)
        {
            //get return from the server and print it
            char recvbuffer[BUFSIZ];
            recv(sock, recvbuffer, BUFSIZ, 0);
            printf("server sent back: \"%s\"\n", recvbuffer);
        }
    } while (0 != exitv);

    close(sock);
    exit(EXIT_SUCCESS);
}
示例#4
0
文件: alg.cpp 项目: AoD314/pat
	Alg::Alg(PAT_ALGORITHM alg)
	{
		port = 13314;
		socket = new QTcpSocket;
		socket->connectToHost(QHostAddress(QString::fromUtf8("127.0.0.1")), port);

		std::vector<std::string> msgs;
		msgs.push_back("alg");
		switch(alg)
		{
            case PAT_ALG_RND:
				msgs.push_back("rnd");
				break;
            case PAT_ALG_BF:
				msgs.push_back("bf");
				break;
            case PAT_ALG_GR:
				msgs.push_back("gr");
				break;
            case PAT_ALG_DHS:
				msgs.push_back("dhs");
				break;
		}
		send_message_to_server(msgs);
	}
示例#5
0
文件: alg.cpp 项目: AoD314/pat
	void Alg::set_n(size_t n)
	{
		std::vector<std::string> msgs;
		msgs.push_back("params");
		msgs.push_back("n");
		msgs.push_back(to_str<size_t>(n));
		send_message_to_server(msgs);
	}
示例#6
0
void BaseBot::send_orders_to_server( void )
{
	TokenMessage sub_command;

	sub_command = m_map_and_units->build_sub_command();

	if ( sub_command.get_message_length() > 1 )
	{
		send_message_to_server( m_map_and_units->build_sub_command() );
	}
}
示例#7
0
文件: alg.cpp 项目: AoD314/pat
	void Alg::init(const std::string & name, Gen gen)
	{
		std::vector<std::string> msgs;
		msgs.push_back("init");
		msgs.push_back(name);
		msgs.push_back(gen.get_type());

		std::vector<Number> l = gen.get_list();
		msgs.push_back(to_str(l.size()));

		send_message_to_server(msgs, l);
	}
示例#8
0
void BaseBot::send_press_to_server( TokenMessage press_to, TokenMessage press_message, bool resend_partial )
{
	SentPressInfo sent_press_record;

	sent_press_record.original_receiving_powers = press_to;
	sent_press_record.receiving_powers = press_to;
	sent_press_record.press_message = press_message;
	sent_press_record.resend_partial = resend_partial;
	sent_press_record.is_broadcast = false;

	m_sent_press.push_back( sent_press_record );

	send_message_to_server( TOKEN_COMMAND_SND & press_to & press_message );
}
示例#9
0
// Handle an incoming FRM message. Default version replies with HUH( message ). TRY().
void BaseBot::process_frm_message( TokenMessage &incoming_message )
{
	TokenMessage huh_message;
	TokenMessage try_message;
	Token from_power;
	TokenMessage press_message;
	TokenMessage message_id;

	message_id = incoming_message.get_submessage( 1 );
	from_power = message_id.get_token();
	press_message = incoming_message.get_submessage( 3 );

	if ( ( press_message.get_token( 0 ) != TOKEN_COMMAND_HUH )
	  && ( press_message.get_token( 0 ) != TOKEN_PRESS_TRY ) )
	{
		// Changed as message IDs no longer used.
		huh_message = TOKEN_COMMAND_SND & from_power & ( TOKEN_COMMAND_HUH & ( TOKEN_PARAMETER_ERR + press_message ) );
		try_message = TOKEN_COMMAND_SND & from_power & ( TOKEN_PRESS_TRY & try_message );

		send_message_to_server( huh_message );
		send_message_to_server( try_message );
	}
}
示例#10
0
int send_directional_message(char* direction, int client_msqid){
  //Send specific directional message.
  int message_set = 0;
  char* right = "right\n"; 
  char* left = "left\n"; 
  char* up = "up\n"; 
  char* down = "down\n";

  char* id = NULL;
  id = (char*)malloc(sizeof(char)* get_len_id(client_msqid));
  sprintf(id, "%d", client_msqid);
  direction = strtok(direction, " ");

  struct msg m;
  m.mtype = M_TYPE;
  memset(&m.mtext, 0, 10);

  if (!strcmp(direction, right)){
    strcat(m.mtext, "R_");
    strcat(m.mtext, id);
    message_set = 1;
  }
  if (!strcmp(direction, left)){
    strcat(m.mtext, "L_");
    strcat(m.mtext, id);
    message_set = 1;
  }
  if (!strcmp(direction, up)){
    strcat(m.mtext, "U_");
    strcat(m.mtext, id);
    message_set = 1;
  }
  if (!strcmp(direction, down)){
    strcat(m.mtext, "D_");
    strcat(m.mtext, id);
    message_set = 1;
  }

  if (message_set == 0){
    printf("Please enter valid move\n");
  }else {
    send_message_to_server(SERVER_MSQID, &m, strlen(m.mtext), MSG_NOERROR);
  }
  free(id);
  return message_set;
}
示例#11
0
void BaseBot::send_name_and_version_to_server( char name[], char version[] )
{
	TokenMessage name_message;
	CString name_in_quotes;
	CString version_in_quotes;
	TokenMessage name_tokens;
	TokenMessage version_tokens;

	name_in_quotes = "'" + (CString)( name ) + "'";
	version_in_quotes = "'" + (CString)( version ) + "'";

	name_tokens.set_message_from_text( (string)( (const char *)( name_in_quotes ) ) );
	version_tokens.set_message_from_text ( (string)( (const char *)( version_in_quotes ) ) );

	name_message = TOKEN_COMMAND_NME & name_tokens & version_tokens;

	send_message_to_server( name_message );
}
示例#12
0
// Handle the MAP message. Store the map name, send the MDF, then pass on
void BaseBot::process_map( TokenMessage &incoming_message )
{
	TokenMessage mdf_message( TOKEN_COMMAND_MDF );
	TokenMessage name_submessage;

	// Store the map name
	name_submessage = incoming_message.get_submessage( 1 );
	m_map_and_units->map_name = name_submessage.get_message_as_text().c_str();
	remove_quotes( m_map_and_units->map_name );

	// Send an MDF
	send_message_to_server( mdf_message );

	// Store the map message
	m_map_message = incoming_message;

	// Call the users handler function
	process_map_message( incoming_message );
}
示例#13
0
char* send_JOIN(int msq_id, int num_players){
  //Send join message to the server.
  int server_msqid;
  char* id = NULL;
  id = (char*)malloc(sizeof(char)* get_len_id(msq_id));
  struct msg j;
  j.mtype = M_TYPE;
  memset(&j.mtext, 0, 100);
  strcat(j.mtext, "J_");
  sprintf(id, "%d_%d", msq_id, num_players);
  strcat(j.mtext,id );
  strcat(j.mtext,"_");
  strcat(j.mtext, MY_NAME);
  printf("join message: %s\n", j.mtext);
  get_server_msqid();

  send_message_to_server(SERVER_MSQID, &j, strlen(j.mtext), MSG_NOERROR);
  free(id);
}
示例#14
0
void BaseBot::send_to_reduced_powers( SentPressList::iterator &sent_press_iterator, Token &cd_power )
{
	TokenMessage receiving_powers;
	TokenMessage reduced_powers;
	int power_counter;

	receiving_powers = sent_press_iterator->receiving_powers;	

	for ( power_counter = 0; power_counter < receiving_powers.get_message_length(); power_counter++ )
	{
		if ( receiving_powers.get_token( power_counter ) != cd_power )
		{
			reduced_powers = reduced_powers + receiving_powers.get_token( power_counter );
		}
	}

	sent_press_iterator->receiving_powers = reduced_powers;

	send_message_to_server( TOKEN_COMMAND_SND & reduced_powers & sent_press_iterator->press_message );
}
示例#15
0
// Handle an incoming REJ( NME() ) message.
void BaseBot::process_rej_nme_message( TokenMessage &incoming_message, TokenMessage &message_parameters )
{
	bool attempt_reconnect;				// Whether to try and reconnect
	Token power_token = 0;				// The token for the power to reconnect as
	int passcode = 0;					// The passcode to reconnect as
	Token passcode_token;				// The passcode as a token

	attempt_reconnect = get_reconnect_details( power_token, passcode );

	if ( attempt_reconnect )
	{
		// Send an IAM message
		passcode_token.set_number( passcode );
		send_message_to_server( TOKEN_COMMAND_IAM & power_token & passcode_token );
	}
	else
	{
		// Disconnect
		disconnect_from_server();
	}
}
示例#16
0
// Handle an incoming THX message. Default supplies a simple replacement order if not MBV.
void BaseBot::process_thx_message( TokenMessage &incoming_message )
{
	TokenMessage order;					// The order submitted
	Token note;							// The order note returned
	TokenMessage unit;					// The unit ordered
	TokenMessage new_order;				// The replacement order to submit
	bool send_new_order = false;		// Whether to send a new order

	order = incoming_message.get_submessage( 1 );
	unit = order.get_submessage( 0 ).enclose();
	note = incoming_message.get_submessage( 2 ).get_token();
	
	if ( note == TOKEN_ORDER_NOTE_MBV )
	{
		// Everything is good. Nothing to do.
	}

	if ( ( note == TOKEN_ORDER_NOTE_FAR )
	  || ( note == TOKEN_ORDER_NOTE_NSP )
	  || ( note == TOKEN_ORDER_NOTE_NSU )
	  || ( note == TOKEN_ORDER_NOTE_NAS )
	  || ( note == TOKEN_ORDER_NOTE_NSF )
	  || ( note == TOKEN_ORDER_NOTE_NSA ) )
	{
		// Illegal movement order. Replace with a hold order.
		new_order = unit + TOKEN_ORDER_HLD;
		send_new_order = true;
	}

	if ( note == TOKEN_ORDER_NOTE_NVR )
	{
		// Illegal retreat order. Replace with a disband order
		new_order = unit + TOKEN_ORDER_DSB;
		send_new_order = true;
	}

	if ( ( note == TOKEN_ORDER_NOTE_YSC )
	  || ( note == TOKEN_ORDER_NOTE_ESC )
	  || ( note == TOKEN_ORDER_NOTE_HSC )
	  || ( note == TOKEN_ORDER_NOTE_NSC )
	  || ( note == TOKEN_ORDER_NOTE_CST ) )
	{
		// Illegal build order. Replace with a waive order
		new_order = unit.get_submessage( 0 ) + TOKEN_ORDER_WVE;
		send_new_order = true;
	}

	if ( ( note == TOKEN_ORDER_NOTE_NYU )
	  || ( note == TOKEN_ORDER_NOTE_NRS ) )
	{
		// Illegal order, but not much we can do about it
	}

	if ( ( note == TOKEN_ORDER_NOTE_NRN )
	  || ( note == TOKEN_ORDER_NOTE_NMB )
	  || ( note == TOKEN_ORDER_NOTE_NMR ) )
	{
		// Order wasn't needed in the first place!
	}

	if ( ( send_new_order ) && ( new_order != order ) )
	{
		log_error("THX returned %s for order '%s'. Replacing with '%s'",
					incoming_message.get_submessage( 2 ).get_message_as_text().c_str(),
					order.get_message_as_text().c_str(),
					new_order.get_message_as_text().c_str() );

		send_message_to_server( new_order );
	}
	else if ( note != TOKEN_ORDER_NOTE_MBV )
	{
		log_error("THX returned %s for order '%s'. No replacement order sent.",
					incoming_message.get_submessage( 2 ).get_message_as_text().c_str(),
					order.get_message_as_text().c_str() );
	}
}
示例#17
0
void BaseBot::send_nme_or_obs( void )
{
	TokenMessage obs_message( TOKEN_COMMAND_OBS );

	send_message_to_server( obs_message );
}
示例#18
0
void BaseBot::request_map( void )
{
	m_map_requested = true;

	send_message_to_server( TokenMessage( TOKEN_COMMAND_MAP ) );
}
示例#19
0
// Handle an incoming LOD message. Default replies with REJ (LOD (...) )
void BaseBot::process_lod_message( TokenMessage &incoming_message )
{
	send_message_to_server( TOKEN_COMMAND_REJ & incoming_message );
}