void loop() { checkSerialAPI(); checkTimeout(); switch(toupper(CMD[0])){ case 'R': runningLights(DEL, BRI); break; case 'F': flashingLights(DEL, BRI); break; case 'P': police(DEL, BRI); break; case 'S': still(BRI); break; case 'D': disco(DEL, BRI); break; case 'G': glow(DEL, BRI); break; case 'O': default: off(); } }
/** * Initiating connection handshake to the server. (Overloading from superclass!) */ void ClientDbxZonePro::connect() { _pendingAnswer = false; _hasInitialized = false; _resetDeviceStateVariables(); if (_serialOutput) Serial.print(F("Connecting to dbx ZonePro... ")); if (_client.connect(_IP, _localPort)) { if (_serialOutput) Serial.println(F("connected")); _isConnected = true; _lastIncomingMsg = millis(); _lastStatusRequest = millis(); disco(); } else { if (_serialOutput) Serial.println(F("connection failed")); _isConnected = false; } _lastReconnectAttempt = millis(); }
/* Interacts with the tile at players feet. This will usually change the tile to an identical-looking but functionally inert tile. */ int interact() { int tile; char line[DEFLEN]; tile = gtile(player->y, player->x); if (tile == TL_P_CHEST || tile == TL_P_UWCHEST) { loot_chest(player->y, player->x); } else if (tile == TL_P_NPC1) { shop_chef(); } else if (tile == TL_P_NPC3) { shop_train(); } else if (tile == TL_P_NPC4) { shop_map(); } else if (tile == TL_P_NPC_ARMOR) { shop_armor(); } else if (tile == TL_P_NPC_RANGED) { shop_weapons(1); } else if (tile == TL_P_NPC_WEAPONS) { shop_weapons(0); } else if (tile == TL_P_NPC_CLOSED) { pwait("SORRY WE ARE CLOSED FOR RENOVATION"); } else if (tile == TL_P_NPC_SCUBA) { shop_scuba(); } else if (tile == TL_P_NPC_BAR) { ye_olde_bar(); } else if (tile == TL_P_NPC_SUSHI) { shop_sushi(); } else if (tile == TL_P_COFFIN) { stile(player->y, player->x, TL_COFFIN_HL); stile(player->y - 1, player->x, TL_VOID); stile(player->y - 1, player->x - 1, TL_VOID); stile(player->y - 1, player->x + 1, TL_VOID); stile(player->y - 2, player->x, TL_VOID); if (player->flip) { stile(player->y, player->x + 4, TL_COFFIN_LID_R); stile(player->y - 1, player->x + 3, TL_COFFIN_LID_R); stile(player->y - 2, player->x + 2, TL_COFFIN_LID_R); } else { stile(player->y, player->x - 4, TL_COFFIN_LID_L); stile(player->y - 1, player->x - 3, TL_COFFIN_LID_L); stile(player->y - 2, player->x - 2, TL_COFFIN_LID_L); } strcpy(line, "YOU OPEN THE COFFIN\n\n"); if (rand() % 5 == 0) { strcat(line, "A GHOUL ESCAPES!"); if (rand() % 2 == 0) make_monster(player->y, player->x - 5, MOB_GHOUL); else make_monster(player->y, player->x + 5, MOB_GHOUL); draw_board(); pwait(line); } else if (rand() % 5 == 0) { draw_board(); find_random_armor(line); } else { draw_board_norefresh(); give_item(line, 10 + rand() % COFFIN_GOLD, LOOT_NORMAL); } } else if (tile == TL_P_FOUNTAIN || tile == TL_P_BLOOD_FOUNTAIN) { fountain(); } else if (tile == TL_P_SWSTONE) { sword_in_stone(); } else if (tile == TL_P_ORB) { gaze_orb(); } else if (tile == TL_P_TABLET) { stone_tablet(); } else if (tile == TL_P_HELL) { stile(player->y, player->x, TL_VOID); pwait("YOU ARE IN HELL"); draw_board(); } else if (tile == TL_P_PORTAL) { portal_travel(); } else if (tile == TL_P_IDOL) { idol(); } else if (tile == TL_P_MUSHROOMS) { mushrooms(); } else if (tile == TL_P_BOOKSHELF) { bookshelf(); } else if (tile == TL_P_CAMP) { if (anything_near()) { pwait("YOU CANNOT REST NOW!\nENEMIES ARE NEAR"); return false; } camp(); } else if (tile == TL_P_DISCO) { if (anything_near()) { pwait("YOU CANNOT DANCE WHILE\nSOMEONE IS WATCHING"); return false; } disco(); } else if (tile == TL_P_ALTAR) { altar(); } else if (tile == TL_P_CORPSE || tile == TL_P_SKELETON) { if (tile == TL_P_CORPSE) { stile(player->y, player->x, TL_VOID); strcpy(line, "YOU FIND A DECAYING CORPSE\n\n"); } else { stile(player->y, player->x, TL_VOID); strcpy(line, "YOU FIND A LONG-DEAD SKELETON\n\n"); } switch(rand() % 10) { case 0: case 1: case 2: find_random_armor(line); break; case 3: case 4: case 5: find_random_weapon(line); break; default: if (tile == TL_P_SKELETON) give_item(line, 5 + rand() % 25, LOOT_BONES); else give_item(line, 5 + rand() % 25, LOOT_NORMAL); break; } //pwait(line); return true; } return true; }
void parse_args(struct client *client, int *argc, char **argv[]) { assert(client && argc && argv); static const struct option opts[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'v' }, { "ignorecase", no_argument, 0, 'i' }, { "wrap", no_argument, 0, 'w' }, { "list", required_argument, 0, 'l' }, { "prompt", required_argument, 0, 'p' }, { "index", required_argument, 0, 'I' }, { "prefix", required_argument, 0, 'P' }, { "scrollbar", required_argument, 0, 0x100 }, { "bottom", no_argument, 0, 'b' }, { "grab", no_argument, 0, 'f' }, { "monitor", required_argument, 0, 'm' }, { "fn", required_argument, 0, 0x101 }, { "tb", required_argument, 0, 0x102 }, { "tf", required_argument, 0, 0x103 }, { "fb", required_argument, 0, 0x104 }, { "ff", required_argument, 0, 0x105 }, { "nb", required_argument, 0, 0x106 }, { "nf", required_argument, 0, 0x107 }, { "hb", required_argument, 0, 0x108 }, { "hf", required_argument, 0, 0x109 }, { "sb", required_argument, 0, 0x110 }, { "sf", required_argument, 0, 0x111 }, { "scb", required_argument, 0, 0x112 }, { "scf", required_argument, 0, 0x113 }, { "disco", no_argument, 0, 0x114 }, { 0, 0, 0, 0 } }; /* TODO: getopt does not support -sf, -sb etc.. * Either break the interface and make them --sf, --sb (like they are now), * or parse them before running getopt.. */ for (;;) { int32_t opt = getopt_long(*argc, *argv, "hviwl:I:p:P:I:bfm:", opts, NULL); if (opt < 0) break; switch (opt) { case 'h': usage(stdout, *argv[0]); break; case 'v': version(*argv[0]); break; case 'i': client->filter_mode = BM_FILTER_MODE_DMENU_CASE_INSENSITIVE; break; case 'w': client->wrap = true; break; case 'l': client->lines = strtol(optarg, NULL, 10); break; case 'p': client->title = optarg; break; case 'P': client->prefix = optarg; break; case 'I': client->selected = strtol(optarg, NULL, 10); break; case 0x100: client->scrollbar = (!strcmp(optarg, "always") ? BM_SCROLLBAR_ALWAYS : (!strcmp(optarg, "autohide") ? BM_SCROLLBAR_AUTOHIDE : BM_SCROLLBAR_NONE)); break; case 'b': client->bottom = true; break; case 'f': client->grab = true; break; case 'm': client->monitor = strtol(optarg, NULL, 10); break; case 0x101: client->font = optarg; break; case 0x102: client->colors[BM_COLOR_TITLE_BG] = optarg; break; case 0x103: client->colors[BM_COLOR_TITLE_FG] = optarg; break; case 0x104: client->colors[BM_COLOR_FILTER_BG] = optarg; break; case 0x105: client->colors[BM_COLOR_FILTER_FG] = optarg; break; case 0x106: client->colors[BM_COLOR_ITEM_BG] = optarg; break; case 0x107: client->colors[BM_COLOR_ITEM_FG] = optarg; break; case 0x108: client->colors[BM_COLOR_HIGHLIGHTED_BG] = optarg; break; case 0x109: client->colors[BM_COLOR_HIGHLIGHTED_FG] = optarg; break; case 0x110: client->colors[BM_COLOR_SELECTED_BG] = optarg; break; case 0x111: client->colors[BM_COLOR_SELECTED_FG] = optarg; break; case 0x112: client->colors[BM_COLOR_SCROLLBAR_BG] = optarg; break; case 0x113: client->colors[BM_COLOR_SCROLLBAR_FG] = optarg; break; case 0x114: disco(); break; case ':': case '?': fputs("\n", stderr); usage(stderr, *argv[0]); break; } } *argc -= optind; *argv += optind; }
std::pair<int, int> simulacion(int bloques, int bloque_size, int vias, int accesos, int pagina_size) { int paginas_disco, paginas_mem, fallos_pagina, fallos_cache, bits_offset, div_virt, div_fisica; std::cout << "Numero de bloques: " << bloques << std::endl; std::cout << "Tamano de bloque: " << bloque_size << std::endl; std::cout << "Numero de vias: " << vias << std::endl; std::cout << "Numero de accesos: " << accesos << std::endl; std::cout << "Tamanio de pagina: " << pagina_size << std::endl; std::cout << "Inicializando..."; std::random_device rseed; // Para numeros aleatorios std::mt19937 rgen(rseed()); // mersenne_twister std::uniform_int_distribution<int> idist(0, DIR_VIRUTALES - 1); // [0,4095] std::uniform_int_distribution<int> odist(0, 1); // [0,1] std::uniform_int_distribution<int> ddist(0, 255); // [0,255] std::uniform_int_distribution<int> nueva_dist(256, 511); // [0,255] /* ins_virtuales[*][x], x: 0 - direccion, 1 - lectura/escritura, 2 - dato */ std::vector<std::vector<int> > ins_virtuales (accesos, std::vector<int> (3,0)); std::vector<int> memoria (POS_MEMORIA); std::vector<int> disco (POS_DISCO); t_tabla tabla; /* Creamos la cache */ Cache mem_cache (vias, bloques, bloque_size); /* Inicializacion */ paginas_disco = POS_DISCO / pagina_size; paginas_mem = POS_MEMORIA / pagina_size; std::uniform_int_distribution<int> mdist(0, paginas_mem-1); // [0,paginas_memoria] fallos_pagina = 0; fallos_cache = 0; bits_offset = bits_para(pagina_size); div_virt = potencia(bits_offset);// para posterior division div_fisica = potencia(bits_para(bloque_size));// para posterior division std::cout << " Inicializacion terminada!" << std::endl; std::cout << "Paginas Memoria: " << paginas_mem << std::endl; std::cout << "Paginas Disco: " << paginas_disco << std::endl; std::cout << "Generando instrucciones..." << std::endl; /* Generar instrucciones virtuales */ for (int i = 0; i < accesos; ++i) { ins_virtuales[i][0] = idist(rgen); ins_virtuales[i][1] = odist(rgen); ins_virtuales[i][2] = nueva_dist(rgen); } std::cout << " Terminado!" << std::endl; std::cout << "Generando tabla de traduccion..." << std::endl; /* Generamos la tabla de traduccion */ int contador; for (contador = 0; contador < accesos; ++contador) { int tmp = ins_virtuales[contador][0]/div_virt; if(tabla.size() > paginas_mem) break; if(tabla.count(tmp) == 0) { tabla[tmp].push_back(odist(rgen)); /* 1 - memoria principal */ tabla[tmp].push_back(0); /* 1 - dato en disco mem llena */ tabla[tmp].push_back(contador); /* dir fisica */ } } for (; contador < accesos; ++contador) { int tmp = ins_virtuales[contador][0]/div_virt; if(tabla.size() >= (paginas_mem + paginas_disco)) break; if(tabla.count(tmp) == 0) { tabla[tmp].push_back(0); /* 1 - memoria principal */ tabla[tmp].push_back(1); /* 1 - dato en disco mem llena */ tabla[tmp].push_back(contador); /* dir disco */ } } std::cout << " Terminado!" << std::endl; std::cout << " Tamaño tabla: " << tabla.size() << std::endl; /* leemos la memoria y el disco */ std::ifstream inputmem; std::ifstream inputdisc; std::string outmem; std::string outdisc; int valor_io; int contador_io = 0; std::cout << "Leyendo memoria..." << std::endl; inputmem.open("memoria.txt", std::ifstream::in); while(inputmem >> valor_io) { memoria[contador_io] = valor_io; contador_io++; } inputmem.close(); std::cout << " Terminado!" << std::endl; if (contador_io == 0) { std::cout << "Memoria vacia, abortando!" << std::endl; return std::make_pair(0,0); } std::cout << "Leyendo disco..." << std::endl; inputdisc.open("disco.txt", std::ifstream::in); contador_io = 0; while(inputdisc >> valor_io) { disco[contador_io] = valor_io; contador_io++; } inputdisc.close(); std::cout << " Terminado!" << std::endl; if (contador_io == 0) { std::cout << "Disco vacio, abortando!" << std::endl; return std::make_pair(0,0); } std::cout << "Procesando instrucciones..." << std::endl; /* Iteramos en cada instruccion */ int dir_fisica, tmp, tmp2; std::vector<int> movimiento (bloque_size,0); std::vector<int> respuesta_cache; for (int i = 0; i < accesos; ++i) { /* Traducimos direccion virtual a fisica */ dir_fisica = ins_virtuales[i][0]/div_virt; /* No esta en memoria principal? */ if(tabla[dir_fisica][0] == 0) { //std::cout << "Fallo Pagina!" << std::endl; tabla[dir_fisica][0] = 1; fallos_pagina++; // nuevo fallo de pagina tmp2 = tabla[dir_fisica][2]; // direccion disco /* no esta asigana? */ if(tabla[dir_fisica][1] == 1) { tabla[dir_fisica][1] = 0; tmp = mdist(rgen); // nueva asignacion. tabla[dir_fisica][2] = tmp; /* Movemos de disco a memoria */ } else tmp = tmp2; // Si esta asignada disco - memoria concuerdan. tmp = tmp * div_virt; tmp2 = tmp2 * div_virt; for(int j = 0; j < pagina_size; ++j) { memoria[tmp + j] = disco[tmp2 + j]; } } /* El dato ya esta en memoria principal */ /* Extraemos direccion fisica */ dir_fisica = tabla[dir_fisica][2] * div_virt; /* Agregamos el offset */ dir_fisica = dir_fisica + (ins_virtuales[i][0] % div_virt); /* Cargamos los datos que hay en la memoria por si hay un miss en cache */ tmp = dir_fisica - (dir_fisica % div_fisica); // quitamos el offset de un bloque. for (int j = 0; j < bloque_size; ++j) { movimiento[j] = memoria[tmp + j]; } /* Lectura o escritura */ if (ins_virtuales[i][1] == 0) { //std::cout << "Read" << std::endl; respuesta_cache = mem_cache.read_cache(dir_fisica, movimiento); } else { //::cout << "Write" << std::endl; respuesta_cache = mem_cache.write_cache(dir_fisica, movimiento, ins_virtuales[i][2]); } /* Analimamos la respuesta de la cache */ /* no fue un hit? */ if(respuesta_cache[0] != 1) fallos_cache++; /* hay que escribir en memoria, por write-back? */ if (respuesta_cache[1] == 1) { //std::cout << "write-back" << std::endl; tmp = respuesta_cache[2]; // donde, escribir tmp = tmp - (tmp % div_fisica); // quitamos el offset del bloque. for (int j = 0; j < bloque_size; ++j) { memoria[tmp + j] = respuesta_cache[3+j]; } } } std::cout << " Terminado!" << std::endl; std::cout << "Reescribiendo memoria..." << std::endl; /* Excribimos en los archivos */ std::ofstream ofm ("memoria.txt", std::ofstream::out); for (int i = 0; i < POS_MEMORIA; ++i) { ofm << memoria[i] << "\n"; } ofm.close(); std::cout << "Terminado!" << std::endl; std::cout << "Reescribiendo disco..." << std::endl; std::ofstream ofd ("disco.txt", std::ofstream::out); for (int i = 0; i < POS_DISCO; ++i) { ofd << disco[i] << "\n"; } ofd.close(); std::cout << "Terminado!" << std::endl; std::cout << fallos_pagina << " " << fallos_cache << std::endl; return std::make_pair(fallos_pagina, fallos_cache); }
/** * Sends status command. (Overloading from superclass) */ void ClientDbxZonePro::_sendStatus() { disco(); }
/** * Sends ping command. (Overloading from superclass) */ void ClientDbxZonePro::_sendPing() { disco(); // _client.write(0x8C); }
int main(int argc, char** argv) { // get a configuration object Configuration &conf = Configuration::getInstance(); int opt = 0; std::string p_hostname = conf.getHostname(); std::string p_iface = "lo"; std::string p_ntpserver = "127.0.0.1"; bool p_gps = false; unsigned int _p_disco_port = 3232; unsigned int _p_port = 3486; while((opt = ::getopt(argc, argv, "hd:vgp:n:i:t:")) != -1) { switch (opt) { case 'h': print_help(); return 0; case 'd': _p_disco_port = ::atoi(optarg); break; case 'p': _p_port = atoi(optarg); break; case 'g': p_gps = true; break; case 'n': p_hostname = optarg; break; case 'i': p_iface = optarg; break; case 't': p_ntpserver = optarg; break; default: std::cout << "unknown command" << std::endl; return -1; } } // some output for the user std::cout << "startup of hydra node daemon" << std::endl; std::cout << "hostname: " << p_hostname << std::endl; // listen on interface const ibrcommon::vinterface iface(p_iface); conf.setInterface(iface); // create a fake gps FakeGPS &gps = FakeGPS::getInstance(); if (!p_gps) gps.disable(); // create clock monitor instance ClockMonitor &cm = ClockMonitor::getInstance(); cm.setReference(p_ntpserver); cm.start(); // listen on incoming tcp connections CommandServer srv(_p_port); srv.start(); while (true) { try { // run discovery module DiscoverComponent disco(p_hostname, _p_disco_port, iface); disco.run(); } catch (const std::exception&) { // error retry in 2 seconds std::cout << "can not listen on multicast socket" << std::endl; ibrcommon::Thread::sleep(2000); } } }