void *pingUnknownAgents(void *args) { AgentList* agentList = (AgentList*) args; const int PING_INTERVAL_USECS = 1 * 1000000; timeval lastSend; while (!pingUnknownAgentThreadStopFlag) { gettimeofday(&lastSend, NULL); for(AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { if (!agent->getActiveSocket() && agent->getPublicSocket() && agent->getLocalSocket()) { // ping both of the sockets for the agent so we can figure out // which socket we can use agentList->getAgentSocket()->send(agent->getPublicSocket(), &PACKET_HEADER_PING, 1); agentList->getAgentSocket()->send(agent->getLocalSocket(), &PACKET_HEADER_PING, 1); } } long long usecToSleep = PING_INTERVAL_USECS - (usecTimestampNow() - usecTimestamp(&lastSend)); if (usecToSleep > 0) { usleep(usecToSleep); } } return NULL; }
void *removeSilentAgents(void *args) { AgentList* agentList = (AgentList*) args; long long checkTimeUSecs, sleepTime; while (!silentAgentThreadStopFlag) { checkTimeUSecs = usecTimestampNow(); for(AgentList::iterator agent = agentList->begin(); agent != agentList->end(); ++agent) { if ((checkTimeUSecs - agent->getLastHeardMicrostamp()) > AGENT_SILENCE_THRESHOLD_USECS && agent->getType() != AGENT_TYPE_VOXEL_SERVER) { printLog("Killed "); Agent::printLog(*agent); agent->setAlive(false); } } sleepTime = AGENT_SILENCE_THRESHOLD_USECS - (usecTimestampNow() - checkTimeUSecs); #ifdef _WIN32 Sleep( static_cast<int>(1000.0f*sleepTime) ); #else usleep(sleepTime); #endif } pthread_exit(0); return NULL; }
void WorldWindow::ShowProperties() { AgentListButton * btn = (AgentListButton *)sender(); AgentList * al = btn->get_agent_list(); std::string agent_name = al->currentText().toStdString(); Agent * curAgent = get_agent_by_name(agent_name.c_str()); InteractionOptions(curAgent); }
AgentList* MicroscopicDataDB::getAgent(unsigned int tick, QPoint& bottomLeft, QPoint& topRight) { // remove old data AgentList *agentList = new AgentList(); agentList->setDeepDelete(); QList<Agent*>& list = agentList->getList(); // load driver if (isAgentExisted_) { QString whereQuery = "frame = " + QString::number(tick) + " AND " "xpos BETWEEN " + QString::number(bottomLeft.x()) + " AND " + QString::number(topRight.x()) + " AND " "ypos BETWEEN " + QString::number(bottomLeft.y()) + " AND " + QString::number(topRight.y()); QString driverQuery = "SELECT * FROM \"" + fileId_ + DBManager::agentTableName + "\" WHERE " + whereQuery; reader_.setForwardOnly(true); reader_.exec(driverQuery); unsigned long id; double xpos, ypos, angle; int type; QStringList attributes; while (reader_.next()) { id = reader_.value(0).toULongLong(); xpos = reader_.value(2).toDouble(); ypos = -(reader_.value(3).toDouble()); angle = reader_.value(4).toDouble(); type = reader_.value(5).toInt(); attributes = (reader_.value(6).toString()).split(":"); Agent *agent = 0; if (type == 0) { // driver double length = attributes.at(0).toDouble(); double width = attributes.at(1).toDouble(); int mandatory = attributes.at(2).toInt(); unsigned long currentSegment = attributes.at(3).toULongLong(); int fwdSpeed = attributes.at(4).toInt(); int fwdAccel = attributes.at(5).toInt(); agent = new Driver(id, tick, QPointF(xpos, ypos), angle, length, width, currentSegment, fwdSpeed, fwdAccel, mandatory, attributes.at(6)); } else if (type == 1) { // bus double length = attributes.at(0).toDouble(); double width = attributes.at(1).toDouble(); int passenger = attributes.at(2).toInt(); unsigned long realArrivalTime = attributes.at(3).toULongLong(); int dwellTime = attributes.at(4).toInt(); agent = new BusDriver(id, tick, QPointF(xpos, ypos), angle, length, width, passenger, realArrivalTime, dwellTime, attributes.at(5)); } else { agent = new Pedestrian(id, tick, QPointF(xpos, ypos)); } list.append(agent); } reader_.clear(); } return agentList; }
StringList WorldFacadeSmartPointerImpl::getAgentsInLocation(string lName) { AgentList agentsInside = locations[lName]->agentsInside(); list<string> l; for (AgentList::iterator iter = agentsInside.begin(); iter != agentsInside.end(); iter++) { l.push_back(iter->second->getName()); } return l; }
StringList WorldFacadeSmartPointerImpl::getAgentsInWorld() { StringList l; cout << "locsInWorld size: " << locations.size() << endl; for (LocationSmartPointerMap::const_iterator it = locations.begin(); it != locations.end(); it++) { LocationSmartPointerPtr location = it->second; AgentList agentsInside = location->agentsInside(); for (AgentList::const_iterator it2 = agentsInside.begin(); it2 != agentsInside.end(); it2++) { AgentPointer agent = it2->second; l.push_back(agent->getName()); } } cout << "locsInWorld size: " << locations.size() << endl; return l; }
AgentPointer WorldFacadeSmartPointerImpl::findAgentByName(string name) { for (LocationSmartPointerMap::const_iterator it = locations.begin(); it != locations.end(); it++) { LocationSmartPointerPtr location = it->second; //cout << "FOUND LOCATION: " << *location << endl; AgentList agentsInside = location->agentsInside(); for (AgentList::const_iterator it2 = agentsInside.begin(); it2 != agentsInside.end(); it2++) { AgentPointer agent = it2->second; //cout << "FOUND AGENT: " << *agent << endl; if (name == agent->getName()) { //cout << "AGENT MATCH NAME: " << name << endl; return agent; } } } return NULL; }
std::vector<double> beatTrack(const AgentParameters ¶ms, const EventList &events, const EventList &beats) { AgentList agents; int count = 0; double beatTime = -1; if (!beats.empty()) { count = beats.size() - 1; EventList::const_iterator itr = beats.end(); --itr; beatTime = itr->time; } if (count > 0) { // tempo given by mean of initial beats double ioi = (beatTime - beats.begin()->time) / count; agents.push_back(new Agent(params, ioi)); } else // tempo not given; use tempo induction agents = Induction::beatInduction(params, events); if (!beats.empty()) { for (AgentList::iterator itr = agents.begin(); itr != agents.end(); ++itr) { (*itr)->beatTime = beatTime; (*itr)->beatCount = count; (*itr)->events = beats; } } agents.beatTrack(events, params, -1); Agent *best = agents.bestAgent(); std::vector<double> resultBeatTimes; if (best) { best->fillBeats(beatTime); for (EventList::const_iterator itr = best->events.begin(); itr != best->events.end(); ++itr) { resultBeatTimes.push_back(itr->time); } } for (AgentList::iterator ai = agents.begin(); ai != agents.end(); ++ai) { delete *ai; } return resultBeatTimes; }
AgentList Induction::beatInduction(const AgentParameters ¶ms, const EventList &events) { int i, j, b, bestCount; bool submult; int intervals = 0; // number of interval clusters std::vector<int> bestn; // count of high-scoring clusters bestn.resize(topN); double ratio, err; int degree; int maxClusterCount = (int) ceil((maxIOI - minIOI) / clusterWidth); std::vector<double> clusterMean; clusterMean.resize(maxClusterCount); std::vector<int> clusterSize; clusterSize.resize(maxClusterCount); std::vector<int> clusterScore; clusterScore.resize(maxClusterCount); EventList::const_iterator ptr1, ptr2; Event e1, e2; ptr1 = events.begin(); while (ptr1 != events.end()) { e1 = *ptr1; ++ptr1; ptr2 = events.begin(); e2 = *ptr2; ++ptr2; while (e2 != e1 && ptr2 != events.end()) { e2 = *ptr2; ++ptr2; } while (ptr2 != events.end()) { e2 = *ptr2; ++ptr2; double ioi = e2.time - e1.time; if (ioi < minIOI) // skip short intervals continue; if (ioi > maxIOI) // ioi too long break; for (b = 0; b < intervals; b++) // assign to nearest cluster if (std::fabs(clusterMean[b] - ioi) < clusterWidth) { if ((b < intervals - 1) && (std::fabs(clusterMean[b + 1] - ioi) < std::fabs(clusterMean[b] - ioi))) { b++; // next cluster is closer } clusterMean[b] = (clusterMean[b] * clusterSize[b] + ioi) / (clusterSize[b] + 1); clusterSize[b]++; break; } if (b == intervals) { // no suitable cluster; create new one if (intervals == maxClusterCount) { // System.err.println("Warning: Too many clusters"); continue; // ignore this IOI } intervals++; for ( ; (b > 0) && (clusterMean[b - 1] > ioi); b--) { clusterMean[b] = clusterMean[b - 1]; clusterSize[b] = clusterSize[b - 1]; } clusterMean[b] = ioi; clusterSize[b] = 1; } } } for (b = 0; b < intervals; b++) // merge similar intervals // TODO: they are now in order, so don't need the 2nd loop // TODO: check BOTH sides before averaging or upper gps don't work for (i = b + 1; i < intervals; i++) if (std::fabs(clusterMean[b] - clusterMean[i]) < clusterWidth) { clusterMean[b] = (clusterMean[b] * clusterSize[b] + clusterMean[i] * clusterSize[i]) / (clusterSize[b] + clusterSize[i]); clusterSize[b] = clusterSize[b] + clusterSize[i]; --intervals; for (j = i + 1; j <= intervals; j++) { clusterMean[j - 1] = clusterMean[j]; clusterSize[j - 1] = clusterSize[j]; } } if (intervals == 0) return AgentList(); for (b = 0; b < intervals; b++) clusterScore[b] = 10 * clusterSize[b]; bestn[0] = 0; bestCount = 1; for (b = 0; b < intervals; b++) { for (i = 0; i <= bestCount; i++) { if (i < topN && (i == bestCount || clusterScore[b] > clusterScore[bestn[i]])) { if (bestCount < topN) bestCount++; for (j = bestCount - 1; j > i; j--) bestn[j] = bestn[j - 1]; bestn[i] = b; break; } } } for (b = 0; b < intervals; b++) { // score intervals for (i = b + 1; i < intervals; i++) { ratio = clusterMean[b] / clusterMean[i]; submult = ratio < 1; if (submult) degree = (int) nearbyint(1 / ratio); else degree = (int) nearbyint(ratio); if ((degree >= 2) && (degree <= 8)) { if (submult) err = std::fabs(clusterMean[b] * degree - clusterMean[i]); else err = std::fabs(clusterMean[b] - clusterMean[i] * degree); if (err < (submult ? clusterWidth : clusterWidth * degree)) { if (degree >= 5) degree = 1; else degree = 6 - degree; clusterScore[b] += degree * clusterSize[i]; clusterScore[i] += degree * clusterSize[b]; } } } } AgentList a; for (int index = 0; index < bestCount; index++) { b = bestn[index]; // Adjust it, using the size of super- and sub-intervals double newSum = clusterMean[b] * clusterScore[b]; int newCount = clusterSize[b]; int newWeight = clusterScore[b]; for (i = 0; i < intervals; i++) { if (i == b) continue; ratio = clusterMean[b] / clusterMean[i]; if (ratio < 1) { degree = (int) nearbyint(1 / ratio); if ((degree >= 2) && (degree <= 8)) { err = std::fabs(clusterMean[b] * degree - clusterMean[i]); if (err < clusterWidth) { newSum += clusterMean[i] / degree * clusterScore[i]; newCount += clusterSize[i]; newWeight += clusterScore[i]; } } } else { degree = (int) nearbyint(ratio); if ((degree >= 2) && (degree <= 8)) { err = std::fabs(clusterMean[b] - degree * clusterMean[i]); if (err < clusterWidth * degree) { newSum += clusterMean[i] * degree * clusterScore[i]; newCount += clusterSize[i]; newWeight += clusterScore[i]; } } } } double beat = newSum / newWeight; // Scale within range ... hope the grouping isn't ternary :( while (beat < minIBI) // Maximum speed beat *= 2.0; while (beat > maxIBI) // Minimum speed beat /= 2.0; if (beat >= minIBI) { a.push_back(new Agent(params, beat)); } } return a; }
void Configuration::readApplicationSettings(xml_node<> *app) { xml_node<> *sess = singleChildElement(app, "session-management"); if (sess) { xml_node<> *dedicated = singleChildElement(sess, "dedicated-process"); xml_node<> *shared = singleChildElement(sess, "shared-process"); std::string tracking = singleChildElementValue(sess, "tracking", ""); if (dedicated && shared) throw WServer::Exception("<application-settings> requires either " "<dedicated-process> or <shared-process>, " "not both"); if (dedicated) { sessionPolicy_ = DedicatedProcess; setInt(dedicated, "max-num-sessions", maxNumSessions_); } if (shared) { sessionPolicy_ = SharedProcess; setInt(shared, "num-processes", numProcesses_); } if (!tracking.empty()) { if (tracking == "Auto") sessionTracking_ = CookiesURL; else if (tracking == "URL") sessionTracking_ = URL; else throw WServer::Exception("<session-tracking>: expecting 'Auto' " "or 'URL'"); } setInt(sess, "timeout", sessionTimeout_); setInt(sess, "bootstrap-timeout", bootstrapTimeout_); setInt(sess, "server-push-timeout", serverPushTimeout_); setBoolean(sess, "reload-is-new-session", reloadIsNewSession_); } std::string maxRequestStr = singleChildElementValue(app, "max-request-size", ""); if (!maxRequestStr.empty()) maxRequestSize_ = boost::lexical_cast< ::int64_t >(maxRequestStr) * 1024; std::string debugStr = singleChildElementValue(app, "debug", ""); if (!debugStr.empty()) { if (debugStr == "stack") errorReporting_ = ErrorMessageWithStack; else if (debugStr == "true") errorReporting_ = NoErrors; else if (debugStr == "false") errorReporting_ = ErrorMessage; else throw WServer::Exception("<debug>: expecting 'true', 'false'," "or 'stack'"); } setInt(app, "num-threads", numThreads_); xml_node<> *fcgi = singleChildElement(app, "connector-fcgi"); if (!fcgi) fcgi = app; // backward compatibility valgrindPath_ = singleChildElementValue(fcgi, "valgrind-path", valgrindPath_); runDirectory_ = singleChildElementValue(fcgi, "run-directory", runDirectory_); setInt(fcgi, "num-threads", numThreads_); // backward compatibility < 3.2.0 xml_node<> *isapi = singleChildElement(app, "connector-isapi"); if (!isapi) isapi = app; // backward compatibility setInt(isapi, "num-threads", numThreads_); // backward compatibility < 3.2.0 std::string maxMemoryRequestSizeStr = singleChildElementValue(isapi, "max-memory-request-size", ""); if (!maxMemoryRequestSizeStr.empty()) { isapiMaxMemoryRequestSize_ = boost::lexical_cast< ::int64_t > (maxMemoryRequestSizeStr) * 1024; } setInt(app, "session-id-length", sessionIdLength_); /* * If a session-id-prefix is defined in the configuration file, then * we loose the prefix defined by the connector (e.g. wthttpd), but who * would do such a thing ? */ connectorSessionIdPrefix_ = singleChildElementValue(app,"session-id-prefix", connectorSessionIdPrefix_); setBoolean(app, "send-xhtml-mime-type", xhtmlMimeType_); if (xhtmlMimeType_) LOG_WARN("ignoring send-xhtml-mime-type setting: HTML5 is now always used"); redirectMsg_ = singleChildElementValue(app, "redirect-message", redirectMsg_); setBoolean(app, "behind-reverse-proxy", behindReverseProxy_); setBoolean(app, "strict-event-serialization", serializedEvents_); setBoolean(app, "web-sockets", webSockets_); setBoolean(app, "inline-css", inlineCss_); setBoolean(app, "persistent-sessions", persistentSessions_); uaCompatible_ = singleChildElementValue(app, "UA-Compatible", ""); setBoolean(app, "progressive-bootstrap", progressiveBoot_); if (progressiveBoot_) setBoolean(app, "split-script", splitScript_); setBoolean(app, "session-id-cookie", sessionIdCookie_); setBoolean(app, "cookie-checks", cookieChecks_); std::string plainAjaxSessionsRatioLimit = singleChildElementValue(app, "plain-ajax-sessions-ratio-limit", ""); if (!plainAjaxSessionsRatioLimit.empty()) maxPlainSessionsRatio_ = boost::lexical_cast<float>(plainAjaxSessionsRatioLimit); setBoolean(app, "ajax-puzzle", ajaxPuzzle_); setInt(app, "indicator-timeout", indicatorTimeout_); setInt(app, "double-click-timeout", doubleClickTimeout_); std::vector<xml_node<> *> userAgents = childElements(app, "user-agents"); for (unsigned i = 0; i < userAgents.size(); ++i) { xml_node<> *userAgentsList = userAgents[i]; std::string type; if (!attributeValue(userAgentsList, "type", type)) throw WServer::Exception("<user-agents> requires attribute 'type'"); std::string mode; attributeValue(userAgentsList, "mode", mode); AgentList *list; if (type == "ajax") { list = &ajaxAgentList_; if (mode == "black-list") ajaxAgentWhiteList_ = false; else if (mode == "white-list") ajaxAgentWhiteList_ = true; else throw WServer::Exception ("<user-agents type=\"ajax\" requires attribute 'mode' with value " "\"white-list\" or \"black-list\""); } else if (type == "bot") list = &botList_; else throw WServer::Exception ("<user-agents> requires attribute 'type' with value " "\"ajax\" or \"bot\""); std::vector<xml_node<> *> agents = childElements(userAgentsList, "user-agent"); for (unsigned j = 0; j < agents.size(); ++j) list->push_back(elementValue(agents[j], "user-agent")); } xml_node<> *properties = singleChildElement(app, "properties"); if (properties) { std::vector<xml_node<> *> nodes = childElements(properties, "property"); for (unsigned i = 0; i < nodes.size(); ++i) { xml_node<> *property = nodes[i]; std::string name; if (!attributeValue(property, "name", name)) throw WServer::Exception("<property> requires attribute 'name'"); std::string value = elementValue(property, "property"); if (name == "approot") name = "appRoot"; if (name == "appRoot" && !appRoot_.empty()) LOG_WARN("ignoring configuration property 'appRoot' (" << value << ") because was already set to " << appRoot_); else properties_[name] = value; } } }
int main(int argc, const char * argv[]) { // If user asks to run in "local" mode then we do NOT replace the IP // with the EC2 IP. Otherwise, we will replace the IP like we used to // this allows developers to run a local domain without recompiling the // domain server bool useLocal = cmdOptionExists(argc, argv, "--local"); if (useLocal) { printf("NOTE: Running in Local Mode!\n"); } else { printf("--------------------------------------------------\n"); printf("NOTE: Running in EC2 Mode. \n"); printf("If you're a developer testing a local system, you\n"); printf("probably want to include --local on command line.\n"); printf("--------------------------------------------------\n"); } setvbuf(stdout, NULL, _IOLBF, 0); ssize_t receivedBytes = 0; char agentType; unsigned char *broadcastPacket = new unsigned char[MAX_PACKET_SIZE]; *broadcastPacket = 'D'; unsigned char *currentBufferPos; unsigned char *startPointer; int packetBytesWithoutLeadingChar; sockaddr_in agentPublicAddress, agentLocalAddress; agentLocalAddress.sin_family = AF_INET; in_addr_t serverLocalAddress = getLocalAddress(); agentList.startSilentAgentRemovalThread(); while (true) { if (agentList.getAgentSocket().receive((sockaddr *)&agentPublicAddress, packetData, &receivedBytes)) { std::map<char, Agent *> newestSoloAgents; agentType = packetData[0]; unpackSocket(&packetData[1], (sockaddr *)&agentLocalAddress); // check the agent public address // if it matches our local address we're on the same box // so hardcode the EC2 public address for now if (agentPublicAddress.sin_addr.s_addr == serverLocalAddress) { // If we're not running "local" then we do replace the IP // with the EC2 IP. Otherwise, we use our normal public IP if (!useLocal) { agentPublicAddress.sin_addr.s_addr = 895283510; // local IP in this format... } } if (agentList.addOrUpdateAgent((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType, agentList.getLastAgentId())) { agentList.increaseAgentId(); } currentBufferPos = broadcastPacket + 1; startPointer = currentBufferPos; for(std::vector<Agent>::iterator agent = agentList.getAgents().begin(); agent != agentList.getAgents().end(); agent++) { if (DEBUG_TO_SELF || !agent->matches((sockaddr *)&agentPublicAddress, (sockaddr *)&agentLocalAddress, agentType)) { if (strchr(SOLO_AGENT_TYPES_STRING, (int) agent->getType()) == NULL) { // this is an agent of which there can be multiple, just add them to the packet currentBufferPos = addAgentToBroadcastPacket(currentBufferPos, &(*agent)); } else { // solo agent, we need to only send newest if (newestSoloAgents[agent->getType()] == NULL || newestSoloAgents[agent->getType()]->getFirstRecvTimeUsecs() < agent->getFirstRecvTimeUsecs()) { // we have to set the newer solo agent to add it to the broadcast later newestSoloAgents[agent->getType()] = &(*agent); } } } else { // this is the agent, just update last receive to now agent->setLastRecvTimeUsecs(usecTimestampNow()); } } for (std::map<char, Agent *>::iterator agentIterator = newestSoloAgents.begin(); agentIterator != newestSoloAgents.end(); agentIterator++) { // this is the newest alive solo agent, add them to the packet currentBufferPos = addAgentToBroadcastPacket(currentBufferPos, agentIterator->second); } if ((packetBytesWithoutLeadingChar = (currentBufferPos - startPointer))) { agentList.getAgentSocket().send((sockaddr *)&agentPublicAddress, broadcastPacket, packetBytesWithoutLeadingChar + 1); } } } return 0; }