tcp-state-diagram.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <!doctype html>
  2. <meta charset="utf-8">
  3. <title>Dagre D3 Demo: TCP State Diagram</title>
  4. <link rel="stylesheet" href="demo.css">
  5. <script src="../../node_modules/graphlibrary/dist/graphlib.js"></script>
  6. <script src="../../node_modules/d3/build/d3.js"></script>
  7. <script src="../dagre-d3.js"></script>
  8. <style id="css">
  9. body {
  10. font: 300 14px 'Helvetica Neue', Helvetica;
  11. }
  12. .node rect {
  13. stroke: #333;
  14. fill: #fff;
  15. }
  16. .edgePath path {
  17. stroke: #333;
  18. fill: #333;
  19. stroke-width: 1.5px;
  20. }
  21. </style>
  22. <h1>Dagre D3 Demo: TCP State Diagram</h1>
  23. <svg width=960 height=600><g/></svg>
  24. <section>
  25. <p>A sample rendering of a TCP state diagram
  26. (<a href="http://www.rfc-editor.org/rfc/rfc793.txt">RFC 793</a>). This example
  27. shows how to set custom styles in the input graph and how to set a custom
  28. initial zoom.
  29. </section>
  30. <script id="js">
  31. // Create a new directed graph
  32. var g = new graphlib.Graph().setGraph({});
  33. // States and transitions from RFC 793
  34. var states = [ "CLOSED", "LISTEN", "SYN RCVD", "SYN SENT",
  35. "ESTAB", "FINWAIT-1", "CLOSE WAIT", "FINWAIT-2",
  36. "CLOSING", "LAST-ACK", "TIME WAIT" ];
  37. // Automatically label each of the nodes
  38. states.forEach(function(state) { g.setNode(state, { label: state }); });
  39. // Set up the edges
  40. g.setEdge("CLOSED", "LISTEN", { label: "open" });
  41. g.setEdge("LISTEN", "SYN RCVD", { label: "rcv SYN" });
  42. g.setEdge("LISTEN", "SYN SENT", { label: "send" });
  43. g.setEdge("LISTEN", "CLOSED", { label: "close" });
  44. g.setEdge("SYN RCVD", "FINWAIT-1", { label: "close" });
  45. g.setEdge("SYN RCVD", "ESTAB", { label: "rcv ACK of SYN" });
  46. g.setEdge("SYN SENT", "SYN RCVD", { label: "rcv SYN" });
  47. g.setEdge("SYN SENT", "ESTAB", { label: "rcv SYN, ACK" });
  48. g.setEdge("SYN SENT", "CLOSED", { label: "close" });
  49. g.setEdge("ESTAB", "FINWAIT-1", { label: "close" });
  50. g.setEdge("ESTAB", "CLOSE WAIT", { label: "rcv FIN" });
  51. g.setEdge("FINWAIT-1", "FINWAIT-2", { label: "rcv ACK of FIN" });
  52. g.setEdge("FINWAIT-1", "CLOSING", { label: "rcv FIN" });
  53. g.setEdge("CLOSE WAIT", "LAST-ACK", { label: "close" });
  54. g.setEdge("FINWAIT-2", "TIME WAIT", { label: "rcv FIN" });
  55. g.setEdge("CLOSING", "TIME WAIT", { label: "rcv ACK of FIN" });
  56. g.setEdge("LAST-ACK", "CLOSED", { label: "rcv ACK of FIN" });
  57. g.setEdge("TIME WAIT", "CLOSED", { label: "timeout=2MSL" });
  58. // Set some general styles
  59. g.nodes().forEach(function(v) {
  60. var node = g.node(v);
  61. node.rx = node.ry = 5;
  62. });
  63. // Add some custom colors based on state
  64. g.node('CLOSED').style = "fill: #f77";
  65. g.node('ESTAB').style = "fill: #7f7";
  66. var svg = d3.select("svg"),
  67. inner = svg.select("g");
  68. // Set up zoom support
  69. var zoom = d3.zoom().on("zoom", function() {
  70. inner.attr("transform", d3.event.transform);
  71. });
  72. svg.call(zoom);
  73. // Create the renderer
  74. var render = new dagreD3.render();
  75. // Run the renderer. This is what draws the final graph.
  76. render(inner, g);
  77. // Center the graph
  78. var initialScale = 0.75;
  79. svg.call(zoom.transform, d3.zoomIdentity.translate((svg.attr("width") - g.graph().width * initialScale) / 2, 20).scale(initialScale));
  80. svg.attr('height', g.graph().height * initialScale + 40);
  81. </script>
  82. <script src="demo.js"></script>