form * sendanswer_1_svc(form *argp, struct svc_req *rqstp) { static form result; if(current_user == manager) //se processo corrente e o coordenador recebendo respostas { printf("Coordenador recebeu respostas de %s\n", argp->attr.address); if(find_by_address(argp->attr) >= 0) { printf("1\n"); int index = find_by_address(argp->attr); printf("2\n"); int formsize = sizeof(form); printf("3\n"); memcpy(&answer[index], argp, formsize); printf("4\n"); count_answer[index] = 1; printf("5\n"); check_end(); printf("6\n"); } } else //se processo corrente recebe resposta do cliente, a envia para o coordenador { questions_answered++; printf("Cliente enviando respostas ao coordenador\n"); memcpy(&answer[current_user],argp,sizeof(form)); CLIENT *clnt; form *result_4; form sendanswer_1_arg; memcpy(&sendanswer_1_arg, &answer[current_user], sizeof(form)); char *host = list[manager].ip; printf("host: %s usuario numero: %d\n",host,manager); clnt = clnt_create (host, PROGJOGO, VERJOGO, "udp"); if (clnt == NULL) { reset = 1; } strcpy(sendanswer_1_arg.attr.address, list[current_user].ip); result_4 = sendanswer_1(&sendanswer_1_arg, clnt); if (result_4 == (form *) NULL) { reset = 1; } //manda pro grandao //falta tratamento caso o coordenador esteja morto } return &result; }
bool __init board_staging_dt_node_available(const struct resource *resource, unsigned int num_resources) { unsigned int i; for (i = 0; i < num_resources; i++) { const struct resource *r = resource + i; if (resource_type(r) == IORESOURCE_MEM) if (find_by_address(r->start)) return true; /* DT node available */ } return false; /* Nothing found */ }
static DBusMessage *find_adapter(DBusConnection *conn, DBusMessage *msg, void *data) { DBusMessage *reply; struct adapter *adapter; char *path; struct hci_dev_info di; const char *pattern; int dev_id; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern, DBUS_TYPE_INVALID)) return NULL; /* hci_devid() would make sense to use here, except it is restricted to devices which are up */ if (!strncmp(pattern, "hci", 3) && strlen(pattern) >= 4) dev_id = atoi(pattern + 3); else dev_id = find_by_address(pattern); if (dev_id < 0) return no_such_adapter(msg); if (hci_devinfo(dev_id, &di) < 0) return no_such_adapter(msg); if (hci_test_bit(HCI_RAW, &di.flags)) return no_such_adapter(msg); adapter = manager_find_adapter_by_id(dev_id); if (!adapter) return no_such_adapter(msg); reply = dbus_message_new_method_return(msg); if (!reply) return NULL; path = adapter->path + ADAPTER_PATH_INDEX; dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path, DBUS_TYPE_INVALID); return reply; }
static DBusMessage *old_find_adapter(DBusConnection *conn, DBusMessage *msg, void *data) { DBusMessage *reply; char path[MAX_PATH_LENGTH], *path_ptr = path; struct hci_dev_info di; const char *pattern; int dev_id; if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &pattern, DBUS_TYPE_INVALID)) return invalid_args(msg); /* hci_devid() would make sense to use here, except it is restricted to devices which are up */ if (!strncmp(pattern, "hci", 3) && strlen(pattern) >= 4) dev_id = atoi(pattern + 3); else dev_id = find_by_address(pattern); if (dev_id < 0) return no_such_adapter(msg); if (hci_devinfo(dev_id, &di) < 0) return no_such_adapter(msg); if (hci_test_bit(HCI_RAW, &di.flags)) return no_such_adapter(msg); reply = dbus_message_new_method_return(msg); if (!reply) return NULL; snprintf(path, sizeof(path), "%s/hci%d", BASE_PATH, dev_id); dbus_message_append_args(reply, DBUS_TYPE_STRING, &path_ptr, DBUS_TYPE_INVALID); return reply; }
control * checkhost_1_svc(control *argp, struct svc_req *rqstp) { printf("Game %d", gamestarted); if(gamestarted) //se game ainda nao comecou para o usuario corrente, le arquivo de jogadores e inicia partida { printf("Jogo comecando\n"); readPlayers(); gamestarted = 0; printf("Nome: %sIdade: %d\nIP: %s\n",list[current_user].name,list[current_user].age,list[current_user].ip); } static control result; CLIENT *clnt; control *result_2; control checkhost_1_arg; if(argp->action == 100) //outro servidor avisa que processo corrente e o de maior prioridade vivo achado por ele { printf("Fui avisado que sou o novo coordenador\n"); int i; char *host; strcpy(checkhost_1_arg.attr.address, list[current_user].ip); for(i=0;i<0;i++) { if(i != current_user) { host = list[i].ip; clnt = clnt_create (host, PROGJOGO, VERJOGO, "udp"); if (clnt == NULL) { continue; } checkhost_1_arg.attr.booleanVar = 1; result_2 = checkhost_1(&checkhost_1_arg, clnt); if (result_2 == (control *) NULL) { continue; } printf("Msg enviada para %s", host); //enviar mensagem avisando que agora o processo corrente e o coordenador } } has_new_player = 1; manager = current_user; } else if(argp->attr.booleanVar == 1)//quando recebe aviso do novo coordenador { manager = find_by_address(argp->attr); printf("Recebi msg de %d, dizendo que ele e o novo coordenador\n",manager); clear_all(); } else// quando recebe mensagem originada por um cliente { printf("Cliente detectou que coordenador esta fora\n"); char *host; int i; for(i=0;i<10;i++) { if(i == current_user) //quando processo corrente e o de maior prioridade { manager = current_user; printf("Sou o player de mais alta prioridade\n"); strcpy(checkhost_1_arg.attr.address, list[current_user].ip); for(i=0;i<10;i++) { if(i != current_user) { host = list[i].ip; clnt = clnt_create (host, PROGJOGO, VERJOGO, "udp"); if (clnt == NULL) { printf("Host %s esta fora\n",host); continue; } checkhost_1_arg.attr.booleanVar = 1; result_2 = checkhost_1(&checkhost_1_arg, clnt); if (result_2 == (control *) NULL) { printf("Host %s esta fora\n",host); continue; } //enviar mensagem avisando que agora o processo corrente e o coordenador } printf("Enviei msg para %s",host); } break; } else if(i != current_user)// procura o processo de maior prioridade e envia mensagem { host = list[i].ip; printf("Verificando que %s esta vivo para ser o novo coordenador\n",host); clnt = clnt_create (host, PROGJOGO, VERJOGO, "udp"); if (clnt == NULL) { continue; } checkhost_1_arg.action = 100; //avisa ao host destino que e o coordenador result_2 = checkhost_1(&checkhost_1_arg, clnt); if (result_2 == (control *) NULL) { continue; } printf("%s sera o novo coordenador\n",host); manager = i; break; } } } return &result; }