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