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