Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 1 | /*global d3, document∆*/ |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 2 | |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 3 | d3.selection.prototype.moveToFront = function() { |
| 4 | return this.each(function(){ |
| 5 | this.parentNode.appendChild(this); |
| 6 | }); |
| 7 | }; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 8 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 9 | var line = d3.svg.line() |
| 10 | .x(function(d) { |
| 11 | return d.x; |
| 12 | }) |
| 13 | .y(function(d) { |
| 14 | return d.y; |
| 15 | }); |
| 16 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 17 | var model; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 18 | var svg, selectedFlowsView; |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 19 | var updateTopology; |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 20 | var pendingLinks = {}; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 21 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 22 | var colors = [ |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 23 | 'color1', |
| 24 | 'color2', |
| 25 | 'color3', |
| 26 | 'color4', |
| 27 | 'color5', |
| 28 | 'color6', |
| 29 | 'color7', |
| 30 | 'color8', |
| 31 | 'color9', |
| 32 | 'color10', |
| 33 | 'color11', |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 34 | 'color12' |
| 35 | ]; |
Paul Greyson | 01a5dff | 2013-03-19 15:50:14 -0700 | [diff] [blame] | 36 | colors.reverse(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 37 | |
| 38 | var controllerColorMap = {}; |
| 39 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 40 | function setPending(selection) { |
| 41 | selection.classed('pending', false); |
| 42 | setTimeout(function () { |
| 43 | selection.classed('pending', true); |
| 44 | }) |
| 45 | } |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 46 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 47 | function createTopologyView() { |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 48 | |
| 49 | window.addEventListener('resize', function () { |
| 50 | // this is too slow. instead detect first resize event and hide the paths that have explicit matrix applied |
| 51 | // either that or is it possible to position the paths so they get the automatic transform as well? |
| 52 | // updateTopology(svg, model); |
| 53 | }); |
| 54 | |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 55 | var svg = d3.select('#svg-container').append('svg:svg'); |
| 56 | |
| 57 | svg.append("svg:defs").append("svg:marker") |
| 58 | .attr("id", "arrow") |
| 59 | .attr("viewBox", "0 -5 10 10") |
| 60 | .attr("refX", -1) |
| 61 | .attr("markerWidth", 5) |
| 62 | .attr("markerHeight", 5) |
| 63 | .attr("orient", "auto") |
| 64 | .append("svg:path") |
Paul Greyson | 45303ac | 2013-03-23 16:44:01 -0700 | [diff] [blame] | 65 | .attr("d", "M0,-3L10,0L0,3"); |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 66 | |
| 67 | return svg.append('svg:svg').attr('id', 'viewBox').attr('viewBox', '0 0 1000 1000').attr('preserveAspectRatio', 'none'). |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 68 | attr('id', 'viewbox').append('svg:g').attr('transform', 'translate(500 500)'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 71 | var selectedFlowsData = [ |
| 72 | {selected: false, flow: null}, |
| 73 | {selected: false, flow: null}, |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 74 | {selected: false, flow: null} |
| 75 | ]; |
| 76 | |
| 77 | function drawFlows() { |
| 78 | // DRAW THE FLOWS |
| 79 | var flows = d3.select('svg').selectAll('.flow').data(selectedFlowsData, function (d) { |
| 80 | return d.flow ? d.flow.flowId.value : null; |
| 81 | }); |
| 82 | |
| 83 | flows.enter().append("svg:path") |
| 84 | .attr('class', 'flow') |
| 85 | .attr('d', function (d) { |
| 86 | if (!d.flow) { |
| 87 | return; |
| 88 | } |
| 89 | var pts = []; |
| 90 | d.flow.dataPath.flowEntries.forEach(function (flowEntry) { |
| 91 | var s = d3.select(document.getElementById(flowEntry.dpid.value)); |
| 92 | var pt = document.querySelector('svg').createSVGPoint(); |
| 93 | pt.x = s.attr('x'); |
| 94 | pt.y = s.attr('y'); |
| 95 | pt = pt.matrixTransform(s[0][0].getCTM()); |
| 96 | pts.push(pt); |
| 97 | }); |
| 98 | return line(pts); |
| 99 | }) |
Paul Greyson | acb5941 | 2013-03-25 23:48:06 -0700 | [diff] [blame] | 100 | .attr('stroke-dasharray', '3, 10') |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 101 | .append('svg:animate') |
| 102 | .attr('attributeName', 'stroke-dashoffset') |
| 103 | .attr('attributeType', 'xml') |
| 104 | .attr('from', '500') |
| 105 | .attr('to', '-500') |
| 106 | .attr('dur', '20s') |
| 107 | .attr('repeatCount', 'indefinite'); |
| 108 | |
| 109 | flows.style('visibility', function (d) { |
| 110 | if (d) { |
| 111 | return d.selected ? '' : 'hidden'; |
| 112 | } |
| 113 | }) |
| 114 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 115 | // "marching ants" |
| 116 | // TODO: this will only be true if there's an iperf session running |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 117 | flows.select('animate').attr('from', function (d) { |
| 118 | if (d.flow) { |
| 119 | if (d.selected) { |
| 120 | return '500'; |
| 121 | } else { |
| 122 | return '-500'; |
| 123 | } |
| 124 | } |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | function updateFlowView() { |
| 129 | selectedFlowsView.data(selectedFlowsData); |
| 130 | |
| 131 | selectedFlowsView.classed('selected', function (d) { |
| 132 | if (d.flow) { |
| 133 | return d.selected; |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | selectedFlowsView.select('.flowId') |
| 138 | .text(function (d) { |
| 139 | if (d.flow) { |
| 140 | return d.flow.flowId.value; |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | selectedFlowsView.select('.srcDPID') |
| 145 | .text(function (d) { |
| 146 | if (d.flow) { |
| 147 | return d.flow.dataPath.srcPort.dpid.value; |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | selectedFlowsView.select('.dstDPID') |
| 152 | .text(function (d) { |
| 153 | if (d.flow) { |
| 154 | return d.flow.dataPath.dstPort.dpid.value; |
| 155 | } |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | function createFlowView() { |
| 160 | function rowEnter(d, i) { |
| 161 | var row = d3.select(this); |
| 162 | |
| 163 | row.on('click', function () { |
| 164 | selectedFlowsData[i].selected = !selectedFlowsData[i].selected; |
| 165 | updateFlowView(); |
| 166 | drawFlows(); |
| 167 | }); |
| 168 | |
| 169 | row.append('div') |
| 170 | .classed('flowIndex', true) |
| 171 | .text(function () { |
| 172 | return i+1; |
| 173 | }); |
| 174 | |
| 175 | row.append('div') |
| 176 | .classed('flowId', true); |
| 177 | |
| 178 | row.append('div') |
| 179 | .classed('srcDPID', true); |
| 180 | |
| 181 | row.append('div') |
| 182 | .classed('dstDPID', true); |
| 183 | |
| 184 | row.append('div') |
| 185 | .classed('iperf', true); |
| 186 | } |
| 187 | |
| 188 | var flows = d3.select('#selectedFlows') |
| 189 | .selectAll('.selectedFlow') |
| 190 | .data(selectedFlowsData) |
| 191 | .enter() |
| 192 | .append('div') |
| 193 | .classed('selectedFlow', true) |
| 194 | .each(rowEnter); |
| 195 | |
| 196 | |
| 197 | return flows; |
| 198 | } |
| 199 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 200 | function updateHeader(model) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 201 | d3.select('#lastUpdate').text(new Date()); |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 202 | d3.select('#activeSwitches').text(model.edgeSwitches.length + model.aggregationSwitches.length + model.coreSwitches.length); |
| 203 | d3.select('#activeFlows').text(model.flows.length); |
| 204 | } |
| 205 | |
| 206 | function toRadians (angle) { |
| 207 | return angle * (Math.PI / 180); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 210 | var widths = { |
| 211 | edge: 6, |
| 212 | aggregation: 12, |
| 213 | core: 18 |
| 214 | } |
| 215 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 216 | function createRingsFromModel(model) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 217 | var rings = [{ |
| 218 | radius: 3, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 219 | width: widths.edge, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 220 | switches: model.edgeSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 221 | className: 'edge', |
| 222 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 223 | }, { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 224 | radius: 2.25, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 225 | width: widths.aggregation, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 226 | switches: model.aggregationSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 227 | className: 'aggregation', |
| 228 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 229 | }, { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 230 | radius: 0.75, |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 231 | width: widths.core, |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 232 | switches: model.coreSwitches, |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 233 | className: 'core', |
| 234 | angles: [] |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 235 | }]; |
| 236 | |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 237 | |
| 238 | var aggRanges = {}; |
| 239 | |
| 240 | // arrange edge switches at equal increments |
| 241 | var k = 360 / rings[0].switches.length; |
| 242 | rings[0].switches.forEach(function (s, i) { |
| 243 | var angle = k * i; |
| 244 | |
| 245 | rings[0].angles[i] = angle; |
| 246 | |
| 247 | // record the angle for the agg switch layout |
| 248 | var dpid = s.dpid.split(':'); |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 249 | dpid[7] = '01'; // the last component of the agg switch is always '01' |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 250 | var aggdpid = dpid.join(':'); |
| 251 | var aggRange = aggRanges[aggdpid]; |
| 252 | if (!aggRange) { |
| 253 | aggRange = aggRanges[aggdpid] = {}; |
| 254 | aggRange.min = aggRange.max = angle; |
| 255 | } else { |
| 256 | aggRange.max = angle; |
| 257 | } |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 258 | }); |
| 259 | |
| 260 | // arrange aggregation switches to "fan out" to edge switches |
| 261 | k = 360 / rings[1].switches.length; |
| 262 | rings[1].switches.forEach(function (s, i) { |
| 263 | // rings[1].angles[i] = k * i; |
| 264 | var range = aggRanges[s.dpid]; |
| 265 | |
Paul Greyson | 832d220 | 2013-03-21 13:27:56 -0700 | [diff] [blame] | 266 | rings[1].angles[i] = (range.min + range.max)/2; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 267 | }); |
| 268 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 269 | // find the association between core switches and aggregation switches |
| 270 | var aggregationSwitchMap = {}; |
| 271 | model.aggregationSwitches.forEach(function (s, i) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 272 | aggregationSwitchMap[s.dpid] = i; |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 273 | }); |
| 274 | |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 275 | // put core switches next to linked aggregation switches |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 276 | k = 360 / rings[2].switches.length; |
| 277 | rings[2].switches.forEach(function (s, i) { |
Paul Greyson | 3f890b6 | 2013-03-22 17:39:36 -0700 | [diff] [blame] | 278 | // rings[2].angles[i] = k * i; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 279 | var associatedAggregationSwitches = model.configuration.association[s.dpid]; |
| 280 | // TODO: go between if there are multiple |
| 281 | var index = aggregationSwitchMap[associatedAggregationSwitches[0]]; |
| 282 | |
| 283 | rings[2].angles[i] = rings[1].angles[index]; |
Paul Greyson | de7fad5 | 2013-03-19 12:47:32 -0700 | [diff] [blame] | 284 | }); |
| 285 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 286 | // TODO: construct this form initially rather than converting. it works better because |
| 287 | // it allows binding by dpid |
| 288 | var testRings = []; |
| 289 | rings.forEach(function (ring) { |
| 290 | var testRing = []; |
| 291 | ring.switches.forEach(function (s, i) { |
| 292 | var testSwitch = { |
| 293 | dpid: s.dpid, |
| 294 | state: s.state, |
| 295 | radius: ring.radius, |
| 296 | width: ring.width, |
| 297 | className: ring.className, |
| 298 | angle: ring.angles[i], |
| 299 | controller: s.controller |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 300 | }; |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 301 | testRing.push(testSwitch); |
| 302 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 303 | |
| 304 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 305 | testRings.push(testRing); |
| 306 | }); |
Paul Greyson | 6d9ed86 | 2013-03-23 17:37:15 -0700 | [diff] [blame] | 307 | |
| 308 | |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 309 | // return rings; |
| 310 | return testRings; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 313 | function makeLinkKey(link) { |
| 314 | return link['src-switch'] + '=>' + link['dst-switch']; |
| 315 | } |
| 316 | |
| 317 | function createLinkMap(links) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 318 | var linkMap = {}; |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 319 | links.forEach(function (link) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 320 | var srcDPID = link['src-switch']; |
| 321 | var dstDPID = link['dst-switch']; |
| 322 | |
| 323 | var srcMap = linkMap[srcDPID] || {}; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 324 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 325 | srcMap[dstDPID] = link; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 326 | |
| 327 | linkMap[srcDPID] = srcMap; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 328 | }); |
| 329 | return linkMap; |
| 330 | } |
| 331 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 332 | updateTopology = function(svg, model) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 333 | |
| 334 | // DRAW THE SWITCHES |
| 335 | var rings = svg.selectAll('.ring').data(createRingsFromModel(model)); |
| 336 | |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 337 | |
| 338 | var links = []; |
| 339 | model.links.forEach(function (link) { |
| 340 | links.push(link); |
| 341 | delete pendingLinks[makeLinkKey(link)] |
| 342 | }) |
| 343 | var linkId; |
| 344 | for (linkId in pendingLinks) { |
| 345 | links.push(pendingLinks[linkId]); |
| 346 | } |
| 347 | |
| 348 | var linkMap = createLinkMap(links); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 349 | // var flowMap = createFlowMap(model); |
| 350 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 351 | function mouseOverSwitch(data) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 352 | if (data.highlighted) { |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | // only highlight valid link or flow destination by checking for class of existing highlighted circle |
| 357 | var highlighted = svg.selectAll('circle.highlight')[0]; |
| 358 | if (highlighted.length == 1) { |
| 359 | var s = d3.select(highlighted[0]); |
| 360 | // only allow links |
| 361 | // edge->edge (flow) |
| 362 | // aggregation->core |
| 363 | // core->core |
| 364 | if (data.className == 'edge' && !s.classed('edge') || |
| 365 | data.className == 'core' && !s.classed('core') && !s.classed('aggregation') || |
| 366 | data.className == 'aggregation' && !s.classed('core')) { |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | // don't highlight if there's already a link or flow |
| 371 | // var map = linkMap[data.dpid]; |
| 372 | // console.log(map); |
| 373 | // console.log(s.data()[0].dpid); |
| 374 | // console.log(map[s.data()[0].dpid]); |
| 375 | // if (map && map[s.data()[0].dpid]) { |
| 376 | // return; |
| 377 | // } |
| 378 | |
| 379 | // the second highlighted switch is the target for a link or flow |
| 380 | data.target = true; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', false); |
| 385 | var node = d3.select(document.getElementById(data.dpid)); |
| 386 | node.select('circle').classed('highlight', true).transition().duration(100).attr("r", widths.core); |
| 387 | data.highlighted = true; |
| 388 | node.moveToFront(); |
| 389 | } |
| 390 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 391 | function mouseOutSwitch(data) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 392 | if (data.mouseDown) |
| 393 | return; |
| 394 | |
| 395 | d3.select(document.getElementById(data.dpid + '-label')).classed('nolabel', true); |
| 396 | var node = d3.select(document.getElementById(data.dpid)); |
| 397 | node.select('circle').classed('highlight', false).transition().duration(100).attr("r", widths[data.className]); |
| 398 | data.highlighted = false; |
| 399 | data.target = false; |
| 400 | } |
| 401 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 402 | function mouseDownSwitch(data) { |
| 403 | mouseOverSwitch(data); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 404 | data.mouseDown = true; |
| 405 | } |
| 406 | |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 407 | function mouseUpSwitch(data) { |
| 408 | if (data.mouseDown) { |
| 409 | data.mouseDown = false; |
| 410 | d3.event.stopPropagation(); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | function doubleClickSwitch(data) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 415 | var circle = d3.select(document.getElementById(data.dpid)).select('circle'); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 416 | if (data.state == 'ACTIVE') { |
| 417 | var prompt = 'Deactivate ' + data.dpid + '?'; |
| 418 | if (confirm(prompt)) { |
| 419 | switchDown(data); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 420 | setPending(circle); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 421 | } |
| 422 | } else { |
| 423 | var prompt = 'Activate ' + data.dpid + '?'; |
| 424 | if (confirm(prompt)) { |
| 425 | switchUp(data); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 426 | setPending(circle); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 431 | function ringEnter(data, i) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 432 | if (!data.length) { |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 436 | // create the nodes |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 437 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 438 | .data(data, function (data) { |
| 439 | return data.dpid; |
| 440 | }) |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 441 | .enter().append("svg:g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 442 | .attr("id", function (data, i) { |
| 443 | return data.dpid; |
Paul Greyson | 23b0cd3 | 2013-03-18 23:45:48 -0700 | [diff] [blame] | 444 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 445 | .attr("transform", function(data, i) { |
| 446 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 447 | }); |
| 448 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 449 | // add the cirles representing the switches |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 450 | nodes.append("svg:circle") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 451 | .attr("transform", function(data, i) { |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 452 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 453 | if (data.scale) { |
| 454 | m = m.scale(data.scale); |
| 455 | } |
| 456 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
Paul Greyson | 952ccb6 | 2013-03-18 22:22:08 -0700 | [diff] [blame] | 457 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 458 | .attr("x", function (data) { |
| 459 | return -data.width / 2; |
| 460 | }) |
| 461 | .attr("y", function (data) { |
| 462 | return -data.width / 2; |
| 463 | }) |
| 464 | .attr("r", function (data) { |
| 465 | return data.width; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 466 | }); |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 467 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 468 | // setup the mouseover behaviors |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 469 | nodes.on('mouseover', mouseOverSwitch); |
| 470 | nodes.on('mouseout', mouseOutSwitch); |
| 471 | nodes.on('mouseup', mouseUpSwitch); |
| 472 | nodes.on('mousedown', mouseDownSwitch); |
| 473 | |
| 474 | // only do switch up/down for core switches |
| 475 | if (i == 2) { |
| 476 | nodes.on('dblclick', doubleClickSwitch); |
| 477 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 478 | } |
| 479 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 480 | // append switches |
| 481 | rings.enter().append("svg:g") |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 482 | .attr("class", "ring") |
| 483 | .each(ringEnter); |
Paul Greyson | f8f4317 | 2013-03-18 23:00:30 -0700 | [diff] [blame] | 484 | |
Paul Greyson | f9edc1a | 2013-03-19 13:22:06 -0700 | [diff] [blame] | 485 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 486 | function ringUpdate(data, i) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 487 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 488 | .data(data, function (data) { |
| 489 | return data.dpid; |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 490 | }); |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 491 | nodes.select('circle') |
| 492 | .each(function (data) { |
| 493 | // if there's a pending state changed and then the state changes, clear the pending class |
| 494 | var circle = d3.select(this); |
| 495 | if (data.state === 'ACTIVE' && circle.classed('inactive') || |
| 496 | data.state === 'INACTIVE' && circle.classed('active')) { |
| 497 | circle.classed('pending', false); |
| 498 | } |
| 499 | }) |
| 500 | .attr('class', function (data) { |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 501 | if (data.state === 'ACTIVE' && data.controller) { |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 502 | return data.className + ' active ' + controllerColorMap[data.controller]; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 503 | } else { |
Paul Greyson | 347fb74 | 2013-03-27 13:40:29 -0700 | [diff] [blame] | 504 | return data.className + ' inactive ' + 'colorInactive'; |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 505 | } |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 506 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // update switches |
| 510 | rings.each(ringUpdate); |
| 511 | |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 512 | |
| 513 | // Now setup the labels |
| 514 | // This is done separately because SVG draws in node order and we want the labels |
| 515 | // always on top |
| 516 | var labelRings = svg.selectAll('.labelRing').data(createRingsFromModel(model)); |
| 517 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 518 | d3.select(document.body).on('mouseup', function () { |
| 519 | function clearHighlight() { |
| 520 | svg.selectAll('circle').each(function (data) { |
| 521 | data.mouseDown = false; |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 522 | mouseOutSwitch(data); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 523 | }) |
| 524 | }; |
| 525 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 526 | function removeLink(link) { |
| 527 | var path1 = document.getElementById(link['src-switch'] + '=>' + link['dst-switch']); |
| 528 | var path2 = document.getElementById(link['dst-switch'] + '=>' + link['src-switch']); |
| 529 | |
| 530 | if (path1) { |
| 531 | setPending(d3.select(path1)); |
| 532 | } |
| 533 | if (path2) { |
| 534 | setPending(d3.select(path2)); |
| 535 | } |
| 536 | |
| 537 | linkDown(link); |
| 538 | } |
| 539 | |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 540 | |
| 541 | var highlighted = svg.selectAll('circle.highlight')[0]; |
| 542 | if (highlighted.length == 2) { |
| 543 | var s1Data = d3.select(highlighted[0]).data()[0]; |
| 544 | var s2Data = d3.select(highlighted[1]).data()[0]; |
| 545 | |
| 546 | var srcData, dstData; |
| 547 | if (s1Data.target) { |
| 548 | dstData = s1Data; |
| 549 | srcData = s2Data; |
| 550 | } else { |
| 551 | dstData = s2Data; |
| 552 | srcData = s1Data; |
| 553 | } |
| 554 | |
| 555 | if (s1Data.className == 'edge' && s2Data.className == 'edge') { |
| 556 | var prompt = 'Create flow from ' + srcData.dpid + ' to ' + dstData.dpid + '?'; |
| 557 | if (confirm(prompt)) { |
| 558 | alert('do create flow'); |
| 559 | } else { |
| 560 | alert('do not create flow'); |
| 561 | } |
| 562 | } else { |
| 563 | var map = linkMap[srcData.dpid]; |
| 564 | if (map && map[dstData.dpid]) { |
| 565 | var prompt = 'Remove link between ' + srcData.dpid + ' and ' + dstData.dpid + '?'; |
| 566 | if (confirm(prompt)) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 567 | removeLink(map[dstData.dpid]); |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 568 | } |
| 569 | } else { |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 570 | map = linkMap[dstData.dpid]; |
| 571 | if (map && map[srcData.dpid]) { |
| 572 | var prompt = 'Remove link between ' + dstData.dpid + ' and ' + srcData.dpid + '?'; |
| 573 | if (confirm(prompt)) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 574 | removeLink(map[srcData.dpid]); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 575 | } |
| 576 | } else { |
| 577 | var prompt = 'Create link between ' + srcData.dpid + ' and ' + dstData.dpid + '?'; |
| 578 | if (confirm(prompt)) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 579 | var link1 = { |
| 580 | 'src-switch': srcData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 581 | 'src-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 582 | 'dst-switch': dstData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 583 | 'dst-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 584 | pending: true |
| 585 | }; |
| 586 | pendingLinks[makeLinkKey(link1)] = link1; |
| 587 | var link2 = { |
| 588 | 'src-switch': dstData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 589 | 'src-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 590 | 'dst-switch': srcData.dpid, |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 591 | 'dst-port': 1, |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 592 | pending: true |
| 593 | }; |
| 594 | pendingLinks[makeLinkKey(link2)] = link2; |
| 595 | updateTopology(svg, model); |
| 596 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 597 | linkUp(link1); |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 598 | |
| 599 | // remove the pending link after 10s |
| 600 | setTimeout(function () { |
| 601 | delete pendingLinks[makeLinkKey(link1)]; |
| 602 | delete pendingLinks[makeLinkKey(link2)]; |
| 603 | |
| 604 | updateTopology(svg, model); |
| 605 | }, 10000); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 606 | } |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | clearHighlight(); |
| 612 | } else { |
| 613 | clearHighlight(); |
| 614 | } |
| 615 | |
| 616 | }); |
| 617 | |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 618 | function labelRingEnter(data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 619 | if (!data.length) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 620 | return; |
| 621 | } |
| 622 | |
| 623 | // create the nodes |
| 624 | var nodes = d3.select(this).selectAll("g") |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 625 | .data(data, function (data) { |
| 626 | return data.dpid; |
| 627 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 628 | .enter().append("svg:g") |
| 629 | .classed('nolabel', true) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 630 | .attr("id", function (data) { |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 631 | return data.dpid + '-label'; |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 632 | }) |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 633 | .attr("transform", function(data, i) { |
| 634 | return "rotate(" + data.angle+ ")translate(" + data.radius * 150 + ")rotate(" + (-data.angle) + ")"; |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 635 | }) |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 636 | |
| 637 | // add the text nodes which show on mouse over |
| 638 | nodes.append("svg:text") |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 639 | .text(function (data) {return data.dpid;}) |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 640 | .attr("x", function (data) { |
| 641 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 642 | if (data.className == 'edge') { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 643 | return - data.width*3 - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 644 | } else { |
Paul Greyson | 1eb2dd1 | 2013-03-23 18:22:00 -0700 | [diff] [blame] | 645 | return - data.width - 4; |
Paul Greyson | 9066ab0 | 2013-03-23 18:15:41 -0700 | [diff] [blame] | 646 | } |
| 647 | } else { |
| 648 | if (data.className == 'edge') { |
| 649 | return data.width*3 + 4; |
| 650 | } else { |
| 651 | return data.width + 4; |
| 652 | } |
| 653 | } |
| 654 | }) |
| 655 | .attr("y", function (data) { |
| 656 | var y; |
| 657 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 658 | if (data.className == 'edge') { |
| 659 | y = data.width*3/2 + 4; |
| 660 | } else { |
| 661 | y = data.width/2 + 4; |
| 662 | } |
| 663 | } else { |
| 664 | if (data.className == 'edge') { |
| 665 | y = data.width*3/2 + 4; |
| 666 | } else { |
| 667 | y = data.width/2 + 4; |
| 668 | } |
| 669 | } |
| 670 | return y - 6; |
| 671 | }) |
| 672 | .attr("text-anchor", function (data) { |
| 673 | if (data.angle <= 90 || data.angle >= 270 && data.angle <= 360) { |
| 674 | return "end"; |
| 675 | } else { |
| 676 | return "start"; |
| 677 | } |
| 678 | }) |
| 679 | .attr("transform", function(data) { |
Paul Greyson | 968d1b4 | 2013-03-23 16:58:41 -0700 | [diff] [blame] | 680 | var m = document.querySelector('#viewbox').getTransformToElement().inverse(); |
| 681 | if (data.scale) { |
| 682 | m = m.scale(data.scale); |
| 683 | } |
| 684 | return "matrix( " + m.a + " " + m.b + " " + m.c + " " + m.d + " " + m.e + " " + m.f + " )"; |
| 685 | }) |
| 686 | } |
| 687 | |
| 688 | labelRings.enter().append("svg:g") |
| 689 | .attr("class", "textRing") |
| 690 | .each(labelRingEnter); |
| 691 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 692 | // switches should not change during operation of the ui so no |
| 693 | // rings.exit() |
| 694 | |
| 695 | |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 696 | // DRAW THE LINKS |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 697 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 698 | // key on link dpids since these will come/go during demo |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 699 | var links = d3.select('svg').selectAll('.link').data(links, function (d) { |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 700 | return d['src-switch']+'->'+d['dst-switch']; |
| 701 | }); |
| 702 | |
| 703 | // add new links |
Paul Greyson | b367de2 | 2013-03-23 11:09:11 -0700 | [diff] [blame] | 704 | links.enter().append("svg:path") |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 705 | .attr("class", "link"); |
| 706 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 707 | links.attr('id', function (d) { |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 708 | return makeLinkKey(d); |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 709 | }) |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 710 | .attr("d", function (d) { |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 711 | var src = d3.select(document.getElementById(d['src-switch'])); |
| 712 | var dst = d3.select(document.getElementById(d['dst-switch'])); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 713 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 714 | var srcPt = document.querySelector('svg').createSVGPoint(); |
| 715 | srcPt.x = src.attr('x'); |
| 716 | srcPt.y = src.attr('y'); |
| 717 | srcPt = srcPt.matrixTransform(src[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 718 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 719 | var dstPt = document.querySelector('svg').createSVGPoint(); |
| 720 | dstPt.x = dst.attr('x'); |
| 721 | dstPt.y = dst.attr('y'); // tmp: make up and down links distinguishable |
| 722 | dstPt = dstPt.matrixTransform(dst[0][0].getCTM()); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 723 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 724 | var midPt = document.querySelector('svg').createSVGPoint(); |
| 725 | midPt.x = (srcPt.x + dstPt.x)/2; |
| 726 | midPt.y = (srcPt.y + dstPt.y)/2; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 727 | |
Paul Greyson | 084779b | 2013-03-27 13:55:49 -0700 | [diff] [blame] | 728 | return line([srcPt, midPt, dstPt]); |
| 729 | }) |
Paul Greyson | 40c8a59 | 2013-03-27 14:10:33 -0700 | [diff] [blame] | 730 | .attr("marker-mid", function(d) { return "url(#arrow)"; }) |
| 731 | .classed('pending', function (d) { |
| 732 | return d.pending; |
| 733 | }); |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 734 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 735 | |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 736 | // remove old links |
| 737 | links.exit().remove(); |
Paul Greyson | 644d92a | 2013-03-23 18:00:40 -0700 | [diff] [blame] | 738 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 739 | |
| 740 | drawFlows(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | function updateControllers(model) { |
| 744 | var controllers = d3.select('#controllerList').selectAll('.controller').data(model.controllers); |
Paul Greyson | 3e14216 | 2013-03-19 13:56:17 -0700 | [diff] [blame] | 745 | controllers.enter().append('div') |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 746 | .each(function (c) { |
| 747 | controllerColorMap[c] = colors.pop(); |
| 748 | d3.select(document.body).classed(controllerColorMap[c] + '-selected', true); |
| 749 | }) |
| 750 | .text(function (d) { |
| 751 | return d; |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 752 | }) |
| 753 | .append('div') |
| 754 | .attr('class', 'controllerEye'); |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 755 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 756 | controllers.attr('class', function (d) { |
Paul Greyson | eed3635 | 2013-03-23 11:19:11 -0700 | [diff] [blame] | 757 | var color = 'colorInactive'; |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 758 | if (model.activeControllers.indexOf(d) != -1) { |
| 759 | color = controllerColorMap[d]; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 760 | } |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 761 | var className = 'controller ' + color; |
| 762 | return className; |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 763 | }); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 764 | |
Paul Greyson | e262a29 | 2013-03-23 10:35:23 -0700 | [diff] [blame] | 765 | // this should never be needed |
| 766 | // controllers.exit().remove(); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 767 | |
Paul Greyson | 2913af8 | 2013-03-27 14:53:17 -0700 | [diff] [blame] | 768 | controllers.on('dblclick', function (c) { |
| 769 | if (model.activeControllers.indexOf(c) != -1) { |
| 770 | var prompt = 'Dectivate ' + c + '?'; |
| 771 | if (confirm(prompt)) { |
| 772 | controllerDown(c); |
| 773 | setPending(d3.select(this)); |
| 774 | }; |
| 775 | } else { |
| 776 | var prompt = 'Activate ' + c + '?'; |
| 777 | if (confirm(prompt)) { |
| 778 | controllerUp(c); |
| 779 | setPending(d3.select(this)); |
| 780 | }; |
| 781 | } |
| 782 | }); |
| 783 | |
| 784 | controllers.select('.controllerEye').on('click', function (c) { |
Paul Greyson | c3e21a0 | 2013-03-21 13:56:05 -0700 | [diff] [blame] | 785 | var allSelected = true; |
| 786 | for (var key in controllerColorMap) { |
| 787 | if (!d3.select(document.body).classed(controllerColorMap[key] + '-selected')) { |
| 788 | allSelected = false; |
| 789 | break; |
| 790 | } |
| 791 | } |
| 792 | if (allSelected) { |
| 793 | for (var key in controllerColorMap) { |
| 794 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', key == c) |
| 795 | } |
| 796 | } else { |
| 797 | for (var key in controllerColorMap) { |
| 798 | d3.select(document.body).classed(controllerColorMap[key] + '-selected', true) |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // var selected = d3.select(document.body).classed(controllerColorMap[c] + '-selected'); |
| 803 | // d3.select(document.body).classed(controllerColorMap[c] + '-selected', !selected); |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 804 | }); |
Paul Greyson | 8d1c636 | 2013-03-27 13:05:24 -0700 | [diff] [blame] | 805 | |
| 806 | |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 809 | function sync(svg, selectedFlowsView) { |
Paul Greyson | bcd3c77 | 2013-03-21 13:16:44 -0700 | [diff] [blame] | 810 | var d = Date.now(); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 811 | updateModel(function (newModel) { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 812 | // console.log('Update time: ' + (Date.now() - d)/1000 + 's'); |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 813 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 814 | if (!model || JSON.stringify(model) != JSON.stringify(newModel)) { |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 815 | updateControllers(newModel); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 816 | |
| 817 | // fake flows right now |
| 818 | var i; |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 819 | for (i = 0; i < newModel.flows.length && i < selectedFlowsData.length; i+=1) { |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 820 | var selected = selectedFlowsData[i] ? selectedFlowsData[i].selected : false; |
| 821 | selectedFlowsData[i].flow = newModel.flows[i]; |
| 822 | selectedFlowsData[i].selected = selected; |
| 823 | } |
| 824 | |
| 825 | updateFlowView(newModel); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 826 | updateTopology(svg, newModel); |
| 827 | } else { |
Paul Greyson | 4e6dc3a | 2013-03-27 11:37:14 -0700 | [diff] [blame] | 828 | // console.log('no change'); |
Paul Greyson | b48943b | 2013-03-19 13:27:57 -0700 | [diff] [blame] | 829 | } |
| 830 | updateHeader(newModel); |
| 831 | |
Paul Greyson | 56378ed | 2013-03-26 23:17:36 -0700 | [diff] [blame] | 832 | model = newModel; |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 833 | |
| 834 | // do it again in 1s |
| 835 | setTimeout(function () { |
Paul Greyson | a36a923 | 2013-03-22 22:41:27 -0700 | [diff] [blame] | 836 | sync(svg) |
Paul Greyson | d1a22d9 | 2013-03-19 12:15:19 -0700 | [diff] [blame] | 837 | }, 1000); |
Paul Greyson | 6f86d1e | 2013-03-18 14:40:39 -0700 | [diff] [blame] | 838 | }); |
| 839 | } |
Paul Greyson | 740bdaf | 2013-03-18 16:10:48 -0700 | [diff] [blame] | 840 | |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 841 | svg = createTopologyView(); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 842 | selectedFlowsView = createFlowView(); |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 843 | // workaround for Chrome v25 bug |
| 844 | // if executed immediately, the view box transform logic doesn't work properly |
| 845 | // fixed in Chrome v27 |
| 846 | setTimeout(function () { |
| 847 | // workaround for another Chrome v25 bug |
| 848 | // viewbox transform stuff doesn't work in combination with browser zoom |
Paul Greyson | c17278a | 2013-03-23 10:17:12 -0700 | [diff] [blame] | 849 | // also works in Chrome v27 |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 850 | d3.select('#svg-container').style('zoom', window.document.body.clientWidth/window.document.width); |
Paul Greyson | 127d7fb | 2013-03-25 23:39:20 -0700 | [diff] [blame] | 851 | sync(svg, selectedFlowsView); |
Paul Greyson | 38d8bde | 2013-03-22 22:07:35 -0700 | [diff] [blame] | 852 | }, 100); |