Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 18 | ONOS network topology viewer - version 1.1 |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 19 | |
| 20 | @author Simon Hunt |
| 21 | */ |
| 22 | |
| 23 | (function (onos) { |
| 24 | 'use strict'; |
| 25 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 26 | // shorter names for library APIs |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 27 | var d3u = onos.lib.d3util, |
| 28 | trace; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 29 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 30 | // configuration data |
| 31 | var config = { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 32 | useLiveData: true, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 33 | fnTrace: true, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 34 | debugOn: false, |
| 35 | debug: { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 36 | showNodeXY: true, |
| 37 | showKeyHandler: false |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 38 | }, |
| 39 | options: { |
| 40 | layering: true, |
| 41 | collisionPrevention: true, |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 42 | showBackground: true |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 43 | }, |
| 44 | backgroundUrl: 'img/us-map.png', |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 45 | webSockUrl: 'ws/topology', |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 46 | data: { |
| 47 | live: { |
| 48 | jsonUrl: 'rs/topology/graph', |
| 49 | detailPrefix: 'rs/topology/graph/', |
| 50 | detailSuffix: '' |
| 51 | }, |
| 52 | fake: { |
| 53 | jsonUrl: 'json/network2.json', |
| 54 | detailPrefix: 'json/', |
| 55 | detailSuffix: '.json' |
| 56 | } |
| 57 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 58 | labels: { |
| 59 | imgPad: 16, |
| 60 | padLR: 4, |
| 61 | padTB: 3, |
| 62 | marginLR: 3, |
| 63 | marginTB: 2, |
| 64 | port: { |
| 65 | gap: 3, |
| 66 | width: 18, |
| 67 | height: 14 |
| 68 | } |
| 69 | }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 70 | topo: { |
| 71 | linkInColor: '#66f', |
| 72 | linkInWidth: 14 |
| 73 | }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 74 | icons: { |
| 75 | w: 28, |
| 76 | h: 28, |
| 77 | xoff: -12, |
| 78 | yoff: -8 |
| 79 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 80 | iconUrl: { |
| 81 | device: 'img/device.png', |
| 82 | host: 'img/host.png', |
| 83 | pkt: 'img/pkt.png', |
| 84 | opt: 'img/opt.png' |
| 85 | }, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 86 | force: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 87 | note_for_links: 'link.type is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 88 | linkDistance: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 89 | direct: 100, |
| 90 | optical: 120, |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 91 | hostLink: 5 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 92 | }, |
| 93 | linkStrength: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 94 | direct: 1.0, |
| 95 | optical: 1.0, |
| 96 | hostLink: 1.0 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 97 | }, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 98 | note_for_nodes: 'node.class is used to differentiate', |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 99 | charge: { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 100 | device: -8000, |
| 101 | host: -300 |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 102 | }, |
| 103 | pad: 20, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 104 | translate: function() { |
| 105 | return 'translate(' + |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 106 | config.force.pad + ',' + |
| 107 | config.force.pad + ')'; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 108 | } |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 109 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 112 | // radio buttons |
| 113 | var btnSet = [ |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 114 | { text: 'All Layers', cb: showAllLayers }, |
| 115 | { text: 'Packet Only', cb: showPacketLayer }, |
| 116 | { text: 'Optical Only', cb: showOpticalLayer } |
| 117 | ]; |
| 118 | |
| 119 | // key bindings |
| 120 | var keyDispatch = { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 121 | M: testMe, // TODO: remove (testing only) |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 122 | S: injectStartupEvents, // TODO: remove (testing only) |
| 123 | space: injectTestEvent, // TODO: remove (testing only) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 124 | |
Thomas Vachuska | 65368e3 | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 125 | B: toggleBg, // TODO: do we really need this? |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 126 | L: cycleLabels, |
| 127 | P: togglePorts, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 128 | U: unpin, |
| 129 | |
| 130 | X: requestPath |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 131 | }; |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 132 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 133 | // state variables |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 134 | var network = { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 135 | view: null, // view token reference |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 136 | nodes: [], |
| 137 | links: [], |
| 138 | lookup: {} |
| 139 | }, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 140 | scenario = { |
| 141 | evDir: 'json/ev/', |
| 142 | evScenario: '/scenario.json', |
| 143 | evPrefix: '/ev_', |
| 144 | evOnos: '_onos.json', |
| 145 | evUi: '_ui.json', |
| 146 | ctx: null, |
| 147 | params: {}, |
| 148 | evNumber: 0, |
| 149 | view: null, |
| 150 | debug: false |
| 151 | }, |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 152 | webSock, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 153 | deviceLabelIndex = 0, |
| 154 | hostLabelIndex = 0, |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 155 | detailPane, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 156 | selectOrder = [], |
| 157 | selections = {}, |
| 158 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 159 | highlighted = null, |
| 160 | hovered = null, |
| 161 | viewMode = 'showAll', |
| 162 | portLabelsOn = false; |
| 163 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 164 | // D3 selections |
| 165 | var svg, |
| 166 | bgImg, |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 167 | topoG, |
| 168 | nodeG, |
| 169 | linkG, |
| 170 | node, |
| 171 | link; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 172 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 173 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 174 | // For Debugging / Development |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 175 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 176 | function note(label, msg) { |
| 177 | console.log('NOTE: ' + label + ': ' + msg); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 180 | function debug(what) { |
| 181 | return config.debugOn && config.debug[what]; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 184 | function fnTrace(msg, id) { |
| 185 | if (config.fnTrace) { |
| 186 | console.log('FN: ' + msg + ' [' + id + ']'); |
| 187 | } |
| 188 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 189 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 190 | // ============================== |
| 191 | // Key Callbacks |
| 192 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 193 | function testMe(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 194 | view.alert('test'); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 195 | detailPane.show(); |
| 196 | setTimeout(function () { |
| 197 | detailPane.hide(); |
| 198 | }, 3000); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 201 | function abortIfLive() { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 202 | if (config.useLiveData) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 203 | scenario.view.alert("Sorry, currently using live data.."); |
| 204 | return true; |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 205 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 208 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 209 | function testDebug(msg) { |
| 210 | if (scenario.debug) { |
| 211 | scenario.view.alert(msg); |
| 212 | } |
| 213 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 214 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 215 | function injectTestEvent(view) { |
| 216 | if (abortIfLive()) { return; } |
| 217 | var sc = scenario, |
| 218 | evn = ++sc.evNumber, |
| 219 | pfx = sc.evDir + sc.ctx + sc.evPrefix + evn, |
| 220 | onosUrl = pfx + sc.evOnos, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 221 | uiUrl = pfx + sc.evUi, |
| 222 | stack = [ |
| 223 | { url: onosUrl, cb: handleServerEvent }, |
| 224 | { url: uiUrl, cb: handleUiEvent } |
| 225 | ]; |
| 226 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 229 | function recurseFetchEvent(stack, evn) { |
| 230 | var v = scenario.view, |
| 231 | frame; |
| 232 | if (stack.length === 0) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 233 | v.alert('Oops!\n\nNo event #' + evn + ' found.'); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 234 | return; |
| 235 | } |
| 236 | frame = stack.shift(); |
| 237 | |
| 238 | d3.json(frame.url, function (err, data) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 239 | if (err) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 240 | if (err.status === 404) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 241 | // if we didn't find the data, try the next stack frame |
| 242 | recurseFetchEvent(stack, evn); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 243 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 244 | v.alert('non-404 error:\n\n' + frame.url + '\n\n' + err); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 245 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 246 | } else { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 247 | testDebug('loaded: ' + frame.url); |
| 248 | frame.cb(data); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 249 | } |
| 250 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 251 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 252 | } |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 253 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 254 | function handleUiEvent(data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 255 | scenario.view.alert('UI Tx: ' + data.event + '\n\n' + |
| 256 | JSON.stringify(data)); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | function injectStartupEvents(view) { |
| 260 | var last = scenario.params.lastAuto || 0; |
| 261 | if (abortIfLive()) { return; } |
| 262 | |
| 263 | while (scenario.evNumber < last) { |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 264 | injectTestEvent(view); |
| 265 | } |
| 266 | } |
| 267 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 268 | function toggleBg() { |
| 269 | var vis = bgImg.style('visibility'); |
| 270 | bgImg.style('visibility', (vis === 'hidden') ? 'visible' : 'hidden'); |
| 271 | } |
| 272 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 273 | function cycleLabels() { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 274 | deviceLabelIndex = (deviceLabelIndex === network.deviceLabelCount - 1) |
| 275 | ? 0 : deviceLabelIndex + 1; |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 276 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 277 | network.nodes.forEach(function (d) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 278 | if (d.class === 'device') { |
| 279 | updateDeviceLabel(d); |
| 280 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 281 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | function togglePorts(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 285 | view.alert('togglePorts() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | function unpin(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 289 | view.alert('unpin() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | // ============================== |
| 293 | // Radio Button Callbacks |
| 294 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 295 | function showAllLayers() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 296 | // network.node.classed('inactive', false); |
| 297 | // network.link.classed('inactive', false); |
| 298 | // d3.selectAll('svg .port').classed('inactive', false); |
| 299 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 300 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 301 | network.view.alert('showAllLayers() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | function showPacketLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 305 | showAllLayers(); |
| 306 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 307 | network.view.alert('showPacketLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | function showOpticalLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 311 | showAllLayers(); |
| 312 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 313 | network.view.alert('showOpticalLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 316 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 317 | // Private functions |
| 318 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 319 | function safeId(s) { |
| 320 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 321 | } |
| 322 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 323 | // set the size of the given element to that of the view (reduced if padded) |
| 324 | function setSize(el, view, pad) { |
| 325 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 326 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 327 | width: view.width() - padding, |
| 328 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 329 | }); |
| 330 | } |
| 331 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 332 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 333 | // ============================== |
| 334 | // Event handlers for server-pushed events |
| 335 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 336 | function logicError(msg) { |
| 337 | // TODO, report logic error to server, via websock, so it can be logged |
| 338 | network.view.alert('Logic Error:\n\n' + msg); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 339 | console.warn(msg); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 342 | var eventDispatch = { |
| 343 | addDevice: addDevice, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 344 | addLink: addLink, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 345 | addHost: addHost, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 346 | updateDevice: updateDevice, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 347 | updateLink: updateLink, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 348 | updateHost: updateHost, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 349 | removeDevice: stillToImplement, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 350 | removeLink: removeLink, |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 351 | removeHost: removeHost, |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 352 | showDetails: showDetails, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 353 | showPath: showPath |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | function addDevice(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 357 | fnTrace('addDevice', data.payload.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 358 | var device = data.payload, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 359 | nodeData = createDeviceNode(device); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 360 | network.nodes.push(nodeData); |
| 361 | network.lookup[nodeData.id] = nodeData; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 362 | updateNodes(); |
| 363 | network.force.start(); |
| 364 | } |
| 365 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 366 | function addLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 367 | fnTrace('addLink', data.payload.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 368 | var link = data.payload, |
| 369 | lnk = createLink(link); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 370 | if (lnk) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 371 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 372 | network.lookup[lnk.id] = lnk; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 373 | updateLinks(); |
| 374 | network.force.start(); |
| 375 | } |
| 376 | } |
| 377 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 378 | function addHost(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 379 | fnTrace('addHost', data.payload.id); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 380 | var host = data.payload, |
| 381 | node = createHostNode(host), |
| 382 | lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 383 | network.nodes.push(node); |
| 384 | network.lookup[host.id] = node; |
| 385 | updateNodes(); |
| 386 | |
| 387 | lnk = createHostLink(host); |
| 388 | if (lnk) { |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 389 | node.linkData = lnk; // cache ref on its host |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 390 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 391 | network.lookup[host.ingress] = lnk; |
| 392 | network.lookup[host.egress] = lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 393 | updateLinks(); |
| 394 | } |
| 395 | network.force.start(); |
| 396 | } |
| 397 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 398 | // TODO: fold updateX(...) methods into one base method; remove duplication |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 399 | function updateDevice(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 400 | fnTrace('updateDevice', data.payload.id); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 401 | var device = data.payload, |
| 402 | id = device.id, |
| 403 | nodeData = network.lookup[id]; |
| 404 | if (nodeData) { |
| 405 | $.extend(nodeData, device); |
| 406 | updateDeviceState(nodeData); |
| 407 | } else { |
| 408 | logicError('updateDevice lookup fail. ID = "' + id + '"'); |
| 409 | } |
| 410 | } |
| 411 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 412 | function updateLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 413 | fnTrace('updateLink', data.payload.id); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 414 | var link = data.payload, |
| 415 | id = link.id, |
| 416 | linkData = network.lookup[id]; |
| 417 | if (linkData) { |
| 418 | $.extend(linkData, link); |
| 419 | updateLinkState(linkData); |
| 420 | } else { |
| 421 | logicError('updateLink lookup fail. ID = "' + id + '"'); |
| 422 | } |
| 423 | } |
| 424 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 425 | function updateHost(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 426 | fnTrace('updateHost', data.payload.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 427 | var host = data.payload, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 428 | id = host.id, |
| 429 | hostData = network.lookup[id]; |
| 430 | if (hostData) { |
| 431 | $.extend(hostData, host); |
| 432 | updateHostState(hostData); |
| 433 | } else { |
| 434 | logicError('updateHost lookup fail. ID = "' + id + '"'); |
| 435 | } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 438 | // TODO: fold removeX(...) methods into base method - remove dup code |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 439 | function removeLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 440 | fnTrace('removeLink', data.payload.id); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 441 | var link = data.payload, |
| 442 | id = link.id, |
| 443 | linkData = network.lookup[id]; |
| 444 | if (linkData) { |
| 445 | removeLinkElement(linkData); |
| 446 | } else { |
| 447 | logicError('removeLink lookup fail. ID = "' + id + '"'); |
| 448 | } |
| 449 | } |
| 450 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 451 | function removeHost(data) { |
| 452 | fnTrace('removeHost', data.payload.id); |
| 453 | var host = data.payload, |
| 454 | id = host.id, |
| 455 | hostData = network.lookup[id]; |
| 456 | if (hostData) { |
| 457 | removeHostElement(hostData); |
| 458 | } else { |
| 459 | logicError('removeHost lookup fail. ID = "' + id + '"'); |
| 460 | } |
| 461 | } |
| 462 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 463 | function showDetails(data) { |
| 464 | fnTrace('showDetails', data.payload.id); |
| 465 | populateDetails(data.payload); |
| 466 | detailPane.show(); |
| 467 | } |
| 468 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 469 | function showPath(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 470 | fnTrace('showPath', data.payload.id); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 471 | var links = data.payload.links, |
| 472 | s = [ data.event + "\n" + links.length ]; |
| 473 | links.forEach(function (d, i) { |
| 474 | s.push(d); |
| 475 | }); |
| 476 | network.view.alert(s.join('\n')); |
| 477 | |
| 478 | links.forEach(function (d, i) { |
| 479 | var link = network.lookup[d]; |
| 480 | if (link) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 481 | link.el.classed('showPath', true); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 482 | } |
| 483 | }); |
| 484 | |
| 485 | // TODO: add selection-highlite lines to links |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 488 | // ............................... |
| 489 | |
| 490 | function stillToImplement(data) { |
| 491 | var p = data.payload; |
| 492 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 493 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 494 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 495 | |
| 496 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 497 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | function handleServerEvent(data) { |
| 501 | var fn = eventDispatch[data.event] || unknownEvent; |
| 502 | fn(data); |
| 503 | } |
| 504 | |
| 505 | // ============================== |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 506 | // Out-going messages... |
| 507 | |
| 508 | function getSel(idx) { |
| 509 | return selections[selectOrder[idx]]; |
| 510 | } |
| 511 | |
| 512 | // for now, just a host-to-host intent, (and implicit start-monitoring) |
| 513 | function requestPath() { |
| 514 | var payload = { |
| 515 | one: getSel(0).obj.id, |
| 516 | two: getSel(1).obj.id |
| 517 | }; |
| 518 | sendMessage('requestPath', payload); |
| 519 | } |
| 520 | |
| 521 | // request details for the selected element |
| 522 | function requestDetails() { |
| 523 | var data = getSel(0).obj, |
| 524 | payload = { |
| 525 | id: data.id, |
| 526 | class: data.class |
| 527 | }; |
| 528 | sendMessage('requestDetails', payload); |
| 529 | } |
| 530 | |
| 531 | // ============================== |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 532 | // force layout modification functions |
| 533 | |
| 534 | function translate(x, y) { |
| 535 | return 'translate(' + x + ',' + y + ')'; |
| 536 | } |
| 537 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 538 | function missMsg(what, id) { |
| 539 | return '\n[' + what + '] "' + id + '" missing '; |
| 540 | } |
| 541 | |
| 542 | function linkEndPoints(srcId, dstId) { |
| 543 | var srcNode = network.lookup[srcId], |
| 544 | dstNode = network.lookup[dstId], |
| 545 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 546 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 547 | |
| 548 | if (sMiss || dMiss) { |
| 549 | logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 550 | return null; |
| 551 | } |
| 552 | return { |
| 553 | source: srcNode, |
| 554 | target: dstNode, |
| 555 | x1: srcNode.x, |
| 556 | y1: srcNode.y, |
| 557 | x2: dstNode.x, |
| 558 | y2: dstNode.y |
| 559 | }; |
| 560 | } |
| 561 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 562 | function createHostLink(host) { |
| 563 | var src = host.id, |
| 564 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 565 | id = host.ingress, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 566 | lnk = linkEndPoints(src, dst); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 567 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 568 | if (!lnk) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 569 | return null; |
| 570 | } |
| 571 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 572 | // Synthesize link ... |
| 573 | $.extend(lnk, { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 574 | id: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 575 | class: 'link', |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 576 | type: 'hostLink', |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 577 | svgClass: 'link hostLink', |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 578 | linkWidth: 1 |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 579 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 580 | return lnk; |
| 581 | } |
| 582 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 583 | function createLink(link) { |
| 584 | var lnk = linkEndPoints(link.src, link.dst), |
| 585 | type = link.type; |
| 586 | |
| 587 | if (!lnk) { |
| 588 | return null; |
| 589 | } |
| 590 | |
| 591 | // merge in remaining data |
| 592 | $.extend(lnk, link, { |
| 593 | class: 'link', |
| 594 | svgClass: type ? 'link ' + type : 'link' |
| 595 | }); |
| 596 | return lnk; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 599 | var widthRatio = 1.4, |
| 600 | linkScale = d3.scale.linear() |
| 601 | .domain([1, 12]) |
| 602 | .range([widthRatio, 12 * widthRatio]) |
| 603 | .clamp(true); |
| 604 | |
| 605 | function updateLinkWidth (d) { |
| 606 | // TODO: watch out for .showPath/.showTraffic classes |
| 607 | d.el.transition() |
| 608 | .duration(1000) |
| 609 | .attr('stroke-width', linkScale(d.linkWidth)); |
| 610 | } |
| 611 | |
| 612 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 613 | function updateLinks() { |
| 614 | link = linkG.selectAll('.link') |
| 615 | .data(network.links, function (d) { return d.id; }); |
| 616 | |
| 617 | // operate on existing links, if necessary |
| 618 | // link .foo() .bar() ... |
| 619 | |
| 620 | // operate on entering links: |
| 621 | var entering = link.enter() |
| 622 | .append('line') |
| 623 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 624 | class: function (d) { return d.svgClass; }, |
| 625 | x1: function (d) { return d.x1; }, |
| 626 | y1: function (d) { return d.y1; }, |
| 627 | x2: function (d) { return d.x2; }, |
| 628 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 629 | stroke: config.topo.linkInColor, |
| 630 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 631 | }) |
| 632 | .transition().duration(1000) |
| 633 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 634 | 'stroke-width': function (d) { return linkScale(d.linkWidth); }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 635 | stroke: '#666' // TODO: remove explicit stroke, rather... |
| 636 | }); |
| 637 | |
| 638 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 639 | entering.each(function (d) { |
| 640 | var link = d3.select(this); |
| 641 | // provide ref to element selection from backing data.... |
| 642 | d.el = link; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 643 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 644 | // TODO: add src/dst port labels etc. |
| 645 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 646 | |
| 647 | // operate on both existing and new links, if necessary |
| 648 | //link .foo() .bar() ... |
| 649 | |
| 650 | // operate on exiting links: |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 651 | link.exit() |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 652 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 653 | 'stroke-dasharray': '3, 3' |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 654 | }) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 655 | .style('opacity', 0.4) |
| 656 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 657 | .duration(1500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 658 | .attr({ |
| 659 | 'stroke-dasharray': '3, 12' |
| 660 | }) |
| 661 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 662 | .duration(500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 663 | .style('opacity', 0.0) |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 664 | .remove(); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | function createDeviceNode(device) { |
| 668 | // start with the object as is |
| 669 | var node = device, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 670 | type = device.type, |
| 671 | svgCls = type ? 'node device ' + type : 'node device'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 672 | |
| 673 | // Augment as needed... |
| 674 | node.class = 'device'; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 675 | node.svgClass = device.online ? svgCls + ' online' : svgCls; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 676 | positionNode(node); |
| 677 | |
| 678 | // cache label array length |
| 679 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 680 | return node; |
| 681 | } |
| 682 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 683 | function createHostNode(host) { |
| 684 | // start with the object as is |
| 685 | var node = host; |
| 686 | |
| 687 | // Augment as needed... |
| 688 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 689 | if (!node.type) { |
| 690 | // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ? |
| 691 | node.type = 'endstation'; |
| 692 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 693 | node.svgClass = 'node host'; |
| 694 | // TODO: consider placing near its switch, if [x,y] not defined |
| 695 | positionNode(node); |
| 696 | |
| 697 | // cache label array length |
| 698 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 699 | return node; |
| 700 | } |
| 701 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 702 | function positionNode(node) { |
| 703 | var meta = node.metaUi, |
| 704 | x = 0, |
| 705 | y = 0; |
| 706 | |
| 707 | if (meta) { |
| 708 | x = meta.x; |
| 709 | y = meta.y; |
| 710 | } |
| 711 | if (x && y) { |
| 712 | node.fixed = true; |
| 713 | } |
| 714 | node.x = x || network.view.width() / 2; |
| 715 | node.y = y || network.view.height() / 2; |
| 716 | } |
| 717 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 718 | function iconUrl(d) { |
| 719 | return 'img/' + d.type + '.png'; |
| 720 | } |
| 721 | |
| 722 | // returns the newly computed bounding box of the rectangle |
| 723 | function adjustRectToFitText(n) { |
| 724 | var text = n.select('text'), |
| 725 | box = text.node().getBBox(), |
| 726 | lab = config.labels; |
| 727 | |
| 728 | text.attr('text-anchor', 'middle') |
| 729 | .attr('y', '-0.8em') |
| 730 | .attr('x', lab.imgPad/2); |
| 731 | |
| 732 | // translate the bbox so that it is centered on [x,y] |
| 733 | box.x = -box.width / 2; |
| 734 | box.y = -box.height / 2; |
| 735 | |
| 736 | // add padding |
| 737 | box.x -= (lab.padLR + lab.imgPad/2); |
| 738 | box.width += lab.padLR * 2 + lab.imgPad; |
| 739 | box.y -= lab.padTB; |
| 740 | box.height += lab.padTB * 2; |
| 741 | |
| 742 | return box; |
| 743 | } |
| 744 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 745 | function mkSvgClass(d) { |
| 746 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 747 | } |
| 748 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 749 | function hostLabel(d) { |
| 750 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 751 | return d.labels[idx]; |
| 752 | } |
| 753 | function deviceLabel(d) { |
| 754 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 755 | return d.labels[idx]; |
| 756 | } |
| 757 | function niceLabel(label) { |
| 758 | return (label && label.trim()) ? label : '.'; |
| 759 | } |
| 760 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 761 | function updateDeviceLabel(d) { |
| 762 | var label = niceLabel(deviceLabel(d)), |
| 763 | node = d.el, |
| 764 | box; |
| 765 | |
| 766 | node.select('text') |
| 767 | .text(label) |
| 768 | .style('opacity', 0) |
| 769 | .transition() |
| 770 | .style('opacity', 1); |
| 771 | |
| 772 | box = adjustRectToFitText(node); |
| 773 | |
| 774 | node.select('rect') |
| 775 | .transition() |
| 776 | .attr(box); |
| 777 | |
| 778 | node.select('image') |
| 779 | .transition() |
| 780 | .attr('x', box.x + config.icons.xoff) |
| 781 | .attr('y', box.y + config.icons.yoff); |
| 782 | } |
| 783 | |
| 784 | function updateHostLabel(d) { |
| 785 | var label = hostLabel(d), |
| 786 | host = d.el; |
| 787 | |
| 788 | host.select('text').text(label); |
| 789 | } |
| 790 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 791 | function updateDeviceState(nodeData) { |
| 792 | nodeData.el.classed('online', nodeData.online); |
| 793 | updateDeviceLabel(nodeData); |
| 794 | // TODO: review what else might need to be updated |
| 795 | } |
| 796 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 797 | function updateLinkState(linkData) { |
| 798 | updateLinkWidth(linkData); |
| 799 | // TODO: review what else might need to be updated |
| 800 | // update label, if showing |
| 801 | } |
| 802 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 803 | function updateHostState(hostData) { |
| 804 | updateHostLabel(hostData); |
| 805 | // TODO: review what else might need to be updated |
| 806 | } |
| 807 | |
| 808 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 809 | function updateNodes() { |
| 810 | node = nodeG.selectAll('.node') |
| 811 | .data(network.nodes, function (d) { return d.id; }); |
| 812 | |
| 813 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 814 | // update host labels |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 815 | //node .foo() .bar() ... |
| 816 | |
| 817 | // operate on entering nodes: |
| 818 | var entering = node.enter() |
| 819 | .append('g') |
| 820 | .attr({ |
| 821 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 822 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 823 | transform: function (d) { return translate(d.x, d.y); }, |
| 824 | opacity: 0 |
| 825 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 826 | .call(network.drag) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 827 | //.on('mouseover', function (d) {}) |
| 828 | //.on('mouseover', function (d) {}) |
| 829 | .transition() |
| 830 | .attr('opacity', 1); |
| 831 | |
| 832 | // augment device nodes... |
| 833 | entering.filter('.device').each(function (d) { |
| 834 | var node = d3.select(this), |
| 835 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 836 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 837 | box; |
| 838 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 839 | // provide ref to element from backing data.... |
| 840 | d.el = node; |
| 841 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 842 | node.append('rect') |
| 843 | .attr({ |
| 844 | 'rx': 5, |
| 845 | 'ry': 5 |
| 846 | }); |
| 847 | |
| 848 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 849 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 850 | .attr('dy', '1.1em'); |
| 851 | |
| 852 | box = adjustRectToFitText(node); |
| 853 | |
| 854 | node.select('rect') |
| 855 | .attr(box); |
| 856 | |
| 857 | if (icon) { |
| 858 | var cfg = config.icons; |
| 859 | node.append('svg:image') |
| 860 | .attr({ |
| 861 | x: box.x + config.icons.xoff, |
| 862 | y: box.y + config.icons.yoff, |
| 863 | width: cfg.w, |
| 864 | height: cfg.h, |
| 865 | 'xlink:href': icon |
| 866 | }); |
| 867 | } |
| 868 | |
| 869 | // debug function to show the modelled x,y coordinates of nodes... |
| 870 | if (debug('showNodeXY')) { |
| 871 | node.select('rect').attr('fill-opacity', 0.5); |
| 872 | node.append('circle') |
| 873 | .attr({ |
| 874 | class: 'debug', |
| 875 | cx: 0, |
| 876 | cy: 0, |
| 877 | r: '3px' |
| 878 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 879 | } |
| 880 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 881 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 882 | // augment host nodes... |
| 883 | entering.filter('.host').each(function (d) { |
| 884 | var node = d3.select(this), |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 885 | box; |
| 886 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 887 | // provide ref to element from backing data.... |
| 888 | d.el = node; |
| 889 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 890 | node.append('circle') |
| 891 | .attr('r', 8); // TODO: define host circle radius |
| 892 | |
| 893 | // TODO: are we attaching labels to hosts? |
| 894 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 895 | .text(hostLabel) |
| 896 | .attr('dy', '1.3em') |
| 897 | .attr('text-anchor', 'middle'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 898 | |
| 899 | // debug function to show the modelled x,y coordinates of nodes... |
| 900 | if (debug('showNodeXY')) { |
| 901 | node.select('circle').attr('fill-opacity', 0.5); |
| 902 | node.append('circle') |
| 903 | .attr({ |
| 904 | class: 'debug', |
| 905 | cx: 0, |
| 906 | cy: 0, |
| 907 | r: '3px' |
| 908 | }); |
| 909 | } |
| 910 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 911 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 912 | // operate on both existing and new nodes, if necessary |
| 913 | //node .foo() .bar() ... |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 914 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 915 | // operate on exiting nodes: |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 916 | // Note that the node is removed after 2 seconds. |
| 917 | // Sub element animations should be shorter than 2 seconds. |
| 918 | var exiting = node.exit() |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 919 | .transition() |
| 920 | .duration(2000) |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 921 | .style('opacity', 0) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 922 | .remove(); |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 923 | |
| 924 | // host node exits.... |
| 925 | exiting.filter('.host').each(function (d) { |
| 926 | var node = d3.select(this); |
| 927 | |
| 928 | node.select('text') |
| 929 | .style('opacity', 0.5) |
| 930 | .transition() |
| 931 | .duration(1000) |
| 932 | .style('opacity', 0); |
| 933 | // note, leave <g>.remove to remove this element |
| 934 | |
| 935 | node.select('circle') |
| 936 | .style('stroke-fill', '#555') |
| 937 | .style('fill', '#888') |
| 938 | .style('opacity', 0.5) |
| 939 | .transition() |
| 940 | .duration(1500) |
| 941 | .attr('r', 0); |
| 942 | // note, leave <g>.remove to remove this element |
| 943 | |
| 944 | }); |
| 945 | |
| 946 | // TODO: device node exits |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 949 | function find(id, array) { |
| 950 | for (var idx = 0, n = array.length; idx < n; idx++) { |
| 951 | if (array[idx].id === id) { |
| 952 | return idx; |
| 953 | } |
| 954 | } |
| 955 | return -1; |
| 956 | } |
| 957 | |
| 958 | function removeLinkElement(linkData) { |
| 959 | // remove from lookup cache |
| 960 | delete network.lookup[linkData.id]; |
| 961 | // remove from links array |
| 962 | var idx = find(linkData.id, network.links); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 963 | network.links.splice(idx, 1); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 964 | // remove from SVG |
| 965 | updateLinks(); |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 966 | network.force.resume(); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 967 | } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 968 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 969 | function removeHostElement(hostData) { |
| 970 | // first, remove associated hostLink... |
| 971 | removeLinkElement(hostData.linkData); |
| 972 | |
| 973 | // remove from lookup cache |
| 974 | delete network.lookup[hostData.id]; |
| 975 | // remove from nodes array |
| 976 | var idx = find(hostData.id, network.nodes); |
| 977 | network.nodes.splice(idx, 1); |
| 978 | // remove from SVG |
| 979 | updateNodes(); |
| 980 | network.force.resume(); |
| 981 | } |
| 982 | |
| 983 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 984 | function tick() { |
| 985 | node.attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 986 | transform: function (d) { return translate(d.x, d.y); } |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 987 | }); |
| 988 | |
| 989 | link.attr({ |
| 990 | x1: function (d) { return d.source.x; }, |
| 991 | y1: function (d) { return d.source.y; }, |
| 992 | x2: function (d) { return d.target.x; }, |
| 993 | y2: function (d) { return d.target.y; } |
| 994 | }); |
| 995 | } |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 996 | |
| 997 | // ============================== |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 998 | // Web-Socket for live data |
| 999 | |
| 1000 | function webSockUrl() { |
| 1001 | return document.location.toString() |
| 1002 | .replace(/\#.*/, '') |
| 1003 | .replace('http://', 'ws://') |
| 1004 | .replace('https://', 'wss://') |
| 1005 | .replace('index2.html', config.webSockUrl); |
| 1006 | } |
| 1007 | |
| 1008 | webSock = { |
| 1009 | ws : null, |
| 1010 | |
| 1011 | connect : function() { |
| 1012 | webSock.ws = new WebSocket(webSockUrl()); |
| 1013 | |
| 1014 | webSock.ws.onopen = function() { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1015 | }; |
| 1016 | |
| 1017 | webSock.ws.onmessage = function(m) { |
| 1018 | if (m.data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1019 | wsTraceRx(m.data); |
Thomas Vachuska | d472c6e | 2014-11-07 19:11:05 -0800 | [diff] [blame] | 1020 | handleServerEvent(JSON.parse(m.data)); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1021 | } |
| 1022 | }; |
| 1023 | |
| 1024 | webSock.ws.onclose = function(m) { |
| 1025 | webSock.ws = null; |
| 1026 | }; |
| 1027 | }, |
| 1028 | |
| 1029 | send : function(text) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1030 | if (text != null) { |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1031 | webSock._send(text); |
| 1032 | } |
| 1033 | }, |
| 1034 | |
| 1035 | _send : function(message) { |
| 1036 | if (webSock.ws) { |
| 1037 | webSock.ws.send(message); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1038 | } else { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1039 | network.view.alert('no web socket open\n\n' + message); |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | }; |
| 1044 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1045 | var sid = 0; |
| 1046 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1047 | // TODO: use cache of pending messages (key = sid) to reconcile responses |
| 1048 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1049 | function sendMessage(evType, payload) { |
| 1050 | var toSend = { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1051 | event: evType, |
| 1052 | sid: ++sid, |
| 1053 | payload: payload |
| 1054 | }, |
| 1055 | asText = JSON.stringify(toSend); |
| 1056 | wsTraceTx(asText); |
| 1057 | webSock.send(asText); |
| 1058 | } |
| 1059 | |
| 1060 | function wsTraceTx(msg) { |
| 1061 | wsTrace('tx', msg); |
| 1062 | } |
| 1063 | function wsTraceRx(msg) { |
| 1064 | wsTrace('rx', msg); |
| 1065 | } |
| 1066 | function wsTrace(rxtx, msg) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1067 | console.log('[' + rxtx + '] ' + msg); |
| 1068 | // TODO: integrate with trace view |
| 1069 | //if (trace) { |
| 1070 | // trace.output(rxtx, msg); |
| 1071 | //} |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | |
| 1075 | // ============================== |
| 1076 | // Selection stuff |
| 1077 | |
| 1078 | function selectObject(obj, el) { |
| 1079 | var n, |
| 1080 | meta = d3.event.sourceEvent.metaKey; |
| 1081 | |
| 1082 | if (el) { |
| 1083 | n = d3.select(el); |
| 1084 | } else { |
| 1085 | node.each(function(d) { |
| 1086 | if (d == obj) { |
| 1087 | n = d3.select(el = this); |
| 1088 | } |
| 1089 | }); |
| 1090 | } |
| 1091 | if (!n) return; |
| 1092 | |
| 1093 | if (meta && n.classed('selected')) { |
| 1094 | deselectObject(obj.id); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1095 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | if (!meta) { |
| 1100 | deselectAll(); |
| 1101 | } |
| 1102 | |
Simon Hunt | 5f36d34 | 2014-11-08 21:33:14 -0800 | [diff] [blame] | 1103 | selections[obj.id] = { obj: obj, el : el}; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1104 | selectOrder.push(obj.id); |
| 1105 | |
| 1106 | n.classed('selected', true); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1107 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | function deselectObject(id) { |
| 1111 | var obj = selections[id]; |
| 1112 | if (obj) { |
| 1113 | d3.select(obj.el).classed('selected', false); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1114 | delete selections[id]; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1115 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1116 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | function deselectAll() { |
| 1120 | // deselect all nodes in the network... |
| 1121 | node.classed('selected', false); |
| 1122 | selections = {}; |
| 1123 | selectOrder = []; |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1124 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1127 | // FIXME: this click handler does not get unloaded when the view does |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1128 | $('#view').on('click', function(e) { |
| 1129 | if (!$(e.target).closest('.node').length) { |
| 1130 | if (!e.metaKey) { |
| 1131 | deselectAll(); |
| 1132 | } |
| 1133 | } |
| 1134 | }); |
| 1135 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1136 | // update the state of the detail pane, based on current selections |
| 1137 | function updateDetailPane() { |
| 1138 | var nSel = selectOrder.length; |
| 1139 | if (!nSel) { |
| 1140 | detailPane.hide(); |
| 1141 | } else if (nSel === 1) { |
| 1142 | singleSelect(); |
| 1143 | } else { |
| 1144 | multiSelect(); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | function singleSelect() { |
| 1149 | requestDetails(); |
| 1150 | // NOTE: detail pane will be shown from showDetails event. |
| 1151 | } |
| 1152 | |
| 1153 | function multiSelect() { |
| 1154 | // TODO: use detail pane for multi-select view. |
| 1155 | //detailPane.show(); |
| 1156 | } |
| 1157 | |
| 1158 | function populateDetails(data) { |
| 1159 | detailPane.empty(); |
| 1160 | |
| 1161 | var title = detailPane.append("h2"), |
| 1162 | table = detailPane.append("table"), |
| 1163 | tbody = table.append("tbody"); |
| 1164 | |
| 1165 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1166 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1167 | |
| 1168 | data.propOrder.forEach(function(p) { |
| 1169 | if (p === '-') { |
| 1170 | addSep(tbody); |
| 1171 | } else { |
| 1172 | addProp(tbody, p, data.props[p]); |
| 1173 | } |
| 1174 | }); |
| 1175 | |
| 1176 | function addSep(tbody) { |
| 1177 | var tr = tbody.append('tr'); |
| 1178 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1179 | } |
| 1180 | |
| 1181 | function addProp(tbody, label, value) { |
| 1182 | var tr = tbody.append('tr'); |
| 1183 | |
| 1184 | tr.append('td') |
| 1185 | .attr('class', 'label') |
| 1186 | .text(label + ' :'); |
| 1187 | |
| 1188 | tr.append('td') |
| 1189 | .attr('class', 'value') |
| 1190 | .text(value); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | // ============================== |
| 1195 | // Test harness code |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1196 | |
| 1197 | function prepareScenario(view, ctx, dbg) { |
| 1198 | var sc = scenario, |
| 1199 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 1200 | |
| 1201 | if (!ctx) { |
| 1202 | view.alert("No scenario specified (null ctx)"); |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | sc.view = view; |
| 1207 | sc.ctx = ctx; |
| 1208 | sc.debug = dbg; |
| 1209 | sc.evNumber = 0; |
| 1210 | |
| 1211 | d3.json(urlSc, function(err, data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1212 | var p = data && data.params || {}, |
| 1213 | desc = data && data.description || null, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1214 | intro = data && data.title; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1215 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1216 | if (err) { |
| 1217 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 1218 | } else { |
| 1219 | sc.params = p; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1220 | if (desc) { |
| 1221 | intro += '\n\n ' + desc.join('\n '); |
| 1222 | } |
| 1223 | view.alert(intro); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1224 | } |
| 1225 | }); |
| 1226 | |
| 1227 | } |
| 1228 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1229 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1230 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1231 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1232 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1233 | var w = view.width(), |
| 1234 | h = view.height(), |
| 1235 | idBg = view.uid('bg'), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1236 | showBg = config.options.showBackground ? 'visible' : 'hidden', |
| 1237 | fcfg = config.force, |
| 1238 | fpad = fcfg.pad, |
| 1239 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1240 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1241 | // TODO: set trace api |
| 1242 | //trace = onos.exported.webSockTrace; |
| 1243 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1244 | // NOTE: view.$div is a D3 selection of the view's div |
| 1245 | svg = view.$div.append('svg'); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1246 | setSize(svg, view); |
| 1247 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1248 | // add blue glow filter to svg layer |
| 1249 | d3u.appendGlow(svg); |
| 1250 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1251 | // load the background image |
| 1252 | bgImg = svg.append('svg:image') |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1253 | .attr({ |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1254 | id: idBg, |
| 1255 | width: w, |
| 1256 | height: h, |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1257 | 'xlink:href': config.backgroundUrl |
| 1258 | }) |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1259 | .style({ |
| 1260 | visibility: showBg |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1261 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1262 | |
| 1263 | // group for the topology |
| 1264 | topoG = svg.append('g') |
| 1265 | .attr('transform', fcfg.translate()); |
| 1266 | |
| 1267 | // subgroups for links and nodes |
| 1268 | linkG = topoG.append('g').attr('id', 'links'); |
| 1269 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 1270 | |
| 1271 | // selection of nodes and links |
| 1272 | link = linkG.selectAll('.link'); |
| 1273 | node = nodeG.selectAll('.node'); |
| 1274 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1275 | function chrg(d) { |
| 1276 | return fcfg.charge[d.class] || -12000; |
| 1277 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1278 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1279 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1280 | } |
| 1281 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1282 | // 0.0 - 1.0 |
| 1283 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1284 | } |
| 1285 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1286 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1287 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1291 | // once we've finished moving, pin the node in position |
| 1292 | d.fixed = true; |
| 1293 | d3.select(self).classed('fixed', true); |
| 1294 | if (config.useLiveData) { |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1295 | sendUpdateMeta(d); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1299 | function sendUpdateMeta(d) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1300 | sendMessage('updateMeta', { |
| 1301 | id: d.id, |
| 1302 | 'class': d.class, |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1303 | 'memento': { |
| 1304 | x: Math.floor(d.x), |
| 1305 | y: Math.floor(d.y) |
| 1306 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1307 | }); |
| 1308 | } |
| 1309 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1310 | // set up the force layout |
| 1311 | network.force = d3.layout.force() |
| 1312 | .size(forceDim) |
| 1313 | .nodes(network.nodes) |
| 1314 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1315 | .gravity(0.4) |
| 1316 | .friction(0.7) |
| 1317 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1318 | .linkDistance(ldist) |
| 1319 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1320 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1321 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1322 | network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd); |
| 1323 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1324 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1325 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1326 | // resize, in case the window was resized while we were not loaded |
| 1327 | resize(view, ctx, flags); |
| 1328 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1329 | // cache the view token, so network topo functions can access it |
| 1330 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1331 | config.useLiveData = !flags.local; |
| 1332 | |
| 1333 | if (!config.useLiveData) { |
| 1334 | prepareScenario(view, ctx, flags.debug); |
| 1335 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1336 | |
| 1337 | // set our radio buttons and key bindings |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1338 | view.setRadio(btnSet); |
| 1339 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1340 | |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 1341 | if (config.useLiveData) { |
| 1342 | webSock.connect(); |
| 1343 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1346 | function resize(view, ctx, flags) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1347 | setSize(svg, view); |
| 1348 | setSize(bgImg, view); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1349 | |
| 1350 | // TODO: hook to recompute layout, perhaps? work with zoom/pan code |
| 1351 | // adjust force layout size |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | // ============================== |
| 1356 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1357 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 1358 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1359 | preload: preload, |
| 1360 | load: load, |
| 1361 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1362 | }); |
| 1363 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1364 | detailPane = onos.ui.addFloatingPanel('topo-detail'); |
| 1365 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1366 | }(ONOS)); |