int multireceive(int PORT, std::string group, std::string ip, BlockingQueue<std::string> &q, BlockingQueue<std::string> &foreign){ int pack = makesocket(PORT, group, ip); // call function above to make socket int recvlen; char buf[BUFSIZE] = ""; std::string foreignIP; // string to store IP of sender recvlen = 0; struct sockaddr_in peer_address; // makes structure for peer_address socklen_t peer_address_len; peer_address_len = sizeof(struct sockaddr_storage); while(true){ // while loop to keep receiving //std::cout << "waiting on port " << PORT << std::endl; recvlen = recvfrom(pack, buf, BUFSIZE, 0, (struct sockaddr *)&peer_address, &peer_address_len); //actual receiving mechanism, returns amount of bytes received //std::cout << "received bytes: " << recvlen << std::endl; if (recvlen > 0) { // if bytes available push them to the BlockingQueue foreignIP = inet_ntoa(peer_address.sin_addr); // stores IP of sender foreign.push(std::string(foreignIP, foreignIP.size())); q.push(std::string(buf, recvlen)); } } exit(0); }
int receivePacket(string ip, uint port, string group, BlockingQueue<string> &receiveQueue) { try { int rsock; rsock = get_receive_socket(ip, port, group); // prepare a structure to put peer data into struct sockaddr_in peer_address; socklen_t peer_address_len; peer_address_len = sizeof(struct sockaddr_storage); // allocate memory to put the received data into char data[1500]; int len; len = 0; while (1) { // Receive packet and put its contents in data, recvfrom will block until a packet for this socket has been received len = recvfrom(rsock, data, sizeof(data), 0, (struct sockaddr *) &peer_address, &peer_address_len); if(len > 0){ //cout << "receiver received a packet" << endl; receiveQueue.push(std::string(data, len)); } } } catch(std::exception &e) { std::cout << e.what() << std::endl; exit(0); } return 0; }
void test1() { float block[FRAMES_PER_BUFFER]; Sample sample; for(int j = 0; j< 2500; j++) { //std::unique_lock<std::mutex> mlock(this->bqMutex); std::cout<<"nagrywam"<<j; Pa_ReadStream(inputStream, block, FRAMES_PER_BUFFER); sample.setSample(block); bq.push(sample); // mlock.unlock(); } }
int testBlockingQueue(void) { BlockingQueue<int> testQueue; testQueue.push(1); testQueue.push(2); testQueue.push(3); int val = testQueue.pop(); printf("First value is %d\n", val); val = testQueue.pop(); printf("Second value is %d\n", val); val = testQueue.pop(); printf("Third value is %d\n", val); bool isEmpty = testQueue.empty(); if(isEmpty){ printf("Queue is empty.\n"); } else { printf("Queue is not empty.\n"); } return 0; }
int sendforwardingPacket(string ip, BlockingQueue<string> &sendingQueue){ while(1) { Protocols protocol; //used to get the update table string forwardtableupdate = ip; forwardtableupdate.append(":1:1:1:1:1"); cout << forwardtableupdate << endl; string message = protocol.receiveProtocols(forwardtableupdate); //send a breadcast signal string bla = protocol.sendProtocols(1,ip,"228.0.0.0",message); //push it on the sending queue sendingQueue.push(bla); sleep(10); } return 0; }
// Running test bool TwitchPlaysBlockingQueueTest::RunTest(const FString& Parameters) { BlockingQueue<int>* q = new BlockingQueue<int>(); int32 val = 42; q->push(val); int32 test = q->pop(); //Test val afther a pop TWITCH_CHECK(test == val, "Error on pop()"); //Test IsEmpty TWITCH_CHECK(q->IsEmpty(), "Error on IsEmpty()"); //Test clear q->clear(); TWITCH_CHECK(q->IsEmpty(), "Error on clear()"); return true; }
void multiGameWorker() { cerr << "Thread #" << this_thread::get_id() << " starting up." << endl; while(true) { // Blocking call. Job j = jobQueue.pop(); if(j.shutdown) { cerr << "Thread #" << this_thread::get_id() << " shutting down." << endl; return; } cerr << "Thread #" << this_thread::get_id() << " starting job." << endl; double score = runMultiGame(j.config); double scoreDiff = -(score - j.currentScore); double flip = (float) drand48() / RAND_MAX; double acc = exp(-scoreDiff / j.temp); bool accepted = scoreDiff < 0 || acc > flip; Result res = { j.config, score, accepted}; resultQueue.push(res); cerr << "Thread #" << this_thread::get_id() << " finished job. Score: " << score << endl; } }
ScoreFactors optimize() { // Start up the workers. vector<thread> workers; for(int i = 0; i < WORKER_COUNT; i++) { workers.push_back(thread(multiGameWorker)); } ScoreFactors current = AnnealingBot().sFactors;//startingSFs(); double currentScore = runMultiGame(current); double bestScore = currentScore; ScoreFactors bestFactors = current; double prevTemp = initialTemp; double temp = initialTemp; double sdPrev = initialSD; for(int i = 0; i < tempReductions; i++) { int acceptCount = 0; int count = 1; double mean = currentScore; double delta = currentScore - 0.0; double M2 = 0.0 + delta*(currentScore-mean); vector<Result> accepted; for(int j = 0; j < neighorhoodSize;) { // Feed the workers. for(int y = 0; y < WORKER_COUNT; y++) { // ScoreFactors altered = randomAlter(current); ScoreFactors altered = randomAlter2(current); Job job = {false, altered, temp, currentScore}; jobQueue.push(job); } for(int z = 0; z < WORKER_COUNT && j < neighorhoodSize; z++) { Result res = resultQueue.pop(); if(res.accepted) { acceptCount++; cerr << "Master accepted update." << endl; accepted.push_back(res); } // Start up more jobs until one is accepted. if(accepted.empty()) { // ScoreFactors altered = randomAlter(current); ScoreFactors altered = randomAlter2(current); Job job = {false, altered, temp, currentScore}; jobQueue.push(job); } // Online mean & variance. count++; delta = res.score - mean; mean += delta/count; M2 += delta*(res.score - mean); // Increment trial counter. j++; } // Choose random success, if any. Random produces better results than first in. if(!accepted.empty()) { int randomIdx = (int) (drand48() * accepted.size()); current = accepted[randomIdx].config; currentScore = accepted[randomIdx].score; if(currentScore > bestScore) { bestScore = currentScore; bestFactors = current; } accepted.clear(); } cerr << "Round " << j << " of " << i << endl; } cerr << "M2: " << M2 << " count: " << count << endl; double sd = sqrt(M2) / (count - 1); // Minus 1 from count when calculating accept ratio as the count includes the starting score/config. cerr << "Accept %:" << (double)acceptCount / (double)(count-1) << " at temperature: " << temp << " and SD: " << sd << endl; sd = (1-smoothingFactor) * sd + smoothingFactor * sdPrev * (temp / prevTemp); sdPrev = sd; prevTemp = temp; temp = nextTemperature(temp, sd, lambda); cerr << "Current score: " << currentScore << endl; cerr << "Current score factors: " << endl << printScoreFactors(current) << endl; cerr << "Best score: " << bestScore << endl; cerr << "Best factors: " << endl << printScoreFactors(bestFactors) << endl; } for(int i = 0; i < WORKER_COUNT; i++) { jobQueue.push({true, 0, 0, 0}); } for(int i = 0; i < WORKER_COUNT; i++) { workers[i].join(); } finalScore = currentScore; return current; }