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