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