⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vis.c

📁 基于DHT的对等协议
💻 C
📖 第 1 页 / 共 3 页
字号:
		   draw_gc, 		   (gint)oldx, (gint)oldy,		   (gint)px,  (gint)py);    oldx = (int)px;    oldy = (int)py;  }  draw_arrow (oldx, oldy, tox, toy, draw_gc);}voidset_foreground_lat (unsigned long lat){  GdkColor c = lat_map[0].c;  unsigned int i = 0;  // Each map entry indicates the high-limit latency for the entry's  // color; we cap everything at the high-end.  while (i < lat_map.size () && lat > lat_map[i].lat)     c = lat_map[i++].c;  gdk_gc_set_foreground (draw_gc, &c);}voiddraw_arrow (int fromx, int fromy, 	    int tox, int toy, GdkGC *draw_gc){  gdk_draw_line (pixmap,		 draw_gc,		 fromx,fromy,		 tox,toy);  float t = atan2 ((tox - fromx), (toy - fromy));  float phi = PI/4 - t;  float theta = PI/4 + t;  float l = 10.0;  float px = l*sin (phi) + tox;  float py = -l*cos (phi) + toy;  float p2y = toy - l*cos (theta);  float p2x = tox - l*sin (theta);    GdkPoint head[4];  head[0].x = (gint)px;  head[0].y = (gint)py;  head[1].x = (gint)tox;  head[1].y = (gint)toy;  head[2].x = (gint)p2x;  head[2].y = (gint)p2y;  gdk_draw_polygon (pixmap,		    draw_gc,		    true,		    head,		    3);}voiddraw_ring (){  if (dual) {    if (ggeo)      drawing_area = drawing_area_g;    else      drawing_area = drawing_area_r;  }  int x, y;  GtkWidget *widget = drawing_area;  GdkColor red;  gdk_color_parse ("red", &red);  gdk_draw_rectangle (pixmap,		      widget->style->white_gc,		      TRUE,		      0,0,		      WINX, WINY);  gdk_draw_rectangle (pixmap,		      widget->style->black_gc,		      FALSE,		      0,0,		      WINX - 1, WINY - 1);  f_node *n = nodes.first ();  while (n) {    int radius = 5;    ID_to_xy (n->ID, &x, &y);    GdkGC *thisgc = widget->style->black_gc;    if (n->highlight) {      radius = 7;      gdk_gc_set_foreground (draw_gc, &highlight_color);      thisgc = draw_gc;    }    if (!drawids) {      gdk_draw_arc (pixmap,		    thisgc,		    TRUE,		    x - radius, y - radius,		    2*radius, 2*radius,		    (gint16)0, (gint16)64*360);            if (n->selected) {	radius += 2;	gdk_draw_arc (pixmap,		      thisgc,		      FALSE,		      x - radius, y - radius,		      2*radius, 2*radius,		      (gint16)0, (gint16)64*360);      }    }    if (drawids) {      char IDs[48];      ID_to_string (n->ID, IDs);      int fudge = -10;      if (x < WINX/2) fudge = 0;      gdk_draw_string (pixmap,		       courier10,		       widget->style->black_gc,		       x + fudge,y,		       IDs);    }    if (!(ggeo && dual)) {      if (n->successors && ((n->draw & DRAW_IMMED_SUCC) == DRAW_IMMED_SUCC) &&	  n->successors->resok->nlist.size () > 1) {	int a,b;	set_foreground_lat (n->successors->resok->nlist[1].a_lat); 	ID_to_xy (make_chordID (n->successors->resok->nlist[1].n), &a, &b);	draw_arrow (x,y,a,b, draw_gc);      }            if (n->fingers && ((n->draw & DRAW_FINGERS) == DRAW_FINGERS)) {	for (unsigned int i=1; i < n->fingers->resok->nlist.size (); i++) {	  int a,b;	  set_foreground_lat (n->fingers->resok->nlist[i].a_lat); 	  ID_to_xy (make_chordID (n->fingers->resok->nlist[i].n), &a, &b);	  draw_arrow (x,y,a,b, draw_gc);	}      }      if (n->fingers && ((n->draw & DRAW_TOP_FINGERS) == DRAW_TOP_FINGERS)) {	unsigned int i=n->fingers->resok->nlist.size () - 1;	int a,b;	set_foreground_lat (n->fingers->resok->nlist[i].a_lat); 	ID_to_xy (make_chordID (n->fingers->resok->nlist[i].n), &a, &b);	draw_arrow (x,y,a,b, draw_gc);      }            if (n->predecessor &&	  ((n->draw & DRAW_IMMED_PRED) == DRAW_IMMED_PRED) &&	  n->predecessor->resok->nlist.size () > 1) {	int a,b;	set_foreground_lat (n->predecessor->resok->nlist[1].a_lat); 	ID_to_xy (make_chordID (n->predecessor->resok->nlist[1].n), &a, &b);	draw_arrow (x,y,a,b, draw_gc);      }            if (n->successors && ((n->draw & DRAW_SUCC_LIST) == DRAW_SUCC_LIST)) {	for (unsigned int i=1; i < n->successors->resok->nlist.size (); i++) {	  draw_arc (n->ID, make_chordID (n->successors->resok->nlist[i].n),		    drawing_area->style->black_gc);	}      }            if (n->predecessor && ((n->draw & DRAW_PRED_LIST) == DRAW_PRED_LIST)) {	for (unsigned int i=1; i < n->predecessor->resok->nlist.size (); i++) {	  draw_arc (n->ID, make_chordID (n->predecessor->resok->nlist[i].n),		    drawing_area->style->black_gc);	}      }    }    n = nodes.next (n);  }  for (size_t i = 0; i < annotations.size (); i++) {    annotations[i]->draw (ggeo, drawing_area);  }    gdk_draw_string (pixmap, courier10,		   widget->style->black_gc,		   15, 20,		   last_clicked);    redraw ();  if(dual && !ggeo) {    ggeo = true;    draw_ring ();  } else if (dual) {    ggeo = false;  }}voidID_to_string (chordID ID, char *str) {  bigint little = (ID >> 144);  unsigned long z = little.getui ();  sprintf(str, "%lx", z);}double ID_to_angle (chordID ID){  bigint little = (ID >> 144);  int z = little.getsi ();   return (z/65535.0) * 2 * 3.14159;}voidrecenter (){  f_node *n = nodes.first ();  float minx=RAND_MAX, miny=RAND_MAX;  float maxx=-RAND_MAX, maxy=-RAND_MAX;  while (n) {    if (n->coords.size () > 0) {      float x = n->coords[xindex];      float y = n->coords[yindex];      minx = (x < minx) ? x : minx;      miny = (y < miny) ? y : miny;      maxx = (x > maxx) ? x : maxx;      maxy = (y > maxy) ? y : maxy;    }    n = nodes.next (n);  }  centerx = (maxx + minx)/2.0;  centery = (maxy + miny)/2.0;    zoomx = maxx - minx;  zoomy = maxy - miny;  zoomx *= 1.3;  zoomy *= 1.3;  if (zoomx > zoomy) zoomy = zoomx;  if (zoomy > zoomx) zoomx = zoomy;}voidxy_to_coord (int x, int y, float *cx, float *cy){  *cx = (x - WINX/2)*zoomx/WINX + centerx;  *cy = (y - WINY/2)*zoomy/WINY + centery;}voidID_to_xy (chordID ID, int *x, int *y){   f_node *f = nodes[ID];    if (ggeo) {    if (f && f->coords.size () > 0) {      *x = (int)(WINX/2 + ((f->coords[xindex] - centerx)/zoomx)*WINX);      *y = (int)(WINY/2 + ((f->coords[yindex] - centery)/zoomy)*WINY);    } else {      if (f) warn << f->ID << " no coords? what gives\n";      *x = WINX/2;       *y = WINY/2;    }  } else {    double angle = ID_to_angle (ID);    double radius = (WINX - 60)/2;        *x = (int)(WINX/2 + sin (angle)*radius);    *y = (int)(WINY/2 - cos (angle)*radius);  }}voidredraw() {  GdkRectangle update_rect;    update_rect.x = 0;  update_rect.y = 0;  update_rect.width = WINX;  update_rect.height = WINY;  gtk_widget_draw( drawing_area, &update_rect);}void usage (){  fatal << "vis [gtk options] -j <IP in dotted decimal>:<port> [-m accordion] [-a delay]\n";}intmain (int argc, char** argv) {  setprogname (argv[0]);  random_init ();  setup ();  gtk_init (&argc, &argv);  str host = "not set";  str sim_file = "network";  int cmd_port = 0;  unsigned short port = 0;  color_file = ".viscolors";  int ch;  while ((ch = getopt (argc, argv, "c:h:j:a:l:f:is:dm")) != -1) {    switch (ch) {    case 'c':      {	cmd_port = atoi (optarg);	break;      }    case 's':      {	simulated_input = true;	sim_file = optarg;	host = "simulated";	break;      }    case 'j':       {	char *bs_port = strchr(optarg, ':');	if (!bs_port) usage ();	*bs_port = 0;	bs_port++;	if (inet_addr (optarg) == INADDR_NONE) {	  //yep, this blocks	  struct hostent *h = gethostbyname (optarg);	  if (!h) {	    warn << "Invalid address or hostname: " << optarg << "\n";	    usage ();	  }	  struct in_addr *ptr = (struct in_addr *)h->h_addr;	  host = inet_ntoa (*ptr);	} else	  host = optarg;	port = atoi (bs_port);	break;      }    case 'a':      {	interval = atoi (optarg);      }      break;    case 'l':      {	glevel = atoi (optarg);	break;      }    case 'f':      {	color_file = optarg;      }      break;    case 'i':      {	drawids = true;      }      break;    case 'd':      {	dual = true;      }      break;    case 'h':      highlight = optarg;      break;    case 'm':      accordion = true;      break;  default:    usage ();    }  };  if (host == "not set")    usage ();  warn << " vis in " << (accordion? "ACCORDION" : "FINGER") << " mode\n";  initgraf ();  if (cmd_port > 0) {    setup_cmd (cmd_port);  }  if (!simulated_input) {    add_node (host, port);    get_cb (NULL);  } else    get_fingers (sim_file);  gtk_poll ();  amain ();}voidgtk_poll () {  // We never call main_loop so there's no reason to  // ever use gtk_main_quit. Thus we don't care about  // this return value. But we don't want this to block.  (void) gtk_main_iteration_do (false);  delaycb (0, 5000000, wrap (&gtk_poll));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -