bool MoleculeDeserializer::deserialize(const void *data, size_t size)
{
  ArrayInputStream ais(data, size);
  CodedInputStream cis(&ais);

  // Read the atoms
  if (!this->deserializeAtomicNumbers(&cis))
    return false;

  // Read the positions2d
  if (!this->deserializePositions2d(&cis))
    return false;

  // Read the positions3d
  if (!this->deserializePostions3d(&cis))
    return false;

  // Read bond pairs
  if (!this->deserializeBondPairs(&cis))
    return false;

  // Read bond orders
  if (!this->deserializeBondOrders(&cis))
    return false;

  return true;
}
    GameEngineMessage* NetUtils::ReadBody( net::Socket* socket, google::protobuf::uint32 size )
    {
        int bytecount;
        GameEngineMessage* payload =  new GameEngineMessage();
        char buffer [ size + 4 ];//size of the payload and hdr
        //Read the entire buffer including the hdr
        bytecount = socket->ReceiveWaitAll( (void *)buffer, 4 + size );

        printf( "Second read byte count is %d\n", bytecount );

        //Assign ArrayInputStream with enough memory
        google::protobuf::io::ArrayInputStream ais( buffer, size + 4 );
        CodedInputStream coded_input( &ais );
        //Read an unsigned integer with Varint encoding, truncating to 32 bits.
        coded_input.ReadVarint32( &size );
        //After the message's length is read, PushLimit() is used to prevent the CodedInputStream
        //from reading beyond that length. Limits are used when parsing length-delimited
        //embedded messages
        google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit( size );
        //De-Serialize
        payload->ParseFromCodedStream( &coded_input );
        //Once the embedded message has been parsed, PopLimit() is called to undo the limit
        coded_input.PopLimit( msgLimit );
        //Print sender of message
        printf( "Message from %s\n", socket->GetSenderAddress().ToString().c_str() );
        //Print the message
        printf( "Message is %s\n", payload->DebugString().c_str() );

        return payload;
    }
Ejemplo n.º 3
0
void download_p2p(int sockfd){

    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 pkg_size = readHdr(bufferFST);
    int byteCount;
    file::File request_file;
    char buffer[pkg_size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&pkg_size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size);
    request_file.ParseFromCodedStream(&coded_input);

    for(auto it=file_sets.begin(); it!=file_sets.end(); ++it){
        if( it-> first == request_file.file_name() && !it->second.empty() ){
            sendCheck(sockfd, true);
            send_download_info(sockfd, it->second);
            return;
        }
    }
    sendCheck(sockfd, false);
    return;
}
Ejemplo n.º 4
0
google::protobuf::uint32 readHdr(char *buf){
	google::protobuf::uint32 size;
	google::protobuf::io::ArrayInputStream ais(buf,4);
	google::protobuf::io::CodedInputStream coded_input(&ais);
	coded_input.ReadVarint32(&size); //Decode the HDR and get the size
	//cout<<"size of payload is "<<size<<endl;
	return size;
}
    google::protobuf::uint32 NetUtils::ReadHeader( char *buf )
    {
        google::protobuf::uint32 size;
        google::protobuf::io::ArrayInputStream ais( buf,4 );
        CodedInputStream coded_input( &ais );
        coded_input.ReadVarint32( &size );//Decode the HDR and get the size
        printf( "Size of payload is %d\n", size );

        return size;
    }
Ejemplo n.º 6
0
std::string CSimulation2::GetAIData()
{
	ScriptInterface& scriptInterface = GetScriptInterface();
	JSContext* cx = scriptInterface.GetContext();
	JSAutoRequest rq(cx);
	JS::RootedValue aiData(cx, ICmpAIManager::GetAIs(scriptInterface));
	
	// Build single JSON string with array of AI data
	JS::RootedValue ais(cx);
	if (!scriptInterface.Eval("({})", &ais) || !scriptInterface.SetProperty(ais, "AIData", aiData))
		return std::string();
	
	return scriptInterface.StringifyJSON(&ais);
}
Ejemplo n.º 7
0
void readBody(int csock, google::protobuf::uint32 siz){
	int byteCount;
	login::Login login;
	char buffer[siz+4];
  	if( ( byteCount = recv(csock, (void *)buffer, 4+siz, MSG_WAITALL) )== -1 ){
        fprintf(stderr, "Error receiving data %d\n", errno);
    }
    google::protobuf::io::ArrayInputStream ais(buffer,siz+4);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&siz);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(siz);
    login.ParseFromCodedStream(&coded_input);
    coded_input.PopLimit(msgLimit);
    std::cout << login.DebugString();
}
Ejemplo n.º 8
0
login::DeleteInfo readDeleteInfo(int csock, google::protobuf::uint32 size){
    //recv login
    int byteCount;
    login::DeleteInfo info;
    char buffer[size + HDR_SIZE];
    if( ( byteCount = recv(csock, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) )== -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size);
    info.ParseFromCodedStream(&coded_input);

    return info;
}
Ejemplo n.º 9
0
bool readCheck(int csock, google::protobuf::uint32 size){
    //recv check
    int byteCount;
    check::Check check;
    char buffer[size + HDR_SIZE];
    if( ( byteCount = recv(csock, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) )== -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size);
    check.ParseFromCodedStream(&coded_input);

    //parse check
    if( check.check() == true )
        return true;
    return false;
}
Ejemplo n.º 10
0
void regist_check(int sockfd){
    //recv regist
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 size = readHdr(bufferFST);
    int byteCount;
    regist::Regist regist;
    char buffer[size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size);
    regist.ParseFromCodedStream(&coded_input);
    coded_input.PopLimit(msgLimit);
    //std::cout << regist.DebugString();

    //check id and password
    Data::Data newRegist;
    newRegist.set_id( regist.id() );
    newRegist.set_password( regist.passwd() );

    for(int i=0; i<loginData.logindata_size(); ++i){
        if( loginData.logindata(i).id() == newRegist.id() ){
            sendCheck(sockfd, false);
            return;
        }
    }

    Data::Data* data = loginData.add_logindata();
    data -> set_id(regist.id());
    data -> set_password(regist.passwd());

    update();
    sendCheck(sockfd, true);
}
Ejemplo n.º 11
0
ACTION readAction(int csock, google::protobuf::uint32 size){
    //recv action
    int byteCount;
    action::Action action;
    char buffer[size + HDR_SIZE];
    if( ( byteCount = recv(csock, (void *)buffer, size + HDR_SIZE, MSG_WAITALL) )== -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(size);
    action.ParseFromCodedStream(&coded_input);

    //parse action
    if( action.action() == "login" )
        return LOGIN;
    if( action.action() == "regist" )
        return REGIST;
    if( action.action() == "deleteaccount" )
        return DELETEACCOUNT;
    if( action.action() == "searchinfo" )
        return SEARCHINFO;
    if( action.action() == "download" )
        return DOWNLOAD;
    if( action.action() == "chat" )
        return CHAT;
    if( action.action() == "logout" )
        return LOGOUT;
    if( action.action() == "sendfileinfo" )
        return RECVFILEINFO;
    if( action.action() == "onlineinfo" )
        return ONLINEINFO;
    if( action.action() == "portrequest" )
        return PORTREQUEST;
    if( action.action() == "messagetopeer" )
        return PEERMESSAGE;
    if( action.action() == "askfile" )
        return ASKFILE;
    return UNDEFINE;
 }
Ejemplo n.º 12
0
void chat(int sockfd){

    // recv name
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 pkg_size = readHdr(bufferFST);
    int byteCount;
    online::OnlinePerson person;
    char buffer[pkg_size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&pkg_size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size);
    person.ParseFromCodedStream(&coded_input);

    // check whether online
    for(int i=0; i<loginData.logindata_size(); ++i){
        if( loginData.logindata(i).online() == true ){
            if( loginData.logindata(i).id() != "" &&
                loginData.logindata(i).id() == person.name() ){
                sendCheck(sockfd, true);
                send_port(sockfd, loginData.logindata(i).port(), loginData.logindata(i).ip());
                return;
            }
        }
    }
    sendCheck(sockfd, false);
    printf("!ok\n");

}
Ejemplo n.º 13
0
void recv_file_info(int sockfd, std::map<std::string, std::set<std::string> > &file_sets, login::Login user){
    int count;
    char bufferFST[4];
    count = recv(sockfd, bufferFST, 4, MSG_PEEK);
    if(count == -1)
        perror("Recv with error");

    google::protobuf::uint32 pkg_size = readHdr(bufferFST);
    int byteCount;
    file::Files files;
    char buffer[pkg_size + HDR_SIZE];
    if( ( byteCount = recv(sockfd, (void *)buffer, pkg_size + HDR_SIZE, MSG_WAITALL) ) == -1 ){
        perror("Error recviving data");
    }
    google::protobuf::io::ArrayInputStream ais(buffer, pkg_size + HDR_SIZE);
    google::protobuf::io::CodedInputStream coded_input(&ais);
    coded_input.ReadVarint32(&pkg_size);
    google::protobuf::io::CodedInputStream::Limit msgLimit = coded_input.PushLimit(pkg_size);
    files.ParseFromCodedStream(&coded_input);

    printf("%sFile update: \n", ANSI_COLOR_GREEN);
    for(int i=0; i<files.files_size(); ++i){
        file::File file = files.files(i);
        if( file_sets.find( file.file_name() ) == file_sets.end() ){
            file_sets[file.file_name()].insert(user.id());
            std::cout << file.file_name() << ":";
            print_file(file_sets[file.file_name()]);
        }
        else{
            file_sets[file.file_name()].insert(user.id());
            std::cout << file.file_name() << ":";
            print_file(file_sets[file.file_name()]);
        }
    }
    printf("%s\n", ANSI_COLOR_RESET);
    update();
}
Ejemplo n.º 14
0
void MainWindow::on_FPS_clicked()
{

    //initial graph
    graphs gr;
    gr.setModal(true);

    int k = 0;
    while(k < COUNT){

        TotalFitness = 0;
        population.clear();
        newpopulation.clear();

        generatePopulations(false);

        //sort by fitness
        sort(this->population.begin(), this->population.end(), by_fitness());

        int j = 0;
        while(j < COUNT){

            calc_prob_ranges(false);

            //offsprings
            for(int i=0; i<((COUNT/2)/2); i++){
                float val = randomRange(0, 1);
                temp = generateOffspring(val);

                offsprint_x1 = temp.x;
                offsprint_y1 = temp.y;

                val = randomRange(0, 1);
                temp = generateOffspring(val);

                offsprint_x2 = temp.x;
                offsprint_y2 = temp.y;

                //mutation process
                mutation(offsprint_x1, offsprint_y2, offsprint_x2, offsprint_y1, false);
            }

            //merge new with old population
            population.insert(population.end(), newpopulation.begin(), newpopulation.end());

            //clear new population
            newpopulation.clear();

            //sort by compute
            sort(population.begin(), population.end(), by_fitness());

            // erase unecessary elements/population:
            population.erase(population.begin()+COUNT,population.end());

            //cout << "population for new iteration" << endl;
            TotalFitness = 0;
            for(int i=0; i < population.size(); i++ ){
                TotalFitness = TotalFitness + population[i].fitness;
                //cout << population[i].x << "," << population[i].y << "," << population[i].fitness << endl;
            }

            //best of iteration
            best.push_back(population[0].fitness);
            //cout << "fps best" << population[0].fitness << endl;
            //avg of iteration
            avg.push_back(population[(COUNT/2)-1].fitness);

            j++;

        }
        k++;
        TotalFitness = 0;
        pso();
        ais();
        gr.data.push_back({best, avg, pso_best, ais_best, ais_avg,COUNT});
        reverse(pso_best.begin(), pso_best.end());
        reverse(ais_best.begin(), ais_best.end());
        reverse(ais_avg.begin(), ais_avg.end());
        reverse(best.begin(), best.end());
        reverse(avg.begin(), avg.end());
        best_best.push_back(best[0]);
        avg_avg.push_back(avg[0]);
        ais_avg_avg.push_back(ais_avg[0]);
        pso_bb.push_back(pso_best[0]);
        pso_best.clear();
        ais_bb.push_back(ais_best[0]);
        ais_best.clear();
        ais_avg.clear();
        best.clear();
        avg.clear();
    }

    gr.data.push_back({best_best,avg_avg, pso_bb, ais_bb, ais_avg_avg,COUNT});
    best_best.clear();
    avg_avg.clear();
    ais_bb.clear();
    pso_bb.clear();
    ais_avg_avg.clear();

//    for(int i=0; i < COUNT; i++ ){
//        gr.best.push_back({best[i].fitness});
//        gr.avg.push_back({avg[i].fitness});
//    }
//        cout << gr.best.size() << endl;

//    gr.generation = COUNT;


    gr.makePlot(0);

    gr.exec();

}
Ejemplo n.º 15
0
void PlayerSettingsControl::CreateWidgets()
{
	// To prevent recursion, don't handle GUI events right now
	m_InGUIUpdate = true;

	// Load default civ and player data
	wxArrayString civNames;
	wxArrayString civCodes;
	AtlasMessage::qGetCivData qryCiv;
	qryCiv.Post();
	std::vector<std::string> civData = *qryCiv.data;
	for (size_t i = 0; i < civData.size(); ++i)
	{
		AtObj civ = AtlasObject::LoadFromJSON(civData[i]);
		civNames.Add(wxString(civ["Name"]));
		civCodes.Add(wxString(civ["Code"]));
	}

	// Load AI data
	ArrayOfAIData ais(AIData::CompareAIData);
	AtlasMessage::qGetAIData qryAI;
	qryAI.Post();
	AtObj aiData = AtlasObject::LoadFromJSON(*qryAI.data);
	for (AtIter a = aiData["AIData"]["item"]; a.defined(); ++a)
	{
		ais.Add(new AIData(wxString(a["id"]), wxString(a["data"]["name"])));
	}

	// Create player pages
	AtIter playerDefs = m_PlayerDefaults["item"];
	if (playerDefs.defined())
		++playerDefs;	// Skip gaia
	
	for (size_t i = 0; i < MAX_NUM_PLAYERS; ++i)
	{
		// Create new player tab and get controls
		wxString name(_("Unknown"));
		if (playerDefs["Name"].defined())
			name = playerDefs["Name"];
		
		PlayerPageControls controls = m_Players->AddPlayer(name, i);
		m_PlayerControls.push_back(controls);

		// Populate civ choice box
		wxChoice* civChoice = controls.civ;
		for (size_t j = 0; j < civNames.Count(); ++j)
			civChoice->Append(civNames[j], new wxStringClientData(civCodes[j]));
		civChoice->SetSelection(0);

		// Populate ai choice box
		wxChoice* aiChoice = controls.ai;
		aiChoice->Append(_("<None>"), new wxStringClientData());
		for (size_t j = 0; j < ais.Count(); ++j)
			aiChoice->Append(ais[j]->GetName(), new wxStringClientData(ais[j]->GetID()));
		aiChoice->SetSelection(0);

		// Only increment AtIters if they are defined
		if (playerDefs.defined())
			++playerDefs;
	}

	m_InGUIUpdate = false;
}
Ejemplo n.º 16
0
void
threadwork(void *_arg)
{
	threadarg *arg = (threadarg *)_arg;
	unsigned int i;

	char fname1[256];
	char fname2[256];

	strcpy(fname1, (char *)arg->mytag);
	strcpy(fname2, (char *)arg->mytag);
	strcat(fname2, "2");
	PR_Delete(fname1);
	PR_Delete(fname2);

	PRfilebuf *fb[MaxCnt];
	PRifstream *ifs[MaxCnt];
	PRofstream *ofs[MaxCnt];
	int mode = 0;
#ifdef XP_UNIX
	mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IRGRP|S_IWOTH|S_IROTH;
#endif

	//
	// Allocate a bunch
	cout << "Testing unused filebufs ----------------" << endl;
	for (i=0; i < MaxCnt; i++){
		fb[i] = new PRfilebuf;
	}
	// Delete them
	for (i=0; i < MaxCnt; i++){
		delete fb[i];
	}
	cout << "Unused filebufs complete ---------------" << endl;

	//
	// Allocate a bunch
	cout << "Testing unused ifstream -----------------" << endl;
	for (i=0; i < MaxCnt; i++){
	  ifs[i] = new PRifstream;
	}
	//
	// Delete them
	for (i=0; i < MaxCnt; i++){
	  delete ifs[i];
	}
	cout << "Unused ifstream complete ----------------" << endl;
	//
	// Allocate a bunch
	cout << "Testing unused ofstream -----------------" << endl;
	for (i=0; i < MaxCnt; i++){
		ofs[i] = new PRofstream;
	}
	for (i=0; i < MaxCnt; i++){
	  *(ofs[i]) << "A"; // Write a bit
	  delete ofs[i]; // Delete it.
	}
	cout << "Unused ofstream complete ----------------" << endl;

	cout << "Testing use of ofstream 1 (extra filebuf allocated) ---------" << endl;
	PRofstream *aos = new PRofstream(fname1, ios::out|ios::ate, mode);
	for (i=0; i < MaxCnt; i++){
	  for (int j=0; j < 8192; j++)
		*aos << "AaBbCcDdEeFfGg" << endl;
		fb[i] = new PRfilebuf; // Allocate as we go to hack at the heap
	}
	//
	// Delete the extra foo we allocated
	for (i=0; i < MaxCnt; i++){
	  delete fb[i];
	}
	aos->flush(); // Explicit flush
	delete aos;
	cout << "Testing use of ofstream 1 complete (extra filebuf deleted) --" << endl;
	cout << "Testing use of ofstream 2 (extra filebuf allocated) ---------" << endl;
	PRofstream *aos2 = new PRofstream(fname2, ios::out, mode);

	for (i=0; i < MaxCnt; i++){
	    *aos2 << "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
	}
	// Force flushing in the dtor
	delete aos2;
	cout << "Testing use of ofstream 2 complete (extra filebuf deleted) --" << endl;
	char line[1024];
	cout << "Testing use of ifstream 1 (stack allocation) -------------" << endl;
	PRifstream ais(fname1);
	for (i=0; i < MaxCnt; i++){
		ais >> line;
	}
	cout << "Testing use of ifstream 1 complete -----------------------" << endl;
	cout << "Testing use of ifstream 2 ----------------------" << endl;
	PRifstream *ais2 = new PRifstream(fname2);
	char achar;
	for (i=0; i < MaxCnt*10; i++){
		*ais2 >> achar;
	}
	delete ais2;
	cout << "Testing use of ifstream 2 complete -------------" << endl;
}