Beispiel #1
0
void Host::delete_content(int cid){
	std::vector<int>::iterator it;
	it = find(cids_.begin(), cids_.end(), cid);
	if(it!=cids_.begin())
		cids_.erase(it);

	/* Delete the content file */
	Content c;
	string filename = c.get_content_name_in_host(id_,cid);
	remove(filename.c_str());
}
Beispiel #2
0
void Host::assign_content(int cid){
	std::vector<int>::iterator it;
	it = find (cids_.begin(), cids_.end(), cid);
	if(it!=cids_.end())
		return;
	cids_.push_back(cid);

	/* copy the content file into the folder */
	Content c;
	string destfile = c.get_content_name_in_host(id_,cid);
	copycontent(c.get_content_name(cid).c_str(), destfile.c_str());
}
Beispiel #3
0
void Host::host_receive_message(int HID, int srcport, int dstport){
	try{
		//cout << "[HOST RECEIVE THREAD]  From: " << srcport << "(" << (short)srcport << "). To: " << dstport << "(" << (short)dstport << ")\n";
		//configure receiving port
		const char* hname = "localhost";
		Address * my_addr = new Address(hname, (short)dstport);
		//LossyReceivingPort *my_port = new LossyReceivingPort(0);
		ReceivingPort *my_port = new ReceivingPort();
		my_port->setAddress(my_addr);
		my_port->init();

		Message *m = new Message();
		Packet *p;
		while (1)
		{
			p = my_port->receivePacket();
			if(p!=NULL){
				int type = m->get_packet_type(p);
				//cout << "[H" << id_ << "] [Received] Type: " << type << "\n";
				switch (type){
				/*
				 * Message.TYPE_REQUEST
				 */
				case 0:{
					/* make RESPONSE packet */
					int CID = m->get_packet_CID(p);
					int HID = m->get_packet_HID(p);
					Content c;
					string filename = c.get_content_name_in_host(id_,CID);
					struct stat buffer;
					if(stat(filename.c_str(), &buffer) != 0) break;	/* if this file doesn't exist, do nothing... */
					Packet *p = m->make_response_packet(CID, HID, filename.c_str());

					/* queue the RESPONSE packet to the sending_queue */
					{
						boost::unique_lock<boost::timed_mutex> lock(* to_send_packets_mutex_ , boost::try_to_lock);
						bool getLock = lock.owns_lock();
						if(!getLock){
							getLock = lock.timed_lock(boost::get_system_time() + boost::posix_time::milliseconds(500));
						}
						if(getLock){
							to_send_packets_.push_back(p);
						}
						boost::timed_mutex *m = lock.release();
						m->unlock();
					}
					break;
				}
				/*
				* Message.TYPE_RESPONSE
				*/
				case 1:{
					/* if this host is not waiting, discard */
					if(!isWaiting){
						break;
					}

					/* else, store the content to disk  */
					int size = p->getPayloadSize();
					char *payload = p->getPayload();
					int CID = m->get_packet_CID(p);
					Content c;
					ofstream outfile(c.get_content_name_in_store(id_, CID).c_str());
					outfile.write(payload, size);
					outfile.close();
					isWaiting = false;
					cout << "\n[H" << id_ << "]Content CID = " << CID << " has been delivered! \n" ;
					break;
				}
				}
			}
		}

	}
	catch(const char *reason ){
		cerr << "Exception:" << reason << endl;
		exit(-1);
	}
}