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

📄 sim.c

📁 一个linux下的各种组播路由算法编程
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
 * Copyright (c) 1995 Center for Advanced Computing and Communications (CACC),
 * North Carolina State University at Raleigh.
 * All rights reserved.
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  The CACC makes no representations about the
 * suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 */


/*****************************************************************************
***            Project: GUI for a Computer Network Simulator               ***
***                       Date: September 9, 1994                          ***
***                            File: sim.c                                 ***
***          The main program and Motif callbacks and event handlers       ***
*****************************************************************************/

#include "sim.h"

//Definition of global variabnles


//First define the main objects: a message bar, a command bar, a graphics
//object, a network manager, and a toplevel widget.
messageBar msgb;
commandBar cmdb;
graphics painter;
TheNodeList manager;
Widget toplevel;


//Define some general purpose variables
int flag, i, j, model, symmetry, batch, alg;
double tmpx, tmpy, tmpz, tmpx2, tmpy2;
NodeListEntry *tmpNode;
AdjacencyListEntry *tmpAdj;

//Define and initialize the parameter for the random network and random link 
//generators
float beta = 2.2;
int   number = 20;
float degree = 4;
float alpha = .15;

//Define and initialize the parameters for the simulation
double batchSize = .01;   //100 msec is the default size of a simulation batch
int    minNumBatches = 3;
int    maxNumBatches = 5;
   
char *currentFile; //name of the curren5t file

void changeTitle() {
   //Changes the title displayed on the title bar to file name.

   Arg al[10];
   int ac;
   char s[50];

   if ((currentFile != NULL) && (currentFile != "")) {
      strcpy(s, "SIM - ");
      strcat(s, currentFile);
      ac = 0;
      XtSetArg(al[ac], XmNtitle, s); ac++;
      XtSetValues(toplevel, al, ac);
   }   
   else {
      strcpy(s, "SIM");
      ac = 0;
      XtSetArg(al[ac], XmNtitle, s); ac++;
      XtSetValues(toplevel, al, ac);
   }; 
};   

main(Cardinal argc, char *argv[]) {

   Arg al[10];
   int ac;

   //Define widgets, menus, and menu items
   XtAppContext context;

   Widget form, MenuBar, MessageBar, CommandBar;
   Widget Separator1, Separator2, DrawingArea;

   menu file, edit, parameters, generator, groups_sources, routing, 
        AddBackgroundTraffic, AddSymmetricBackgroundTraffic,
        simulation, display, DisplayLinkCost, DisplayLinkCap, DisplayNodeName,
        DisplayGroup, DisplayTree, AddSource, Cost, Constrained, Dbv;

   menuItem Open, Save, Close, Exit,
            AddNode, RemoveNode, MoveNode, AddLink, RemoveLink, Refresh,
            Renumber, EditLinkCap,
            Alpha2, Beta2, Number, Degree, DefCap, RandomGraph, RandomLinks,
            AddSourceVoice, AddSourceVideo, RemoveSource, 
            AddDestination, RemoveDestination, 
            AddRandomGroup, RemoveGroup,
            RemoveBackgroundTraffic,
            Dks, Bf, Pim, Mst, Waters, ModWaters, Cstc, Cstcd, Opt, Copt,
            Dksld, Kmb, Cao, Bsma, Dcdimst, Dimst, Dvmrp, Bfnoadm, Cdks,
            RemoveTree,
            BatchSize, MinNumBatches, MaxNumBatches, Run,
            DLCYes, DLCNo, DLCapYes, DLCapNo, DNNYes, DNNNo,
            DGYes, DGNo, DTYes, DTNo, DGraphDegree, Peak, Average,
            Muxed1, Batch1, Muxed2, Batch2,
            AdmitCapacity, DelayBound, DelayStep, DbvYes, DbvNo;

   currentFile = new char(20); 
   currentFile = "";

   //initialize the random numbers generator
   srand(time(NULL));

   // create the toplevel shell and resize it
   toplevel = XtAppInitialize(&context,"",NULL,0,&argc,argv,NULL,NULL,0);
   ac=0;
   XtSetArg(al[ac],XmNheight,SIDE + 100); ac++;
   XtSetArg(al[ac],XmNwidth,SIDE + 30); ac++;
   XtSetArg(al[ac], XmNresizable, False); ac++;
   XtSetValues(toplevel,al,ac);
   changeTitle();

   // Create a form to hold widgets 
   ac = 0;
   XtSetArg(al[ac], XmNforeground, 0); ac++;
   XtSetArg(al[ac], XmNbackground, 0); ac++;
   XtSetArg(al[ac], XmNtopShadowColor, 0); ac++;
   XtSetArg(al[ac], XmNresizable, False); ac++;
   form = XmCreateForm(toplevel, "form", al, ac);
   XtManageChild(form);   

   // create a menu bar
   menuBar mb(form);
   MenuBar = mb.getWidget();

   // cereate a drawing area
   drawingArea da(form);
   DrawingArea = da.getWidget();

   // initialize graphics for the drawing area
   graphics painter1(DrawingArea);
   painter = painter1;

   // create a message bar
   messageBar msgb1(form);
   msgb = msgb1;
   MessageBar = msgb.getWidget();

   // create a command bar
   commandBar cmdb1(form);
   cmdb = cmdb1;
   CommandBar = cmdb.getWidget();

   // create separators
   separator sp1(form), sp2(form);
   Separator1 = sp1.getWidget();
   Separator2 = sp2.getWidget();

   // place the widgets on the form
   ac = 0;
   XtSetArg(al[ac], XmNtopAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
   XtSetValues(MenuBar, al, ac);
   ac = 0;
   XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
   XtSetArg(al[ac], XmNtopWidget, MenuBar); ac++;
   XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
   XtSetValues(Separator1, al, ac);
   ac = 0;
   XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
   XtSetArg(al[ac], XmNtopWidget, Separator1); ac++;
   XtSetArg(al[ac], XmNwidth, (int)(3 *SIDE / 4)); ac++;
   XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
   XtSetValues(MessageBar, al, ac);
   ac = 0;
   XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
   XtSetArg(al[ac], XmNtopWidget, Separator1); ac++;
   XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNleftAttachment, XmATTACH_WIDGET); ac++;
   XtSetArg(al[ac], XmNleftWidget, MessageBar); ac++;
   XtSetValues(CommandBar, al, ac);
   ac = 0;
   XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
   XtSetArg(al[ac], XmNtopWidget, MessageBar); ac++;
   XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
   XtSetValues(Separator2, al, ac);
   ac = 0;
   XtSetArg(al[ac], XmNtopAttachment, XmATTACH_WIDGET); ac++;
   XtSetArg(al[ac], XmNtopWidget, Separator2); ac++;
   XtSetArg(al[ac], XmNrightAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNleftAttachment, XmATTACH_FORM); ac++;
   XtSetArg(al[ac], XmNbottomAttachment, XmATTACH_FORM); ac++;
   XtSetValues(DrawingArea, al, ac);

   // add menus on the menu bar
   file = mb.addMenu("File");
   edit = mb.addMenu("Edit");
   generator = mb.addMenu("Generator");
   groups_sources = mb.addMenu("Groups & Sources"); 
   routing = mb.addMenu("Routing"); 
   simulation = mb.addMenu("Simulation");
   display = mb.addMenu("Display");

   // add items for the menus
   Open = file.addItem("Open", (XtCallbackProc)openCB);
   Save = file.addItem("Save", (XtCallbackProc)saveCB);
   Close = file.addItem("Close", (XtCallbackProc)closeCB);
   Exit = file.addItem("Exit", (XtCallbackProc)exitCB);
   AddNode = edit.addItem("Add Node", (XtCallbackProc)addNodeCB);
   RemoveNode = edit.addItem("Remove Node", (XtCallbackProc)removeNodeCB);
   MoveNode = edit.addItem("Move Node", (XtCallbackProc)moveNodeCB);
   AddLink = edit.addItem("Add Link", (XtCallbackProc)addLinkCB);
   RemoveLink = edit.addItem("Remove Link", (XtCallbackProc)removeLinkCB);
   EditLinkCap = edit.addItem("Edit Link Capacity", 
			      (XtCallbackProc)editLinkCapCB);
   Renumber = edit.addItem("Renumber Nodes", (XtCallbackProc)renumberCB);
   Refresh = edit.addItem("Refresh", (XtCallbackProc)refreshCB);
   Alpha2 = generator.addItem("Alpha", (XtCallbackProc)alphaCB);
   Beta2 = generator.addItem("Beta", (XtCallbackProc)betaCB);
   Number = generator.addItem("# 0f Nodes", (XtCallbackProc)numberCB);
   Degree = generator.addItem("Node Degree", (XtCallbackProc)degreeCB);
   DefCap = generator.addItem("Default Link Capacity", 
			      (XtCallbackProc)defCapCB);
   RandomGraph = generator.addItem("Generate Random Graph", 
                                  (XtCallbackProc)randomGraphCB);
   RandomLinks = generator.addItem("Generate Random Links", 
                                  (XtCallbackProc)randomLinksCB);
   AddDestination = groups_sources.addItem("Add Destination",
                                  (XtCallbackProc)addDestinationCB);
   RemoveDestination = groups_sources.addItem("Remove Destination",
                                  (XtCallbackProc)removeDestinationCB);
   AddRandomGroup = groups_sources.addItem("Add Random Group",
                                  (XtCallbackProc)randomGroupCB);
   RemoveGroup = groups_sources.addItem("Remove Group",
                                  (XtCallbackProc)removeGroupCB);
   AddSource = groups_sources.addMenu("Add Source");
   AddSourceVoice = AddSource.addItem("Add Voice Source", 
                    (XtCallbackProc)addSourceVoiceCB);  
   AddSourceVideo = AddSource.addItem("Add Video Source", 
                    (XtCallbackProc)addSourceVideoCB);  
   RemoveSource = groups_sources.addItem("Remove Source",
                                  (XtCallbackProc)removeSourceCB);
   AddBackgroundTraffic = groups_sources.addMenu("Add Background Traffic");
   Muxed1 = AddBackgroundTraffic.addItem("Multiplexed",
				 (XtCallbackProc)muxedBackgroundTrafficCB);
   Batch1 = AddBackgroundTraffic.addItem("Batch",
				 (XtCallbackProc)batchBackgroundTrafficCB);
   AddSymmetricBackgroundTraffic = groups_sources.addMenu(
     "Add Symmetric Background Traffic");
   Muxed2 = AddSymmetricBackgroundTraffic.addItem("Multiplexed",
	       (XtCallbackProc)muxedSymmetricBackgroundTrafficCB);
   Batch2 = AddSymmetricBackgroundTraffic.addItem("Batch",
	       (XtCallbackProc)batchSymmetricBackgroundTrafficCB);
   RemoveBackgroundTraffic = groups_sources.addItem("Remove Background Traffic",
                                  (XtCallbackProc)removeBackgroundTrafficCB);
   Cost = routing.addMenu("Peak/Average");
   Peak = Cost.addItem("Peak", (XtCallbackProc)PeakCB);
   Average = Cost.addItem("Average", (XtCallbackProc)AverageCB);
   AdmitCapacity = routing.addItem("Admission Capacity",
				   (XtCallbackProc)ADMITCB);
   DelayBound = routing.addItem("Delay Bound", 
				(XtCallbackProc)DBOUNDCB);
   DelayStep = routing.addItem("Delay Step (for KPP)", 
				(XtCallbackProc)DSTEPCB);
   Dbv = routing.addMenu("Delay Bound Violation");
   DbvYes = Dbv.addItem("Allowed", (XtCallbackProc)DBVYESCB);
   DbvNo = Dbv.addItem("Not Allowed", (XtCallbackProc)DBVNOCB);
   Opt = routing.addItem("Optimal Min. Steiner Tree Algorithm", (XtCallbackProc)OPTCB);
   Kmb = routing.addItem("KMB Min. Steiner Tree Heuristic", (XtCallbackProc)KMBCB);
   Dksld = routing.addItem("Least-Delay (Dijkstra)", (XtCallbackProc)DKSLDCB);
   Dks = routing.addItem("Least-Cost (Dijkstra)", (XtCallbackProc)DKSCB);
   Bf = routing.addItem("Least-Cost (Bellman-Ford)", (XtCallbackProc)BFCB);
   Mst = routing.addItem("Pruned MST Heuristic", (XtCallbackProc)MSTCB);
   Dimst = routing.addItem("Pruned Directed MST Heuristic", (XtCallbackProc)DIMSTCB);
//   Pim = routing.addItem("Reverse Path Multicasting (RPM)", (XtCallbackProc)PIMCB);
   Dvmrp = routing.addItem("RPM (adm. ctrl. separate)", (XtCallbackProc)DVMRPCB);
   Bfnoadm = routing.addItem("Least-Cost (Bellman-Ford, adm. ctrl. separate)", (XtCallbackProc)BFNOADMCB);
   Waters = routing.addItem("Semiconstrained Heuristic (SC)", (XtCallbackProc)WatersCB);
   ModWaters = routing.addItem("Mod. Semiconstrained Heuristic (MSC)", 
                               (XtCallbackProc)ModWatersCB);
   Constrained = routing.addMenu("Constrained Steiner Algorithms");
   Copt = Constrained.addItem("Optimal Constrained Steiner Tree", (XtCallbackProc)COPTCB);
   Cstc = Constrained.addItem("KPP - Cost (UC San Diego)", (XtCallbackProc)CSTCCB);
   Cstcd = Constrained.addItem("KPP - Cost&Delay (UC San Diego)", (XtCallbackProc)CSTCDCB);
   Cao = Constrained.addItem("CAO Heuristic (UC Berkeley)", (XtCallbackProc)CAOCB);
   Bsma = Constrained.addItem("BSMA Heuristic (UC Santa Cruz)", (XtCallbackProc)BSMACB);
   Dcdimst = Constrained.addItem("Pruned BDB Heuristic", (XtCallbackProc)DCDIMSTCB);
   Cdks = Constrained.addItem("CDKS Heuristic", (XtCallbackProc)CDKSCB);
   RemoveTree = routing.addItem("Remove Tree", (XtCallbackProc)removeTreeCB);
   BatchSize = simulation.addItem("Batch Size", (XtCallbackProc)batchSizeCB);
   MinNumBatches = simulation.addItem("Min. Number of Batches",
                                   (XtCallbackProc)minNumBatchesCB);
   MaxNumBatches = simulation.addItem("Max. Number of Batches",
                                   (XtCallbackProc)maxNumBatchesCB);
   Run = simulation.addItem("RUN", (XtCallbackProc)runCB);
   DisplayLinkCost = display.addMenu("Display Link Costs");
   DLCYes = DisplayLinkCost.addItem("Yes",(XtCallbackProc)DLCYesCB);
   DLCNo = DisplayLinkCost.addItem("No",(XtCallbackProc)DLCNoCB);
   DisplayLinkCap = display.addMenu("Display Link Capacities");
   DLCapYes = DisplayLinkCap.addItem("Yes",(XtCallbackProc)DLCapYesCB);
   DLCapNo = DisplayLinkCap.addItem("No",(XtCallbackProc)DLCapNoCB);
   DisplayNodeName = display.addMenu("Display Node Names");
   DNNYes = DisplayNodeName.addItem("Yes",(XtCallbackProc)DNNYesCB);
   DNNNo = DisplayNodeName.addItem("No",(XtCallbackProc)DNNNoCB);
   DisplayGroup = display.addMenu("Display Group");
   DGYes = DisplayGroup.addItem("Yes", (XtCallbackProc)DGYesCB);
   DGNo = DisplayGroup.addItem("No", (XtCallbackProc)DGNoCB);
   DisplayTree = display.addMenu("Display Tree");
   DTYes = DisplayTree.addItem("Yes", (XtCallbackProc)DTYesCB);
   DTNo = DisplayTree.addItem("No", (XtCallbackProc)DTNoCB);
   DGraphDegree = display.addItem("Display Graph Degree",
                                   (XtCallbackProc)graphDegreeCB);

   // realize the window and manage the system
   XtRealizeWidget(toplevel);
   XtAppMainLoop(context);
}; // end of main


void openCB() { 
         //open a file and read its contents, must be in netsim format
         flag = OPEN;
         msgb.message("Enter file name: ");
         cmdb.message(currentFile, True);
};

void closeCB() { 
         //erase any work from the drawing area without saving
         msgb.message("");
         cmdb.message("", False);
         flag = NOTHING;
         currentFile = "";
         changeTitle();
         manager.deleteGraph();
         manager.refresh(painter);
};

void saveCB() { 
         //save the network displayed on the drawing area to file   
         flag = SAVE;
         msgb.message("Save as: ");
         cmdb.message(currentFile, True);
};

void addNodeCB() { 
         //   
         flag = ADDNODE;
         msgb.message("Click on the new node's position");
         cmdb.message("", False);
};

void removeNodeCB() {         
         //   
         flag = REMOVENODE;
         msgb.message("Click on the node's position");
         cmdb.message("", False);
};

void moveNodeCB() {
         //
         flag = MOVENODE;
         msgb.message("Click on the node's position");
         cmdb.message("", False);
};

void addLinkCB() { 
         //   
         flag = ADDLINK;
         msgb.message("Click on the first node");
         cmdb.message("", False);
};

void removeLinkCB() {         
         //   
         flag = REMOVELINK;
         msgb.message("Click on the first node");
         cmdb.message("", False);
};

void editLinkCapCB() {
         //   
         flag = EDITLINKCAP;
         msgb.message("Click on the tail node");
         cmdb.message("", False);
};

void exitCB() { 
         //leave the simulator
         msgb.message("");
         cmdb.message("", False);
         flag = NOTHING;
         exit(0); 
};

void randomGraphCB() { 
   //generate arandom graph
   cmdb.message("", False);
   msgb.message("");
   NodeListEntry *tmp = manager.randomGraphGenerator(number, alpha,
                                                     beta, degree);
   if (tmp == NULL) msgb.message("Failed: change parameters");
   manager.refresh(painter);
};

void randomLinksCB() { 
   //generate random links without changing the node positions
   cmdb.message("", False);
   msgb.message("");
   NodeListEntry *tmp = manager.randomLinkGenerator(alpha, beta, degree);
   if (tmp == NULL) msgb.message("Failed: change parameters");
   manager.refresh(painter);
};

void drawingHandler(Widget w, XtPointer client_data, XEvent *event) {

   //Handles the button press event

   int xpos, ypos;
   NodeListEntry *n1, *n2, *n;
   Node *nd1, *nd2, *nd;
   int status;

   msgb.message("");

   //get the mouse's position
   xpos = event->xbutton.x;
   ypos = event->xbutton.y;
   
   switch (flag) {
  
      case ADDNODE:
            tmpx = (xpos - 10.0) / SIDE;
            tmpy = (ypos - 15.0) / SIDE;
            nd = new Node(tmpx, tmpy);
            n = new NodeListEntry;
            n->nodePtr(nd);
            manager.addNode(n);
            manager.refresh(painter);
            flag = NOTHING;

⌨️ 快捷键说明

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