Ejemplo n.º 1
0
  void start(int port){
    sout << "\n\nReciever started\n";
    pLt = new ListenThread(port, q_);
    try{
      pLt->start();
      std::string temp;std::string msg;
      int count = 0; std::string retmsg;
	  AcknowlegementMsg ackMsg;
      do{	
        temp = q_.deQ(); //extract the xml formate string to get the IP address
		XmlReader  rdmsg(temp);
		rdmsg.reset();
		if(temp != "stop"){
		while(rdmsg.next()){
			if(rdmsg.tag() == "IP")
				ackMsg.IP = rdmsg.body().c_str();
			if(rdmsg.tag() == "sourcePort")
				ackMsg.srcPort = atoi(rdmsg.body().c_str());
			if(rdmsg.tag() == "type")
				ackMsg.type = rdmsg.body().c_str();
			if(ackMsg.type == "text"){
					if(rdmsg.tag() == "body"){
						ackMsg.msg = rdmsg.body().c_str();
						msg = ackMsg.msg;
						_ackMsgQ.enQ(ackMsg);
						retmsg = comingmsgOperation(ackMsg.msg);
						ackMsg.retmsg = retmsg; 
						_ackMsgQ.enQ(ackMsg);
					}
			}
			if(rdmsg.tag() =="fileName") ackMsg.fileName = rdmsg.body().c_str();
			if(ackMsg.type =="file"){
				if(rdmsg.tag() == "body" ){
				    std::string inmsg = rdmsg.body(); //decode the body based on the blockSize
					int num = inmsg.size()/1024;
					for(size_t i=0;i<=num;i++){ //unchunk the file
						std::string tmp = inmsg.substr(i*1024,1024+1024*i);
						std::vector<char> vdecoded = Base64::decode(tmp);
		                std::string decodedMsg(vdecoded.begin(), vdecoded.end());
						ackMsg.msg += decodedMsg;
						ackMsg.retmsg = "fileRecieved";
					    writeToFile(ackMsg.msg,ackMsg.fileName);}
				}	_ackMsgQ.enQ(ackMsg);
				msg=ackMsg.msg;
				doLog((std::string("fetching message ") + msg).c_str());}}
		}if(temp == "stop") {
			msg = "stop";
			Sender sndr1(1,ackMsg.retmsg);
	        AcknowledgementThread t1(sndr1,_ackMsgQ);
            t1.start();
	        t1.join();
		}doLog((std::string("fetching message ") + msg).c_str());
        sout << "\n  Recieved from " << msg.c_str();
      } while(true);
    }catch(std::exception& ex){
      delete pLt;
	}catch(...){
      delete pLt;
    }
  }
void client::clientMain(int commandport, int checktemp, int senderid, int loopNum, std::string test,std::string tempport)
{		
		std::mutex* lock = new std::mutex;
		Sender sndr1("send", loopNum, senderid * 2 + 1, checktemp, tempport, test);// sndr2("send", loopNum, senderid * 2 + 2, checktemp, tempport, test);
		Receiver* rcvr1 = new Receiver;//create sender and receiver 
		VaultDispatcher* Disp1;
		Disp1 = new VaultDispatcher("disp");
		EchoCommunicator* Echo1;
		FileCommunicator* File1;
		Echo1 = new EchoCommunicator("echo");
		File1 = new FileCommunicator("file");
		Disp1->Register(Echo1);
		Disp1->Register(File1);
		Echo1->Dispatcher(Disp1);
		File1->Dispatcher(Disp1);
		rcvr1->LOCK(lock);//assign lock to each of the communicator 
		Disp1->LOCK(lock);
		Echo1->LOCK(lock);
		File1->LOCK(lock);
		sndr1.LOCK(lock);
		std::thread sender1(&Sender::start, &sndr1, "127.0.0.1", 8080);
		std::thread AD(&VaultDispatcher::ProcessMsg1, Disp1);//open the thread to each of the processMsg function
		std::thread EC(&EchoCommunicator::ProcessMsg1, Echo1);
		std::thread FC(&FileCommunicator::ProcessMsg1, File1);
		rcvr1->DISP(Disp1);
		rcvr1->start(commandport);
		AD.join();
		EC.join();
		FC.join();
		sender1.join();
		while (Disp1->DisplayQ().size() != 0)
		{
			std::string getresult = Disp1->DisplayQ().deQ();
			display += '?' + getresult;
		}
}