//In this function, calculate whether an event fails or succeeds due to given probabilities. //Functions executed in succession only decide whether an event fails due to constraints //such as population already being at minimum. void CRandomEventCtrl::CalcEvents(CMajor* pRace) { if (!IsActivated()) return; CBotEDoc const* pDoc = resources::pDoc; const unsigned event_type = rand() % (GLOBALEVENTMINOR + 1); if(event_type == GLOBALEVENTSYSTEM)//system affecting event { //Calculate whether such event happens. The more systems we have, the more //likely it is that one of them is affected. CArray<EMPIRE_SYSTEMS> const* systems = pRace->GetEmpire()->GetSystemList(); const unsigned size = systems->GetSize(); const int prob = min(m_uiProbPerSystem * size, 1000); if(rand() % 999 >= prob) return; int whichsystem = rand() % size;//wählt System aus const CPoint ko = systems->GetAt(whichsystem).ko; //sucht koordinaten des Systems //Major home systems are generally unaffected. Unbalances too much. if(pRace->GetCo() == ko) return; for(int i=0; i<100; i++) if(SystemEvent(ko, pRace)) break; } else if (event_type == GLOBALEVENTRESEARCH){ if(rand() % 99 >= static_cast<int>(m_uiGlobalProb)) return; //Es findet kein Ereignis statt GlobalEventResearch(pRace); } else //event_type == GLOBALEVENTMINOR { std::vector<CMinor*> PossibleMinors; const std::map<CString, CMinor*>* pmMinors = pDoc->GetRaceCtrl()->GetMinors(); for (map<CString, CMinor*>::const_iterator it = pmMinors->begin(); it != pmMinors->end(); ++it) { CMinor* pMinor = it->second; if(pRace->IsRaceContacted(pMinor->GetRaceID())&& pMinor->GetRelation(pRace->GetRaceID())<85 && !pMinor->GetSubjugated()) PossibleMinors.insert(PossibleMinors.end(),pMinor); } const unsigned size = PossibleMinors.size(); const int prob = min(m_uiProbPerMinor * size, 1000); if(rand() % 999 >= prob) return; GlobalEventMinor(pRace, PossibleMinors.at(rand() % size)); } }
void WUploadThread::DoUpload() { PRINT("WUploadThread::DoUpload\n"); if (fShutdownFlag && *fShutdownFlag) // Do we need to interrupt? { ConnectTimer(); return; } // Still connected? if (!IsInternalThreadRunning()) { ConnectTimer(); return; } // Small files get to bypass queue if (IsLocallyQueued()) { if ( (fFile && (fFileSize >= gWin->fSettings->GetMinQueuedSize())) || IsManuallyQueued() ) { // not yet fForced = false; WUploadEvent *lq = new WUploadEvent(WUploadEvent::FileQueued); if (lq) { SendReply(lq); } return; } fForced = true; // Set this here to avoid duplicate call to DoUpload() } // Recheck if IP is ignored or not // if (gWin->IsIgnoredIP(fStrRemoteIP) && !IsBlocked()) { SetBlocked(true); } if (IsBlocked()) { WUploadEvent *wue = new WUploadEvent(WUploadEvent::FileBlocked); if (wue) { if (fTimeLeft != -1) wue->SetTime(fTimeLeft); SendReply(wue); } return; } if (fStartTime == 0) fStartTime = GetRunTime64(); if (fFile) { MessageRef uref(GetMessageFromPool(WTransfer::TransferFileData)); if (uref()) { // think about doing this in a dynamic way (depending on connection) double dpps = GetPacketSize() * 1024.0; uint32 bufferSize = lrint(dpps); ByteBufferRef buf = GetByteBufferFromPool(bufferSize); uint8 * scratchBuffer = buf()->GetBuffer(); if (scratchBuffer == NULL) { _nobuffer(); return; } int32 numBytes = 0; numBytes = fFile->ReadBlock32(scratchBuffer, bufferSize); if (numBytes > 0) { buf()->SetNumBytes(numBytes, true); // munge mode switch (fMungeMode) { case WTransfer::MungeModeNone: { uref()->AddInt32("mm", WTransfer::MungeModeNone); break; } case WTransfer::MungeModeXOR: { for (int32 x = 0; x < numBytes; x++) scratchBuffer[x] ^= 0xFF; uref()->AddInt32("mm", WTransfer::MungeModeXOR); break; } default: { break; } } if (uref()->AddFlat("data", buf) == B_OK) { // possibly do checksums here uref()->AddInt32("chk", CalculateFileChecksum(buf)); // a little paranoia, due to file-resumes not working.... (TCP should handle this BUT...) SendMessageToSessions(uref); // NOTE: RequestOutputQueuesDrainedNotification() can recurse, so we need to update the offset before // calling it! fCurrentOffset += numBytes; if (fTunneled) { SignalUpload(); } else { MessageRef drain(GetMessageFromPool()); if (drain()) qmtt->RequestOutputQueuesDrainedNotification(drain); } WUploadEvent *update = new WUploadEvent(WUploadEvent::FileDataSent); if (update) { update->SetOffset(fCurrentOffset); update->SetSize(fFileSize); update->SetSent(numBytes); if (fCurrentOffset >= fFileSize) { update->SetDone(true); // file done! update->SetFile(SimplifyPath(fFileUl)); if (gWin->fSettings->GetUploads()) { SystemEvent( gWin, tr("%1 has finished downloading %2.").arg( GetRemoteUser() ).arg( SimplifyPath(fFileUl) ) ); } } SendReply(update); } return; } else { _nobuffer(); return; } } if (numBytes <= 0) { NextFile(); SignalUpload(); return; } } } else { while (!fFile) { if (fUploads.GetNumItems() != 0) { // grab the ref and remove it from the list fUploads.RemoveHead(fCurrentRef); fFileUl = MakeUploadPath(fCurrentRef); #ifdef _DEBUG // <*****@*****.**> 20021023, 20030702 -- Add additional debug message WString wul(fFileUl); PRINT("WUploadThread::DoUpload: filePath = %S\n", wul.getBuffer()); #endif fFile = new WFile(); Q_CHECK_PTR(fFile); if (!fFile->Open(fFileUl, QIODevice::ReadOnly)) // probably doesn't exist { delete fFile; fFile = NULL; fCurFile++; continue; // onward } // got our file! fFileSize = fFile->Size(); fCurrentOffset = 0; // from the start if (fCurrentRef()->FindInt64("secret:offset", fCurrentOffset) == B_OK) { if (!fFile->Seek(fCurrentOffset)) // <*****@*****.**> 20021026 { fFile->Seek(0); // this can't fail :) (I hope) fCurrentOffset = 0; } } // copy the message in our current file ref MessageRef headRef = fCurrentRef.Clone(); if (headRef()) { headRef()->what = WTransfer::TransferFileHeader; headRef()->AddInt64("beshare:StartOffset", fCurrentOffset); SendMessageToSessions(headRef); } fCurFile++; // Reset statistics InitTransferRate(); InitTransferETA(); WUploadEvent *started = new WUploadEvent(WUploadEvent::FileStarted); if (started) { started->SetFile(SimplifyPath(fFileUl)); started->SetStart(fCurrentOffset); started->SetSize(fFileSize); #ifdef _DEBUG started->SetSession(fRemoteSessionID); #endif SendReply(started); } if (gWin->fSettings->GetUploads()) { SystemEvent( gWin, tr("%1 is downloading %2.").arg( GetRemoteUser() ).arg( SimplifyPath(fFileUl) ) ); } // nested call SignalUpload(); return; } else { PRINT("No more files!\n"); fWaitingForUploadToFinish = true; SetFinished(true); if (fTunneled) { _OutputQueuesDrained(); } else { MessageRef drain(GetMessageFromPool()); if (drain()) qmtt->RequestOutputQueuesDrainedNotification(drain); } break; } } } }
void WListThread::InternalThreadEntry() { Lock(); WinShareWindow * win = dynamic_cast<WinShareWindow *>(fOwner); // reset our shutdown flag if (fShutdownFlag) *fShutdownFlag = false; fFileScanThread->Lock(); int numShares = fFileScanThread->GetNumFiles(); if (win->Settings()->GetInfo()) { QString shares; switch (numShares) { case 0: shares = tr("No shared files."); break; case 1: shares = tr("Sharing 1 file."); break; default: shares = tr("Sharing %1 files.").arg(numShares); } SystemEvent(fOwner, shares); } fNet->SetFileCount(numShares); PRINT("Doing a scan of the returned files for uploading.\n"); int m = 0; MessageRef refScan(GetMessageFromPool(PR_COMMAND_SETDATA)); if (refScan()) { MessageRef mref; HashtableIterator<QString, QString> filesIter = fFileScanThread->GetSharedFilesIterator(HTIT_FLAG_NOREGISTER); while (filesIter.HasData()) { // stop iterating if we are waiting for file scan thread to finish if (fShutdownFlag && *fShutdownFlag) break; QString s = filesIter.GetKey(); filesIter++; MessageRef mref; if (fFileScanThread->FindFile(s, mref)) { MakeNodePath(s); uint32 enc = win->Settings()->GetEncoding(GetServerName(win->CurrentServer()), GetServerPort(win->CurrentServer())); String ms = (const char *) s.utf8(); // Use encoded file attributes? if (enc != 0) { MessageRef packed = DeflateMessage(mref, enc, true); if (packed()) { refScan()->AddMessage(ms, packed); m++; } else { // Failed to pack the message? refScan()->AddMessage(ms, mref); m++; } } else { refScan()->AddMessage(ms, mref); m++; } if (m == 20) { m = 0; fNet->SendMessageToSessions(refScan, 0); refScan = GetMessageFromPool(PR_COMMAND_SETDATA); } } } if (refScan()) { if (!refScan()->IsEmpty()) { fNet->SendMessageToSessions(refScan, 0); refScan.Reset(); } } } fFileScanThread->Unlock(); QCustomEvent *qce = new QCustomEvent(ListDone); if (qce) { QApplication::postEvent(fOwner, qce); } Unlock(); }