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

📄 vis_viv.c

📁 基于chord算法的p2p文件系统。A p2p file system based on chord.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*g++ -c vis_viv.C -I /usr/local/include -I /usr/X11R6/include/gtk-2.0/ -I /usr/local/include/glib-2.0 -I /usr/X11R6/include/pango-1.0/ -I /usr/local/include/atk-1.0/gcc -o vis vis_viv.o -L/usr/X11R6/lib -lgdk-x11-2.0 -lstdc++ -lgtk-x11-2.0 * Copyright (c) 2003-2005 Frank to Frank (via Emil and Frans) *                    Massachusetts Institute of Technology *  * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: *  * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. *  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */#include "gtk/gtk.h"#include "math.h"#include "getopt.h"#include <stdio.h>#include <fstream>#include <iostream>#include <vector>#include <string>using namespace std;#define WINX 700#define WINY 700#define PI 3.14159#define  uint int#define ulong long#define NELEM(x)	(sizeof (x)/ sizeof ((x)[0]))// Interesting things to draw and their handlers.static const unsigned int DRAW_IMMED_SUCC = 1 << 0;static const unsigned int DRAW_SUCC_LIST  = 1 << 1;static const unsigned int DRAW_IMMED_PRED = 1 << 2;static const unsigned int DRAW_DEBRUIJN   = 1 << 3;static const unsigned int DRAW_FINGERS    = 1 << 4;static const unsigned int DRAW_TOES       = 1 << 5;struct handler_info {  unsigned int flag;  char *name;  GtkWidget *widget;  GtkSignalFunc handler;};void draw_toggle_cb (GtkWidget *widget, gpointer data);// widgets will be initialized later, by initgraf.static handler_info handlers[] = {  { DRAW_IMMED_SUCC, "tails", NULL, GTK_SIGNAL_FUNC (draw_toggle_cb) },};unsigned int check_get_state (void);void check_set_state (unsigned int newstate);static int running = 0;static GdkPixmap *pixmap = NULL;static GtkWidget *window = NULL;static GtkWidget *drawing_area = NULL;static GtkWidget *drawing_area_r = NULL;static GdkGC *draw_gc = NULL;static GdkColormap *cmap = NULL;static GdkFont *courier10 = NULL;static GtkAdjustment *bar = NULL;static GtkWidget *scroll;static GdkColor red;static GdkColor search_color;static GtkWidget *last_clicked;static GtkWidget *total_nodes;static long interval = 1000000;static char *color_file;static bool drawids = false;static ifstream in;static GdkGCValues GCValues;static float zoomx = 1.0;static float zoomy = 1.0;static float centerx = 0.0;static float centery = 0.0;static int radius = 6;static bool displaysearch = false;static long begin = 0;static long endofsim = 1000;static long curtime;struct color_pair {  GdkColor c;  unsigned long lat;};vector<color_pair> lat_map;struct f_node {  int id;  vector<float> coords;  vector<float> coords_prev;  vector<int> neighbors;  unsigned int draw;  bool selected;  bool highlight;  int error;  f_node (int ip) : id (ip), selected(false), highlight(false) {    draw = check_get_state ();  };  ~f_node () {};};vector<f_node> nodes;void initgraf ();void init_color_list (char *filename);void draw_arrow (int fromx, int fromy, 		 int tox, int toy, GdkGC *draw_gc);static gint configure_event (GtkWidget *widget, GdkEventConfigure *event);static gint expose_event (GtkWidget *widget, GdkEventExpose *event);static gint delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);static gint key_release_event (GtkWidget *widget,			       GdkEventKey *event,			       gpointer data);static gint button_down_event (GtkWidget *widget,			       GdkEventButton *event,			       gpointer data);void select_all_cb (GtkWidget *widget, gpointer data);void select_none_cb (GtkWidget *widget, gpointer data);void draw_nothing_cb (GtkWidget *widget, gpointer data);void run_cb (GtkWidget *widget, gpointer data);void step_cb (GtkWidget *widget, gpointer data);void scroll_cb (GtkAdjustment *adj, gpointer data);void quit_cb (GtkWidget *widget, gpointer data);void redraw_cb (GtkWidget *widget, gpointer data);void search_cb (GtkWidget *widget, gpointer data);void zoom_in_cb (GtkWidget *widget, gpointer data);void dump_cb (GtkWidget *widget, gpointer data);void gtk_poll ();void draw_ring ();intfind (int n) {  for (uint i = 0; nodes.size (); i++) {    if (nodes[i].id == n) {      return i;    }  }  cerr << "couldn't find node with ID " << n << "\n";  return 0;}void print (){  for (uint i = 0; i < nodes.size (); i++) {    printf ("node %u is %16d\n", i, nodes[i].id);  }}voidadd_node (int n) {  for (uint i = 0; i < nodes.size (); i++) {    if (nodes[i].id > n) {      nodes.insert (nodes.begin() + i, f_node(n));      return;    }  }  nodes.push_back (f_node(n));}// --- drawing ----------------------------------------------------------------voidnode_to_xy (f_node *n, int *x, int *y, int prev = 0){  if (prev && n->coords_prev.size () > 1) {    *x = (int)(WINX/2 + ((n->coords_prev[0] -centerx)/zoomx)*WINX);    *y = (int)(WINX/2 + ((n->coords_prev[1] -centery)/zoomy)*WINY);  } else {    *x = (int)(WINX/2 + ((n->coords[0] - centerx)/zoomx)*WINX);    *y = (int)(WINX/2 + ((n->coords[1] - centery)/zoomy)*WINY);  }}intxy_to_ID (int sx, int sy){  int min = RAND_MAX;  int closest = nodes[0].id;  for (uint i = 0; i < nodes.size (); i++) {    int x,y;    node_to_xy (&nodes[i], &x, &y);    int dist = (sx - x)*(sx - x) + (sy - y)*(sy - y);    if (dist < min) {      closest = nodes[i].id;      min = dist;    }  }  return closest;}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);}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);  gtk_widget_show(scroll);}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 = std::atan2 ((float) (tox - fromx), (float) (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_node (f_node *iter){  int x, y, oldx, oldy;  int rad = (radius + 2);  node_to_xy (iter, &x, &y);  node_to_xy (iter, &oldx, &oldy, 1);  //rotation?  set_foreground_lat (iter->error);  //draw circle  gdk_draw_arc (pixmap, draw_gc, TRUE, x - radius, y - radius,		2*radius, 2*radius, (gint16)0, (gint16)64*360);  gdk_draw_line (pixmap,		 draw_gc,		 oldx,oldy,		 x,y);  //draw circle on selected nodes  if (iter->selected)    gdk_draw_arc (pixmap, draw_gc, FALSE, x - rad, y - rad, 2*rad, 		  2*rad, (gint16)0, (gint16)64*360);  if (drawids) {    char ids[128];    sprintf (ids, "%d", iter->id);    int fudge = -10;    if (x < WINX/2) fudge = 0;    gdk_draw_string (pixmap, courier10, drawing_area->style->black_gc, 		     x + fudge, y, ids);  }}voiddraw_ring (){  int x, y;  GtkWidget *widget = drawing_area;  //clear the draw  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);  for (vector<f_node>::iterator iter = nodes.begin (); iter != nodes.end ();        ++iter) {    f_node fx = *iter;    draw_node(&fx);  }    redraw ();}voidrecenter (){  float minx=RAND_MAX, miny=RAND_MAX;  float maxx=-RAND_MAX, maxy=-RAND_MAX;  for (uint i = 0; i < nodes.size (); i++) {    if (nodes[i].coords.size () > 0) {      float x = nodes[i].coords[0];      float y = nodes[i].coords[1];      minx = (x < minx) ? x : minx;      miny = (y < miny) ? y : miny;      maxx = (x > maxx) ? x : maxx;      maxy = (y > maxy) ? y : maxy;    }  }  centerx = (maxx + minx)/2.0;  centery = (maxy + miny)/2.0;    zoomx = maxx - minx;  zoomy = maxy - miny;  zoomx = zoomx*1.3 + 0.1;  zoomy = zoomy*1.3 + 0.1;  if (zoomx > zoomy) zoomy = zoomx;  if (zoomy > zoomx) zoomx = zoomy;  cerr << "recenter (" << centerx << ", " << centery << ") " << " (" << zoomx << ", " << zoomy << ")\n";}voidupdate () {}// --- process events ----------------------------------------------------------vector<string>split(string line, string delims){  string::size_type bi, ei;  vector<string> words;  bi = line.find_first_not_of(delims);  while(bi != string::npos) {    ei = line.find_first_of(delims, bi);    if(ei == string::npos)      ei = line.length();    words.push_back(line.substr(bi, ei-bi));    bi = line.find_first_not_of(delims, ei);  }  return words;}booldoevent (ulong t){  string line;  ulong ts = curtime;  bool step = false;  if  (curtime >= endofsim) {    return false;  }  while (getline (in, line)) {    vector <string> words = split (line, " ");    if (words.empty () || (words[0] != "vis"))       continue;    ulong ts1 = atoi(words[1].c_str ());    assert (ts1 >= curtime);    curtime = ts1;    //vis ts1 node ID [coords]    if (words[2] == "node") {      int id = strtol (words[3].c_str (), NULL, 16);      add_node (id);      f_node *n = &nodes[find(id)];      for (int i = 4; i < words.size (); i++)	n->coords.push_back(atoi(words[i].c_str()));    }    //vis ts1 step contacted error newpos    if (words[2] == "step" && words.size () > 6) {      int id = strtol (words[3].c_str (), NULL, 16);      int contacted = strtol (words[4].c_str (), NULL, 16);      int error = strtol (words[5].c_str (), NULL, 16);      int pos = find (id);      f_node *n = &nodes[pos];      n->error  = error;      n->coords_prev = n->coords;      n->coords.clear ();      for (int i = 6; i < words.size (); i++)	n->coords.push_back(atoi(words[i].c_str()));            // XXX draw line to contacted?    }        gtk_main_iteration_do (0);    if ((curtime >= endofsim) || step) break;  }  return (step);}    // --- UI ----------------------------------------------------------------unsigned intcheck_get_state (void){  unsigned int state = 0;  for (size_t i = 0; i < NELEM (handlers); i++)    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (handlers[i].widget)))      state |= handlers[i].flag;  return state;}voidcheck_set_state (unsigned int newstate){  // ONLY set the state of the buttons, do NOT actually toggle anything.  for (size_t i = 0; i < NELEM (handlers); i++) {    gtk_signal_handler_block_by_func (GTK_OBJECT (handlers[i].widget),				      GTK_SIGNAL_FUNC (handlers[i].handler),				      NULL);    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (handlers[i].widget),

⌨️ 快捷键说明

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