huffNode * minFromQueues(int verbose) { huffNode * ptFirstLow = 0; huffNode ** pptCharFreqHeap = GlobalHuffCodes.pptCharFreqHeap; huffNode * peekedFromHeap; huffNode * peekedFromRingBuffer; if( verbose == 1 ) printf("\n(D: peekHeap)"); peekedFromHeap = peekHeap( pptCharFreqHeap ); if( verbose == 1 ) printf("\n(D: peekQueue)"); peekedFromRingBuffer = peekQueue( &tTheSecondQueue ); if( verbose == 1 ) printf("\n(D: pointers: %x %x)", peekedFromHeap, peekedFromRingBuffer ); if( peekedFromHeap != 0 && ( tTheSecondQueue.nSize == 0 || peekedFromRingBuffer->nFreq > peekedFromHeap->nFreq ) ) { if( verbose == 1 ) printf("\n(D:from heap)"); ptFirstLow = popPointerHeap( pptCharFreqHeap , verbose ); } else { if( verbose == 1 ) printf("\n(D:from ringbuffer)"); ptFirstLow = getFromQueue( &tTheSecondQueue ); } return ptFirstLow; }
void UDPServer::start() { sleep(UDPSleep); while(UDPLoopGuard) { VideoFile videofileToSend = getFromQueue(); std::ifstream videoFile; videoFile.open(videofileToSend.getLocalPath()); bool isBegin = true; uint datagramNumber = 1; while(!videoFile.eof()) { int maxDataSize = MAX_DATAGRAM_SIZE - DATAGRAM_CUSTOM_HEADER_SIZE; char bytesFromFile[maxDataSize]; memset(bytesFromFile, 0, maxDataSize); videoFile.read(bytesFromFile, maxDataSize); std::string bytesStr(bytesFromFile, videoFile.gcount()); if (isBegin) { sendDatagram(datagramNumber, videofileToSend.getId(), UdpMessagesTypes::Begin, bytesStr,videofileToSend.getTimestamp()); } else { sendDatagram(datagramNumber, videofileToSend.getId(), UdpMessagesTypes::Middle, bytesStr,videofileToSend.getTimestamp()); } isBegin = false; datagramNumber++; } sendDatagram(datagramNumber, videofileToSend.getId(), UdpMessagesTypes::End, "", videofileToSend.getTimestamp()); videoFile.close(); } }
void CPlayer::setUnused() { pingCounter_.reset(); if (chips_ != 0) { char s[200]; if (username_[0] != '\0') { sprintf(s, "NOTE: Player %s leaves, %d chips left\n", username_, chips_.getDollars()); } else { sprintf(s, "NOTE: Player at slot %d leaves, %d chips left\n", slot_, chips_.getDollars()); } // In tournaments this is ok if (!CTournament::Inst()->isTournament()) Sys_LogError(s); } // Forget about this ip address if (ipaddr_ != 0) UnregisterIPAddress(ipaddr_); connfd_ = -1; chips_ = 0; state_ = PLAYER_STATE_UNUSED; memset(username_, 0, sizeof(username_)); memset(password_, 0, sizeof(password_)); memset(city_, 0, sizeof(city_)); muckCards_ = false; sitIn_ = true; disconnected_ = false; missedBlind_ = 0; sitouts_ = 0; pings_ = 0; sendError_ = false; ipaddr_ = 0; isFreeSeat_ = false; isZombie_ = 0; reanimate_ = false; timeouts_ = 0; // Empty player's pdu queue! while (Cpdu* pdu = getFromQueue()) { delete pdu; } }
bool BFSearch::doSearch(Problem &problem, std::vector<MapNode *> &path) { MapNode *init = problem.getInitState(); if(problem.isGoal(init)) { init->getPathFromRoot(path); return true; } insertQueue(init); while(!_queue->isEmpty()) { MapNode * node = getFromQueue(); insertExplored(node); std::vector<MovAction_t> & actions = problem.getActions(); std::vector<MovAction_t>::iterator it; for(it = actions.begin(); it < actions.end(); it++) { if(*it == MOV_ACT_NOOP) { continue; } MapNode * child = NULL; int res = 0; res = problem.movAction(node, *it, &child); if(res == OP_OK) { //check if in the queue if(!isInQueue(child) && !isInExplored(child)) { if(problem.isGoal(child)) { child->getPathFromRoot(path); return true; } insertQueue(child); } } } //for } //while return false; }
static Byte *q_get(QTYPE *queue, Byte *data){ // Nothing in the queue if (queue->count == 0) return (NULL); else { do{ getFromQueue(&*queue, &*data); }while(queue->count > 0 && !isValidChar(*data)); if(queue->count < MAX_LOWERLIMIT){ controlChar = XON; bufferSend[0] = controlChar; printf("Buffer < maksimum lowerlimit. Send XON\n"); ssize_t lengthSentBytes = sendto(sockfd, bufferSend, sizeof(bufferSend), 4, (struct sockaddr *) &remAddr, remAddrLen); if(lengthSentBytes < 0){ perror("sendto() failed\n"); } } } return data; }
/* * Method to process staff menu * * Daniel Lennart */ void handleProcessMenu (int opt) { switch (opt) { case 1: { int i; for (i=0;i<queue_index;i++) { printOrder(productQueue[i]); } break; } case 2: { Order order = getFromQueue(); printOrder(order); break; } case 3: { printf("There are %d orders.\n", queue_index); break; } case 4: { break; } default: { printf("Incorrect Option"); handleProcessMenu(opt); break; } } }
// // MFUNCTION: CPlayer::waitPdu // // PURPOSE: Wait for an incoming pdu for the // specified timeout. // If the incoming pdu has a sequence number different // from what is expected, the pdu is ignored. // // RETURN: // If a pdu arrives in time, return 0, otherwise // return Pdu_Wait_Timeout. // int CPlayer::waitPdu(Cpdu** ppPdu, DWORD timeOut, u_int16_t sequenceNumber) { int rc = 0; if (disconnected_) return CPlayer::Pdu_Wait_Timeout; long start = 0, elapsed = 0; time(&start); // note: timeOut is in milliseconds while // elapsed is in seconds while (timeOut > (1000 * elapsed)) { if (pduEvent_.lock(timeOut - (1000 * elapsed))) { // When we get here we have an incoming PDU pduEvent_.resetEvent(); // Get all pdus from queue, consider the one // with correct sequence number while (Cpdu* pdu = getFromQueue()) { // if sequence numbers don't match, ignore // the pdu and wait for next one to arrive if (checkSequenceNumber(pdu, sequenceNumber)) { // XXX Tournament: pdu received, reset timeouts setTimeouts(0); *ppPdu = pdu; return 0; // OK! } else { // pdu had wrong sequence number - it is // and old pdu and is ignored delete pdu; // The loop will check for more pdus and // will resume waiting if necessary } } long now = 0; time(&now); elapsed += (now - start); } else { // Wait timed out, will exit loop elapsed = (1000 * timeOut); } } // this is ok, player did not respond within timeout printf("CPlayer::waitPdu: timed out (%s)\n", getUsername()); rc = CPlayer::Pdu_Wait_Timeout; // XXX Tournament if (CTournament::Inst()->isTournament()) { CpduPing p; if (p.sendPing(this) == -1) { char buf[200]; sprintf(buf, "Player %s sendPing==-1\n", getUsername()); Sys_LogError(buf); setSendError(true); } setTimeouts(getTimeouts() + 1); if (getTimeouts() >= MAX_TIMEOUTS) { // Player is not responding - set network error flag char buf[200]; sprintf(buf, "Player %s (%d) times out\n", getUsername(), getConnfd()); Sys_LogError(buf); setSendError(true); } } return rc; }
huffNode * makeHuffman( int verbose ) { huffNode * ptCharFreq; huffNode * ptFirstLow = 0; huffNode * ptSecondLow = 0; huffNode * ptNewNode =0; huffNode * ptHuffRoot = 0; //huffNode * ptNewNode; huffNode ** pptCharFreqHeap; ptCharFreq = GlobalHuffCodes.ptCharFreq; pptCharFreqHeap = createPointerHeap( ptCharFreq, GlobalHuffCodes.pptCharFreqHeap, verbose ); initializeRignBuffer( &tTheSecondQueue ); GlobalHuffCodes.nNextNotleaf = 0; //3. While there is more than one node in the queues: while( !( peekHeap( pptCharFreqHeap ) == 0 && tTheSecondQueue.nSize == 1) ) { //1. Remove two nodes with the lowest weight from the queues. if( verbose == 1 ) printf("\n(D:1)"); ptFirstLow = minFromQueues( verbose); if( verbose == 1 ) printf("\n(D:1 %c %d %d)",ptFirstLow->cCharacter,ptFirstLow->cCharacter,ptFirstLow->nFreq ); if( verbose == 1 ) printf("\n(D:2)"); ptSecondLow = minFromQueues( verbose); if( verbose == 1 ) printf("\n(D: initializing)"); if( ptSecondLow != 0) { if( verbose == 1 ) printf("\n(D:2 %c %d %d)\n",ptSecondLow->cCharacter,ptSecondLow->cCharacter,ptSecondLow->nFreq ); //2. Create a new internal node, with the two just-removed nodes as children //(either node can be either child) and the sum of their weights as the new weight. ptNewNode = getNewNode( verbose ); if( ptNewNode == 0) return 0; ptNewNode->cCharacter = 0; ptNewNode->nFreq = ptFirstLow->nFreq + ptSecondLow->nFreq; ptNewNode->ptOneChild = ptFirstLow; ptNewNode->ptZeroChild = ptSecondLow; //zeroes are less expensive :) ptNewNode->ptParent = 0; if( verbose == 1 ) printf("\n(D:new node initialized.)"); //3. Update the parent links in the two just-removed nodes to point to the just-created parent node. ptFirstLow->ptParent = ptNewNode; ptSecondLow->ptParent = ptNewNode; } else { ptNewNode = getNewNode( verbose ); if( ptNewNode == 0) return 0; ptNewNode->ptZeroChild = ptFirstLow; ptNewNode->ptOneChild = 0; ptFirstLow->ptParent = ptNewNode; ptNewNode->nFreq = ptFirstLow->nFreq; ptNewNode->cCharacter = 0; } //4. Queue the new node into the second queue. if( putToRignBuffer(ptNewNode, &tTheSecondQueue ) == 1 ) { fprintf(stderr,"\nError: ringbuffer full\n"); if( verbose == 1 ) { printf("\nD: size:%d max:%d )", tTheSecondQueue.nSize, RINGBUFFERSIZE ); } return 0; } if( verbose == 1 ) printf("\n(D: iteration end)"); } ptHuffRoot = getFromQueue( &tTheSecondQueue ); ptHuffRoot->ptParent = 0; GlobalHuffCodes.ptHuffRoot = ptHuffRoot; if( verbose == 1 ) printHeap( GlobalHuffCodes.pptCharFreqHeap ) ; return ptHuffRoot; }