/* ** Authors: Krishna Puttaswamy ** this file represents an instance of a dht node for the churn test of dht */ #include #include #include #include #include #include #include #include #include #include #include #include #include "dht.h" #include "chimera.h" #include "log.h" #include "key.h" #include "route.h" #include "dtime.h" //joinwaittime is the time the node waits before sending the message #define USAGE "timeddhtnode [ -j bootstrap:port ] port key timetolive" #define OPTSTR "j:" #define RAND() (lrand48()) #define SEED(s) (srand48(s)) char *optarg; int optind; char mynodekey[195]; ChimeraState *state; bool isbootstrap = false; pthread_mutex_t lock; void die_handler(int sig) { exit(0); } void signal_handler(int sig) { if (sig == SIGALRM) { if (isbootstrap) fprintf(stderr, "BOOTSTRAP NODE %s is dying \n", mynodekey); else fprintf(stderr, "%s is dying \n", mynodekey); log_message (state->log, LOG_DATA, "dying of timeout \n"); exit(0); } } bool read_from_file(FILE *fp, char *block) { // assumption is that the file contains each entry of size entry_size if (fgets(block, ENTRY_SIZE, fp) == NULL) return false; int i = 0; // replace new line -- just to making the output pretty int len = strlen(block); for(i = 0; i < len; i++) if(block[i] == '\n') { block[i] = '\0'; break;; } return true; } int main(int argc, char **argv) { int opt; char *hn = NULL; int port, joinport; ChimeraHost *host = NULL; char tmp[256]; Key key, keyinput; int i, j; char *digest; char message[] = " Hello "; mynodekey[0] = '\0'; signal(SIGALRM, signal_handler); /*fprintf(stderr, "%d arguments are ... ", argc); for ( i = 0; i < argc; i++) { fprintf(stderr, " %s ", argv[i]); } fprintf(stderr, "\n"); fflush(stderr); */ while((opt = getopt(argc, argv, OPTSTR)) != EOF) { switch((char) opt) { case 'j': for(i=0; optarg[i] != ':' && ilog, LOG_DATA, datalogfp); //log_direct(state->log, LOG_WARN, stderr); //log_direct(state->log, LOG_ERROR, stderr); if(host != NULL) { chimera_join(state,host); } //fprintf(stderr,"** send messages to key with command **\n"); if(hn == NULL) { // The amount by which the driver script sleeps for the bootstrap node isbootstrap = true; sleep(5); // sleep for the same amount as the driver } dhtlayer_init(state, mynodekey); int ii = 0; if (isbootstrap) { fprintf(stderr, "I am the bootstrap node, taking the control!!\n"); dsleep(30); FILE *blockFP = fopen(BLOCKS_FILE, "r"); int i = 0; char block[100]; for (i = 0; i < 10; i++){ if (read_from_file(blockFP, block) == false) break; fprintf(stderr, "Storing %s \n", block); Key tempKey; str_to_key(block, &tempKey); DhtMessage *send = get_new_dhtmessage(block, block, mynodekey, NULL); chimera_send(state, tempKey, DHT_PUT_ROOT, sizeof(DhtMessage), (char *)send); free_dhtmessage(send); dsleep(.1); } fclose(blockFP); fprintf(stderr, "Inserted %d blocks into the DHT ...now looking them up\n", i); dsleep(100); fprintf(stderr, "now, should lookup \n"); blockFP = fopen(BLOCKS_FILE, "r"); for (i = 0; i < 10; i++){ if (read_from_file(blockFP, block) == false) break; fprintf(stderr, "Looking for %s\n", block); Key tempKey; str_to_key(block, &tempKey); DhtMessage *send = get_new_dhtmessage(block, NULL, mynodekey, NULL); chimera_send(state, tempKey, DHT_GET_ROOT, sizeof(DhtMessage), (char *)send); free_dhtmessage(send); dsleep(.1); } fclose(blockFP); fprintf(stderr, "Lookedup %d blocks into the DHT \n", i); fflush(stderr); sleep(10); blockFP = fopen(HOSTS_FILE, "r"); int count = 0; while(read_from_file(blockFP, block)){ if (count == 75) break; //fprintf(stderr, "block read is %s \n",block); Key tempKey; char host[100], temp[100]; int kkk; sscanf (block, "%s %d %s", host, &kkk, temp); str_to_key(temp, &tempKey); char str[10] = "test"; chimera_send(state, tempKey, DHT_DUMP_STATE, strlen(str)+1 , (char *)str); count++; dsleep(.1); } fclose(blockFP); fprintf(stderr, "Send %d dump messages \n", count); } while(1) sleep(1000); //fclose(datalogfp); //fclose(securelogfp); fprintf(stderr, "exiting... \n"); exit(0); }