void roombatest::receive(uint8 len, const uint8 * buf, uint16 src_addr, uint16 dest_addr, uint16 lqi, uint8 seq_no, uint8 interface) { // os_.debug("receive, len: %i, src: %h, dest: %h", len, src_addr, dest_addr); if(len > 0) { // os_.debug("receive, buf[0]: %i", buf[0]); } if(!connectedTo && dest_addr == 0xffff) { if(len > 0 && buf[0] == ROOMBA_REMOTE_START_COMMUNICATION && connectCandidate == 0) { uint8 buffer[] = {ROOMBA_REMOTE_PING}; os_.radio().send(src_addr, 1, buffer, 0, NULL); connectCandidate = src_addr; os_.add_timeout_in(Time(2000), this, (uint8*) TASK_CONNECTCAND_TIMEOUT); } } if(!connectedTo && dest_addr == os_.id() && connectCandidate == src_addr) { if(len > 0 && buf[0] == ROOMBA_REMOTE_PONG) { connectCandidate = 0; connectedTo = src_addr; m_robot.setLeds(8,16,255); os_.add_timeout_in(Time(3000), this, (uint8*) TASK_PINGPONG); os_.debug("connected to %h", connectedTo); } } if(connectedTo == src_addr && dest_addr == os_.id()) { interpretMessage(len, buf); } }
void startDistNode(DistNode *node){ char header_buf[sizeof(MessageHeader)], msg_buf[BUF_SIZE], total_buf[BUF_SIZE + sizeof(MessageHeader)]; int fd, header_bytes, msg_bytes, offset = 0, last_hb = 0, hb_length = 0; MessageHeader* head; char* pipe_name = convertPidToPipe(node->pid); // Open pipe for reading if ((fd = open(pipe_name, O_RDONLY)) == -1) { perror("Failed to open pipe for reading"); exit(-1); } srand(time(NULL)); while (node->running) { // Synchronize with neighbors if (node->start_syn == 1 && node->syn == 0) { node->start_syn = 0; node->syn = 1; node->resp_count = 3; node->msg_count += 3; sendMessage(peek_bottom(node->local_hist)->pid, MSG_GETTIME, 0, 0); sendMessage(node->pid, MSG_GETTIME, 0, 0); sendMessage(node->local_hist->pid, MSG_GETTIME, 0, serializeListNode(node->local_hist)); } //read from pipe for new messages //if ((num_bytes = read(fd, buf, sizeof(MessageHeader) + BUF_SIZE)) == -1) { while ((header_bytes = read(fd, header_buf, sizeof(MessageHeader))) > 0) { head = (MessageHeader*) header_buf; if ((msg_bytes = read(fd, msg_buf, head->body_length)) == -1) { perror("Unable to read from pipe"); exit(-1); } memcpy(total_buf, header_buf, sizeof(MessageHeader)); memcpy(total_buf + sizeof(MessageHeader), msg_buf, strlen(msg_buf) + 1); interpretMessage(total_buf, node); } if (header_bytes == -1) { perror("Unable to read from pipe"); exit(-1); } } // Close pipe if (close(fd) == -1) { perror("Unable to close pipe"); exit(-1); } }
void startDistNode(DistNode *node) { char header_buf[sizeof(MessageHeader)], msg_buf[BUF_SIZE], total_buf[BUF_SIZE + sizeof(MessageHeader)]; int fd, header_bytes, msg_bytes, offset = 0, last_hb = 0, hb_length = 0; MessageHeader* head; char* pipe_name = convertPidToPipe(node->pid); // Open pipe for reading if ((fd = open(pipe_name, O_RDONLY)) == -1) { perror("Failed to open pipe for reading"); exit(-1); } srand(time(NULL)); while (node->running) { //if master node, check if it needs to send a heartbeat if( node->pid == node->master_pid) { if((node->offset - last_hb) > hb_length) { hb_length = MIN_HB + (rand() % (MAX_HB - MIN_HB + 1)); last_hb = node->offset; int sent = 0; //ask every node for their time ListNode *iter = node->local_hist; while(iter) { node->msg_count++; sendMessage(iter->pid, MSG_GETTIME, 0, 0); ++sent; iter = iter->next; } node->resp_count = sent; node->max_resp = sent; } } // Check to see if master has died. Determined to be dead if 5 seconds passed and there is no communication from master //else if (node->offset - node->master_comm > TIMEOUT) { if ((node->election_in_progress == 0) && (node->offset - node->master_comm > TIMEOUT)) { printf("Election Started\n"); // Start election algorithm node->last_sent = MSG_ELECTION_E; node->master_pid = 0; node->initiated_election = 1; node->election_in_progress = 1; node->msg_count++; sendMessage(node->local_hist->pid, MSG_ELECTION_E, node->pid, serializeListNode(node->local_hist)); } //read from pipe for new messages //if ((num_bytes = read(fd, buf, sizeof(MessageHeader) + BUF_SIZE)) == -1) { while ((header_bytes = read(fd, header_buf, sizeof(MessageHeader))) > 0) { head = (MessageHeader*) header_buf; if ((msg_bytes = read(fd, msg_buf, head->body_length)) == -1) { perror("Unable to read from pipe"); exit(-1); } memcpy(total_buf, header_buf, sizeof(MessageHeader)); memcpy(total_buf + sizeof(MessageHeader), msg_buf, strlen(msg_buf) + 1); interpretMessage(total_buf, node); } if (header_bytes == -1) { perror("Unable to read from pipe"); exit(-1); } } // Close pipe if (close(fd) == -1) { perror("Unable to close pipe"); exit(-1); } }