static int event_handler(struct mg_event *event) { if (event->type == MG_REQUEST_BEGIN) { routes(event); return 1; } // We do not handle any other event return 0; }
unsigned long long int routes(unsigned int i, unsigned int j) { if(store[i][j] == -1) { if ((i == N - 1) && (j == N - 1)) store[i][j] = 1; else if(i + 1 < N) if(j + 1 < N) store[i][j] = routes(i + 1, j) + routes(i, j + 1); else store[i][j] = routes(i + 1, j); else if(j + 1 < N) store[i][j] = routes(i, j + 1); else store[i][j] = 0; } return store[i][j]; }
std::string RouteContainer::getRoutes(bool styled) const { Json::Value routes(Json::arrayValue); for (auto const &r : m_routingDirectives) { routes.append(parseRoutingDirective(r)); } if (styled) { return routes.toStyledString(); } else { return toFastString(routes); } }
int main() { unsigned int i, j; for(i = 0; i < N; i++) for(j = 0; j < N; j++) store[i][j] = -1; store[0][0] = routes(0, 0); printf("\nNo of routes available are : %llu", store[0][0]); return 0; }
/* * main loop - construct the event_base and start the loop */ int run_server(char* ip, short port, int timeout_s, int backlog) { struct event_base *ev_base; struct event_config *ev_cfg; struct evhttp *ev_httpd; evutil_socket_t socket_fd; int socket_opts_flag = 1; int worker_id, socket_opts_results; openlog("Backend", LOG_PID|LOG_NDELAY, LOG_LOCAL0); ev_cfg = event_config_new(); //event_config_set_flag(ev_cfg, EV_TIMEOUT|EV_PERSIST); ev_base = event_base_new_with_config(ev_cfg); if (!ev_base) { printf("ERROR: Failed to initialize HTTP event loop!\n"); return -1; } // Set up httpd interface event ev_httpd = evhttp_new(ev_base); evhttp_set_timeout(ev_httpd, timeout_s); routes(ev_httpd, ev_base); socket_fd = create_socket(ip, port, timeout_s, backlog); socket_opts_results = setsockopt(socket_fd, IPPROTO_TCP, TCP_NODELAY, (char *) socket_opts_flag, sizeof(int)); if ( socket_opts_results < 0 ) { syslog( LOG_INFO, "Nagle DISABLED"); } else { syslog( LOG_INFO, "Nagle ENABLED"); } evhttp_accept_socket(ev_httpd, socket_fd); for (worker_id = 0; worker_id < WORKERS; worker_id++) { if (fork() == 0) { printf("Starting worker_id %d ... ", worker_id); worker(ev_base); printf("done.\n"); exit(0); } } closelog(); return 0; }
void device_sound_interface::interface_validity_check(validity_checker &valid) const { // loop over all the routes for (sound_route const &route : routes()) { // find a device with the requested tag device_t const *const target = route.m_base.get().subdevice(route.m_target.c_str()); if (!target) osd_printf_error("Attempting to route sound to non-existent device '%s'\n", route.m_base.get().subtag(route.m_target.c_str()).c_str()); // if it's not a speaker or a sound device, error device_sound_interface const *sound; if (target && (target->type() != SPEAKER) && !target->interface(sound)) osd_printf_error("Attempting to route sound to a non-sound device '%s' (%s)\n", target->tag(), target->name()); } }
unsigned long int routes(int x, int y) { return x==MAX && y==MAX ? 1 : /* one route */ (x<MAX ? routes(x+1, y) : 0) + (y<MAX ? routes(x, y+1) : 0); }
int main() { for ( MAX=1; MAX<=20; ++MAX ) printf("%dx%d grid has %lu routes\n", MAX, MAX, routes(0, 0)); }
std::vector<std::string> Router::find_routes() const { POMAGMA_INFO("Routing all obs"); const size_t item_count = m_carrier.item_count(); std::vector<float> best_probs(1 + item_count, 0); std::vector<Segment> best_segments(1 + item_count); bool changed = true; while (changed) { changed = false; POMAGMA_DEBUG("finding best local routes"); //#pragma omp parallel for schedule(dynamic, 1) for (size_t i = 0; i < item_count; ++i) { Ob ob = 1 + i; float& best_prob = best_probs[ob]; Segment& best_segment = best_segments[ob]; bool best_changed = false; for (const Segment& segment : iter_val(ob)) { float prob = get_prob(segment, best_probs); if (unlikely(std::make_pair(-prob, segment) < std::make_pair(-best_prob, best_segment))) { best_prob = prob; // relaxed memory order best_segment = segment; // relaxed memory order best_changed = true; } } if (best_changed) { //#pragma omp atomic changed = true; } } } POMAGMA_DEBUG("scheduling route building"); std::vector<Ob> schedule; schedule.reserve(item_count); for (auto iter = m_carrier.iter(); iter.ok(); iter.next()) { Ob ob = *iter; POMAGMA_ASSERT_LT(0, best_probs[ob]); schedule.push_back(ob); } std::sort(schedule.begin(), schedule.end(), [&](const Ob& x, const Ob& y) { return best_probs[x] > best_probs[y]; }); POMAGMA_DEBUG("building full routes"); std::vector<std::string> routes(1 + item_count); for (Ob ob : schedule) { const Segment& segment = best_segments[ob]; const SegmentType& type = m_types[segment.type]; switch (type.arity) { case NULLARY: { routes[ob] = type.name; } break; case UNARY: { const auto& arg = routes[segment.arg1]; POMAGMA_ASSERT(not arg.empty(), "unknown arg route"); routes[ob] = type.name + " " + arg; } break; case BINARY: { const auto& lhs = routes[segment.arg1]; const auto& rhs = routes[segment.arg2]; POMAGMA_ASSERT(not lhs.empty(), "unknown lhs route"); POMAGMA_ASSERT(not rhs.empty(), "unknown rhs route"); routes[ob] = type.name + " " + lhs + " " + rhs; } break; } } return routes; }
/*@maynotreturn@*/ void routeParser(void) { struct sRoute * pRoute = NULL; unsigned short nr = 0; unsigned short i = 0; unsigned short j = 0; enum eToken tok = tokNone; char name[NAMELEN]; enum eRoutePartType ePartType; parserExpectTokenNeu(tokLBracket); parserExpectTokenNeu(tokZuFas); nr = parserExpectNumber("Anzahl Zugfahrstrassen erwartet"); routes(nr); /* construct route structure */ parserExpectTokenNeu(tokLBracket); while(NULL != (pRoute = routeGet(i))) { tok = parserGetWord(name); parserAssert(tokNone == tok, "Name der Zugfahrstrasse erwartet"); routeSetName(pRoute, name); routeSingleParser(pRoute); i++; } tok = parserGetToken(); parserAssert(tokRBracket == tok, "zu viele Zugfahrstrassen angegeben"); /* I did not use parserExpectToken, because a better error * message can be given */ parserExpectTokenNeu(tokRBracket); /* now make the resolving of the route names given as illegal route * or conditional route. Note, these routes were probably not known * before the parsing of the last route */ i = 0; while(NULL != (pRoute = routeGet(i))) { for(j = 0; j < routeGetParts(pRoute); j++) { switch(routeGetPartType(pRoute, j)) { case RoutePartIllegalRoute: /* now get the name of the illegal route, search for it * in the routes and store the pointer to the route found */ (void) routeGetIllegalRouteName(pRoute, name, j); routeSetIllegalRoute(pRoute, routeFind(name), j); break; case RoutePartCondRoute: (void) routeGetCondRouteName(pRoute, name, j); routeSetCondRoute(pRoute, routeFind(name), j); break; default: break; } } } }
int main() { srand(time(NULL)); int WIDTH(750), HEIGHT(750); sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "RTest"); window.setFramerateLimit(60); RegionMap world(100, 80); Region *region1(nullptr), *region2(nullptr); std::vector<Region*> path; double offsetX = 0; sf::Vector2i clicPos; sf::Transform slide, loopSlide; while (window.isOpen()) { sf::Vector2i localPosition = sf::Mouse::getPosition(window); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } else if (event.type == sf::Event::MouseButtonPressed) { clicPos = localPosition; } } if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) { region1 = world.getRegionAt(localPosition.x, localPosition.y); int move = localPosition.x-clicPos.x; offsetX += move; slide.translate(sf::Vector2f(move, 0)); if (offsetX > 0) loopSlide.translate(sf::Vector2f(move-WIDTH, 0)); else loopSlide.translate(sf::Vector2f(move+WIDTH, 0)); clicPos = localPosition; } Region* newRegion2 = world.getRegionAt(localPosition.x, localPosition.y); if (newRegion2 != region2 && newRegion2) { region2 = newRegion2; path = world.getRoute(region1, region2); } sf::VertexArray routes(sf::Lines, 0); if (path.size()) { for (unsigned int i(0); i<path.size()-1; ++i) { unsigned int x = path[i]->capital()._x; unsigned int y = path[i]->capital()._y; sf::Vertex routePoint; routePoint.position = sf::Vector2f(x, y); routePoint.color = sf::Color::Red; routes.append(routePoint); routePoint.position = sf::Vector2f(path[i+1]->capital()._x, path[i+1]->capital()._y); routePoint.color = sf::Color::Red; routes.append(routePoint); } } window.clear(sf::Color::White); world.render(&window, slide); window.draw(routes, slide); window.draw(routes, loopSlide); window.display(); } return 0; }
void ForwardRules::buildDefaultRules(const char* domain, const char* hostname, const char* ipAddress, const char* fqhn, int localPort, TiXmlDocument& xmlDoc) { // Note: fqhn == fully qualified host name UtlString hostnamePort(hostname ? hostname : "localhost"); UtlString domainPort(domain ? domain : ""); UtlString ipAddressPort(ipAddress ? ipAddress : "127.0.0.1"); UtlString fqhnPort(fqhn ? fqhn : "localhost"); if(localPort == 5060) localPort = PORT_NONE; if(portIsValid(localPort)) { char portString[40]; sprintf(portString,":%d", localPort); hostnamePort.append(portString); domainPort.append(portString); ipAddressPort.append(portString); fqhnPort.append(portString); } UtlString sdsAddress(fqhn); sdsAddress.append(":5090"); UtlString statusAddress(fqhn); statusAddress.append(":5110"); UtlString regAddress(fqhn); regAddress.append(":5070"); UtlString configAddress("sipuaconfig"); UtlString configFqhnAddress(configAddress); configFqhnAddress.append("."); configFqhnAddress.append(domain); TiXmlElement routes("routes"); TiXmlElement route("route"); route.SetAttribute("mappingType", "local"); TiXmlElement routeFromDomain("routeFrom"); TiXmlText domainText(domainPort.data()); TiXmlElement routeFromFqhn("routeFrom"); TiXmlText fqhnText(fqhnPort.data()); TiXmlElement routeFromHost("routeFrom"); TiXmlText hostText(hostnamePort.data()); TiXmlElement routeFromIp("routeFrom"); TiXmlText ipText(ipAddressPort.data()); TiXmlElement methodMatch("methodMatch"); TiXmlElement methodPattern("methodPattern"); TiXmlText subscribe("SUBSCRIBE"); TiXmlElement fieldMatchConfig("fieldMatch"); fieldMatchConfig.SetAttribute("fieldName", "Event"); TiXmlElement fieldPatternConfig("fieldPattern"); TiXmlText configEvent("sip-config"); TiXmlElement routeToSds("routeTo"); TiXmlText sdsText(sdsAddress.data()); TiXmlElement fieldMatchStatus("fieldMatch"); fieldMatchStatus.SetAttribute("fieldName", "Event"); TiXmlElement fieldPatternStatus("fieldPattern"); TiXmlText mwiEvent("message-summary*"); TiXmlElement routeToStatus("routeTo"); TiXmlText statusText(statusAddress.data()); TiXmlElement routeToReg("routeTo"); TiXmlText regText(regAddress.data()); TiXmlElement routeConfig("route"); TiXmlElement routeFromFqhnConfig("routeFrom"); TiXmlText fqhnConfigText(configFqhnAddress.data()); TiXmlElement routeFromConfig("routeFrom"); TiXmlText configText(configAddress.data()); // Link everything up in reverse order as it TinyXml // makes copies routeFromDomain.InsertEndChild(domainText); route.InsertEndChild(routeFromDomain); routeFromHost.InsertEndChild(hostText); route.InsertEndChild(routeFromHost); routeFromFqhn.InsertEndChild(fqhnText); route.InsertEndChild(routeFromFqhn); routeFromIp.InsertEndChild(ipText); route.InsertEndChild(routeFromIp); methodPattern.InsertEndChild(subscribe); methodMatch.InsertEndChild(methodPattern); fieldPatternStatus.InsertEndChild(mwiEvent); fieldMatchStatus.InsertEndChild(fieldPatternStatus); routeToStatus.InsertEndChild(statusText); fieldMatchStatus.InsertEndChild(routeToStatus); methodMatch.InsertEndChild(fieldMatchStatus); fieldPatternConfig.InsertEndChild(configEvent); fieldMatchConfig.InsertEndChild(fieldPatternConfig); routeToSds.InsertEndChild(sdsText); fieldMatchConfig.InsertEndChild(routeToSds); methodMatch.InsertEndChild(fieldMatchConfig); routeToReg.InsertEndChild(regText); methodMatch.InsertEndChild(routeToReg); route.InsertEndChild(methodMatch); route.InsertEndChild(routeToReg); routeFromFqhnConfig.InsertEndChild(fqhnConfigText); routeConfig.InsertEndChild(routeFromFqhnConfig); routeFromConfig.InsertEndChild(configText); routeConfig.InsertEndChild(routeFromConfig); routeConfig.InsertEndChild(routeToReg); routes.InsertEndChild(route); routes.InsertEndChild(routeConfig); xmlDoc.InsertEndChild(routes); }