Exemplo n.º 1
0
t_instance_outcome cInstanceObject::TryToBecomeInstance(int inst) { // test instance, tries to become it
	_info("Trying instance inst="<<inst);

	const std::string igrp_name = std::string("instancegroup_") 
		+ m_program_name // testprogram
		+ ( (m_range==e_range_user) ? ("_user_n_"+cMyNamedMutex::EscapeMutexNameWithLen(GetUserName())) : ( "_user_ANY" ) ) // testprogram_alice  testprogram
		+ ( (m_range==e_range_maindir) ? ("_dir_n_"+cMyNamedMutex::EscapeMutexNameWithLen(GetDirName())) : ( "_dir_ANY" )   )
	;

	_info("igrp_name=" << igrp_name);

	boost::interprocess::permissions mutex_curr_perms;
	if (m_range==e_range_system) mutex_curr_perms.set_unrestricted(); // others users on same system need to synchronize with this

	std::ostringstream mutex_name_str; mutex_name_str << igrp_name << "_instM" << inst;
	std::string mutex_name = mutex_name_str.str();

	// mutex_name="name1foo1";
	_info("Will try to become instance as mutex_name="<<mutex_name);

	m_curr_mutex.reset( new cMyNamedMutex ( boost::interprocess::open_or_create, mutex_name.c_str(), mutex_curr_perms ) );
	// we created this mutex or it existed

	bool curr_locked = m_curr_mutex->try_lock_msg(str_to_vec("La"));// GetProcessIdentification() ) );
	if (curr_locked) { // we locked it! 
		_info("WE BECOME INSTANCE: Created and locked the Mc - we became the instance at inst="<<inst);
		m_curr_mutex->SetOwnership(true); // I own this mutex e.g. M5

		// I am probably the leader
		
		// ... TODO ... ping all lower numbers are they responding after all to ping ...


		_info("I will start responding to pings (curr)");
		m_pingable_curr.reset(new cInstancePingable( mutex_name, mutex_curr_perms )); // start responding to ping
		m_pingable_curr->Run();

		return e_instance_i_won; // we created and locked (or just locked - reclaimed) an instance
	}

	_info("Can not create/lock the Mc so we lost or it is dead. inst="<<inst);
	bool other_is_alive = PingInstance(mutex_name, mutex_curr_perms);

	if (! other_is_alive) {
		_info("The other instance is dead!");
		return e_instance_seems_dead;
	}
	
	return e_instance_i_lost;
}
Exemplo n.º 2
0
World::World(const std::string& file_path)
{
	setup_engine();
	// Open the file
	std::fstream fin(file_path);
	std::string command;

	// Insert all of the entities we will need.
	entities.insert(std::make_pair("player", std::vector<Entity*>()));
	entities.insert(std::make_pair("map", std::vector<Entity*>()));
	entities.insert(std::make_pair("zombie", std::vector<Entity*>()));
	entities.insert(std::make_pair("item", std::vector<Entity*>()));



	// Iterate over each line in the file.
	std::cout << "* Constructing World" << std::endl;
	while (std::getline(fin, command)) {
		// Create a vector to store the tokens, and convert the line to a string stream.
		std::vector<std::string> tokens;
		str_to_vec(command, tokens);
		
		if (tokens.size() == 4)
		{
			std::cout << " -> Entity - Type: " << tokens[0] << " File: " << tokens[1] << " Position: " << tokens[2] << " Rotation: " << tokens[3] << std::endl;
			
			// Construct the entity, and create position and rotation values.
			irr::core::vector3df position(str_to_vec3df(tokens[2]));
			irr::core::vector3df rotation(str_to_vec3df(tokens[3]));
			std::string path = "../Data/data/";
			path += tokens[1] + ".txt";

			// Simple dispatcher for each of the valid types.
			if (tokens[0] == "player")      entities["player"].push_back(new Player(this, path, position, rotation));
			else if (tokens[0] == "item")   entities["item"].push_back(new Item(this, path, position, rotation));
			else if (tokens[0] == "map")    entities["map"].push_back(new Map(this, path, position, rotation));
			else if (tokens[0] == "zombie") entities["zombie"].push_back(new Zombie(this, path, position, rotation));
		}
		else
		{
			std::cout << " -> Entity - Malformed command, ignoring" << std::endl;
		}
	}
	std::cout << "* Constructing World Finished" << std::endl;
}