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 = {}, |
| 163 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 164 | highlighted = null, |
| 165 | hovered = null, |
| 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 | |
| 293 | function unpin(view) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 294 | view.alert('unpin() callback') |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // ============================== |
| 298 | // Radio Button Callbacks |
| 299 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 300 | function showAllLayers() { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 301 | // network.node.classed('inactive', false); |
| 302 | // network.link.classed('inactive', false); |
| 303 | // d3.selectAll('svg .port').classed('inactive', false); |
| 304 | // d3.selectAll('svg .portText').classed('inactive', false); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 305 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 306 | network.view.alert('showAllLayers() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | function showPacketLayer() { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 310 | showAllLayers(); |
| 311 | // TODO ... |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 312 | network.view.alert('showPacketLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | function showOpticalLayer() { |
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('showOpticalLayer() callback'); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 321 | // ============================== |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 322 | // Private functions |
| 323 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 324 | function safeId(s) { |
| 325 | return s.replace(/[^a-z0-9]/gi, '-'); |
| 326 | } |
| 327 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 328 | // set the size of the given element to that of the view (reduced if padded) |
| 329 | function setSize(el, view, pad) { |
| 330 | var padding = pad ? pad * 2 : 0; |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 331 | el.attr({ |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 332 | width: view.width() - padding, |
| 333 | height: view.height() - padding |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 334 | }); |
| 335 | } |
| 336 | |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 337 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 338 | // ============================== |
| 339 | // Event handlers for server-pushed events |
| 340 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 341 | function logicError(msg) { |
| 342 | // TODO, report logic error to server, via websock, so it can be logged |
| 343 | network.view.alert('Logic Error:\n\n' + msg); |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 344 | console.warn(msg); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 345 | } |
| 346 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 347 | var eventDispatch = { |
| 348 | addDevice: addDevice, |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 349 | addLink: addLink, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 350 | addHost: addHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 351 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 352 | updateDevice: updateDevice, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 353 | updateLink: updateLink, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 354 | updateHost: updateHost, |
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 | removeDevice: stillToImplement, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 357 | removeLink: removeLink, |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 358 | removeHost: removeHost, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 359 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 360 | showDetails: showDetails, |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 361 | showPath: showPath, |
| 362 | showTraffic: showTraffic |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 363 | }; |
| 364 | |
| 365 | function addDevice(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 366 | fnTrace('addDevice', data.payload.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 367 | var device = data.payload, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 368 | nodeData = createDeviceNode(device); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 369 | network.nodes.push(nodeData); |
| 370 | network.lookup[nodeData.id] = nodeData; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 371 | updateNodes(); |
| 372 | network.force.start(); |
| 373 | } |
| 374 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 375 | function addLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 376 | fnTrace('addLink', data.payload.id); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 377 | var link = data.payload, |
| 378 | lnk = createLink(link); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 379 | if (lnk) { |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 380 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 381 | network.lookup[lnk.id] = lnk; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 382 | updateLinks(); |
| 383 | network.force.start(); |
| 384 | } |
| 385 | } |
| 386 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 387 | function addHost(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 388 | fnTrace('addHost', data.payload.id); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 389 | var host = data.payload, |
| 390 | node = createHostNode(host), |
| 391 | lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 392 | network.nodes.push(node); |
| 393 | network.lookup[host.id] = node; |
| 394 | updateNodes(); |
| 395 | |
| 396 | lnk = createHostLink(host); |
| 397 | if (lnk) { |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 398 | node.linkData = lnk; // cache ref on its host |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 399 | network.links.push(lnk); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 400 | network.lookup[host.ingress] = lnk; |
| 401 | network.lookup[host.egress] = lnk; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 402 | updateLinks(); |
| 403 | } |
| 404 | network.force.start(); |
| 405 | } |
| 406 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 407 | // TODO: fold updateX(...) methods into one base method; remove duplication |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 408 | function updateDevice(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 409 | fnTrace('updateDevice', data.payload.id); |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 410 | var device = data.payload, |
| 411 | id = device.id, |
| 412 | nodeData = network.lookup[id]; |
| 413 | if (nodeData) { |
| 414 | $.extend(nodeData, device); |
| 415 | updateDeviceState(nodeData); |
| 416 | } else { |
| 417 | logicError('updateDevice lookup fail. ID = "' + id + '"'); |
| 418 | } |
| 419 | } |
| 420 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 421 | function updateLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 422 | fnTrace('updateLink', data.payload.id); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 423 | var link = data.payload, |
| 424 | id = link.id, |
| 425 | linkData = network.lookup[id]; |
| 426 | if (linkData) { |
| 427 | $.extend(linkData, link); |
| 428 | updateLinkState(linkData); |
| 429 | } else { |
| 430 | logicError('updateLink lookup fail. ID = "' + id + '"'); |
| 431 | } |
| 432 | } |
| 433 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 434 | function updateHost(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 435 | fnTrace('updateHost', data.payload.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 436 | var host = data.payload, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 437 | id = host.id, |
| 438 | hostData = network.lookup[id]; |
| 439 | if (hostData) { |
| 440 | $.extend(hostData, host); |
| 441 | updateHostState(hostData); |
| 442 | } else { |
| 443 | logicError('updateHost lookup fail. ID = "' + id + '"'); |
| 444 | } |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 445 | } |
| 446 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 447 | // TODO: fold removeX(...) methods into base method - remove dup code |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 448 | function removeLink(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 449 | fnTrace('removeLink', data.payload.id); |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 450 | var link = data.payload, |
| 451 | id = link.id, |
| 452 | linkData = network.lookup[id]; |
| 453 | if (linkData) { |
| 454 | removeLinkElement(linkData); |
| 455 | } else { |
| 456 | logicError('removeLink lookup fail. ID = "' + id + '"'); |
| 457 | } |
| 458 | } |
| 459 | |
Simon Hunt | 4403110 | 2014-11-11 13:20:36 -0800 | [diff] [blame] | 460 | function removeHost(data) { |
| 461 | fnTrace('removeHost', data.payload.id); |
| 462 | var host = data.payload, |
| 463 | id = host.id, |
| 464 | hostData = network.lookup[id]; |
| 465 | if (hostData) { |
| 466 | removeHostElement(hostData); |
| 467 | } else { |
| 468 | logicError('removeHost lookup fail. ID = "' + id + '"'); |
| 469 | } |
| 470 | } |
| 471 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 472 | function showDetails(data) { |
| 473 | fnTrace('showDetails', data.payload.id); |
| 474 | populateDetails(data.payload); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 475 | // TODO: Add single-select actions ... |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 476 | detailPane.show(); |
| 477 | } |
| 478 | |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 479 | function showPath(data) { |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 480 | fnTrace('showPath', data.payload.id); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 481 | var links = data.payload.links, |
| 482 | s = [ data.event + "\n" + links.length ]; |
| 483 | links.forEach(function (d, i) { |
| 484 | s.push(d); |
| 485 | }); |
| 486 | network.view.alert(s.join('\n')); |
| 487 | |
| 488 | links.forEach(function (d, i) { |
| 489 | var link = network.lookup[d]; |
| 490 | if (link) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 491 | link.el.classed('showPath', true); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 492 | } |
| 493 | }); |
| 494 | |
| 495 | // TODO: add selection-highlite lines to links |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 498 | function showTraffic(data) { |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 499 | fnTrace('showTraffic', data.payload.id); |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 500 | var paths = data.payload.paths; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 501 | |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 502 | // Revert any links hilighted previously. |
| 503 | network.links.forEach(function (d) { |
| 504 | d.el.classed('showPath', false); |
| 505 | }); |
| 506 | |
| 507 | // Now hilight all links in the paths payload. |
| 508 | paths.forEach(function (d) { |
| 509 | var links = d.links; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 510 | links.forEach(function (d, i) { |
| 511 | var link = network.lookup[d]; |
| 512 | if (link) { |
| 513 | link.el.classed('showPath', true); |
| 514 | } |
| 515 | }); |
| 516 | }); |
| 517 | //network.view.alert("showTraffic() -- TODO") |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 520 | // ............................... |
| 521 | |
| 522 | function stillToImplement(data) { |
| 523 | var p = data.payload; |
| 524 | note(data.event, p.id); |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 525 | network.view.alert('Not yet implemented: "' + data.event + '"'); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 526 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 527 | |
| 528 | function unknownEvent(data) { |
Simon Hunt | 50128c0 | 2014-11-08 13:36:15 -0800 | [diff] [blame] | 529 | network.view.alert('Unknown event type: "' + data.event + '"'); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | function handleServerEvent(data) { |
| 533 | var fn = eventDispatch[data.event] || unknownEvent; |
| 534 | fn(data); |
| 535 | } |
| 536 | |
| 537 | // ============================== |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 538 | // Out-going messages... |
| 539 | |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 540 | function userFeedback(msg) { |
| 541 | // for now, use the alert pane as is. Maybe different alert style in |
| 542 | // the future (centered on view; dismiss button?) |
| 543 | network.view.alert(msg); |
| 544 | } |
| 545 | |
| 546 | function nSel() { |
| 547 | return selectOrder.length; |
| 548 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 549 | function getSel(idx) { |
| 550 | return selections[selectOrder[idx]]; |
| 551 | } |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 552 | function getSelId(idx) { |
| 553 | return getSel(idx).obj.id; |
| 554 | } |
| 555 | function allSelectionsClass(cls) { |
| 556 | for (var i=0, n=nSel(); i<n; i++) { |
| 557 | if (getSel(i).obj.class !== cls) { |
| 558 | return false; |
| 559 | } |
| 560 | } |
| 561 | return true; |
| 562 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 563 | |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 564 | function requestTraffic(hoverNode) { |
| 565 | if (nSel() > 0 || hoverNode) { |
| 566 | var nodes = hoverNode ? selectOrder.concat(hoverNode.id) : selectOrder; |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 567 | sendMessage('requestTraffic', { |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 568 | ids: nodes |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 569 | }); |
| 570 | } else { |
| 571 | userFeedback('Request-Traffic requires one or\n' + |
| 572 | 'more items to be selected.'); |
| 573 | } |
| 574 | } |
| 575 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 576 | function requestPath() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 577 | if (nSel() === 2 && allSelectionsClass('host')) { |
| 578 | sendMessage('requestPath', { |
| 579 | one: getSelId(0), |
| 580 | two: getSelId(1) |
| 581 | }); |
| 582 | } else { |
| 583 | userFeedback('Request-Path requires two\n' + |
| 584 | 'hosts to be selected.'); |
| 585 | } |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 588 | function cancelTraffic(hoverNode) { |
| 589 | if (hoverNode && selectOrder.length) { |
| 590 | requestTraffic(); |
| 591 | } else { |
| 592 | // FIXME: from where do we get the intent id(s) to send to the server? |
| 593 | sendMessage('cancelTraffic', { |
| 594 | ids: ["need_the_intent_id"] |
| 595 | }); |
| 596 | } |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 599 | // request details for the selected element |
| 600 | function requestDetails() { |
| 601 | var data = getSel(0).obj, |
| 602 | payload = { |
| 603 | id: data.id, |
| 604 | class: data.class |
| 605 | }; |
| 606 | sendMessage('requestDetails', payload); |
| 607 | } |
| 608 | |
| 609 | // ============================== |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 610 | // force layout modification functions |
| 611 | |
| 612 | function translate(x, y) { |
| 613 | return 'translate(' + x + ',' + y + ')'; |
| 614 | } |
| 615 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 616 | function missMsg(what, id) { |
| 617 | return '\n[' + what + '] "' + id + '" missing '; |
| 618 | } |
| 619 | |
| 620 | function linkEndPoints(srcId, dstId) { |
| 621 | var srcNode = network.lookup[srcId], |
| 622 | dstNode = network.lookup[dstId], |
| 623 | sMiss = !srcNode ? missMsg('src', srcId) : '', |
| 624 | dMiss = !dstNode ? missMsg('dst', dstId) : ''; |
| 625 | |
| 626 | if (sMiss || dMiss) { |
| 627 | logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
| 628 | return null; |
| 629 | } |
| 630 | return { |
| 631 | source: srcNode, |
| 632 | target: dstNode, |
| 633 | x1: srcNode.x, |
| 634 | y1: srcNode.y, |
| 635 | x2: dstNode.x, |
| 636 | y2: dstNode.y |
| 637 | }; |
| 638 | } |
| 639 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 640 | function createHostLink(host) { |
| 641 | var src = host.id, |
| 642 | dst = host.cp.device, |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 643 | id = host.ingress, |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 644 | lnk = linkEndPoints(src, dst); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 645 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 646 | if (!lnk) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 647 | return null; |
| 648 | } |
| 649 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 650 | // Synthesize link ... |
| 651 | $.extend(lnk, { |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 652 | id: id, |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 653 | class: 'link', |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 654 | type: 'hostLink', |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 655 | svgClass: 'link hostLink', |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 656 | linkWidth: 1 |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 657 | }); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 658 | return lnk; |
| 659 | } |
| 660 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 661 | function createLink(link) { |
| 662 | var lnk = linkEndPoints(link.src, link.dst), |
| 663 | type = link.type; |
| 664 | |
| 665 | if (!lnk) { |
| 666 | return null; |
| 667 | } |
| 668 | |
| 669 | // merge in remaining data |
| 670 | $.extend(lnk, link, { |
| 671 | class: 'link', |
| 672 | svgClass: type ? 'link ' + type : 'link' |
| 673 | }); |
| 674 | return lnk; |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 677 | var widthRatio = 1.4, |
| 678 | linkScale = d3.scale.linear() |
| 679 | .domain([1, 12]) |
| 680 | .range([widthRatio, 12 * widthRatio]) |
| 681 | .clamp(true); |
| 682 | |
| 683 | function updateLinkWidth (d) { |
| 684 | // TODO: watch out for .showPath/.showTraffic classes |
| 685 | d.el.transition() |
| 686 | .duration(1000) |
| 687 | .attr('stroke-width', linkScale(d.linkWidth)); |
| 688 | } |
| 689 | |
| 690 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 691 | function updateLinks() { |
| 692 | link = linkG.selectAll('.link') |
| 693 | .data(network.links, function (d) { return d.id; }); |
| 694 | |
| 695 | // operate on existing links, if necessary |
| 696 | // link .foo() .bar() ... |
| 697 | |
| 698 | // operate on entering links: |
| 699 | var entering = link.enter() |
| 700 | .append('line') |
| 701 | .attr({ |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 702 | class: function (d) { return d.svgClass; }, |
| 703 | x1: function (d) { return d.x1; }, |
| 704 | y1: function (d) { return d.y1; }, |
| 705 | x2: function (d) { return d.x2; }, |
| 706 | y2: function (d) { return d.y2; }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 707 | stroke: config.topo.linkInColor, |
| 708 | 'stroke-width': config.topo.linkInWidth |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 709 | }) |
| 710 | .transition().duration(1000) |
| 711 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 712 | 'stroke-width': function (d) { return linkScale(d.linkWidth); }, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 713 | stroke: '#666' // TODO: remove explicit stroke, rather... |
| 714 | }); |
| 715 | |
| 716 | // augment links |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 717 | entering.each(function (d) { |
| 718 | var link = d3.select(this); |
| 719 | // provide ref to element selection from backing data.... |
| 720 | d.el = link; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 721 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 722 | // TODO: add src/dst port labels etc. |
| 723 | }); |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 724 | |
| 725 | // operate on both existing and new links, if necessary |
| 726 | //link .foo() .bar() ... |
| 727 | |
| 728 | // operate on exiting links: |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 729 | link.exit() |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 730 | .attr({ |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 731 | 'stroke-dasharray': '3, 3' |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 732 | }) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 733 | .style('opacity', 0.4) |
| 734 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 735 | .duration(1500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 736 | .attr({ |
| 737 | 'stroke-dasharray': '3, 12' |
| 738 | }) |
| 739 | .transition() |
Simon Hunt | ea80eb4 | 2014-11-11 13:46:57 -0800 | [diff] [blame] | 740 | .duration(500) |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 741 | .style('opacity', 0.0) |
Thomas Vachuska | 4830d39 | 2014-11-09 17:09:56 -0800 | [diff] [blame] | 742 | .remove(); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | function createDeviceNode(device) { |
| 746 | // start with the object as is |
| 747 | var node = device, |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 748 | type = device.type, |
| 749 | svgCls = type ? 'node device ' + type : 'node device'; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 750 | |
| 751 | // Augment as needed... |
| 752 | node.class = 'device'; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 753 | node.svgClass = device.online ? svgCls + ' online' : svgCls; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 754 | positionNode(node); |
| 755 | |
| 756 | // cache label array length |
| 757 | network.deviceLabelCount = device.labels.length; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 758 | return node; |
| 759 | } |
| 760 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 761 | function createHostNode(host) { |
| 762 | // start with the object as is |
| 763 | var node = host; |
| 764 | |
| 765 | // Augment as needed... |
| 766 | node.class = 'host'; |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 767 | if (!node.type) { |
| 768 | // TODO: perhaps type would be: {phone, tablet, laptop, endstation} ? |
| 769 | node.type = 'endstation'; |
| 770 | } |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 771 | node.svgClass = 'node host'; |
| 772 | // TODO: consider placing near its switch, if [x,y] not defined |
| 773 | positionNode(node); |
| 774 | |
| 775 | // cache label array length |
| 776 | network.hostLabelCount = host.labels.length; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 777 | return node; |
| 778 | } |
| 779 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 780 | function positionNode(node) { |
| 781 | var meta = node.metaUi, |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 782 | x = meta && meta.x, |
| 783 | y = meta && meta.y, |
| 784 | xy; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 785 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 786 | // If we have [x,y] already, use that... |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 787 | if (x && y) { |
| 788 | node.fixed = true; |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 789 | node.x = x; |
| 790 | node.y = y; |
| 791 | return; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 792 | } |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 793 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 794 | var location = node.location; |
| 795 | if (location && location.type === 'latlng') { |
| 796 | var coord = geoMapProjection([location.lng, location.lat]); |
| 797 | node.fixed = true; |
| 798 | node.x = coord[0]; |
| 799 | node.y = coord[1]; |
| 800 | return; |
| 801 | } |
| 802 | |
Simon Hunt | ac9e24f | 2014-11-12 10:12:21 -0800 | [diff] [blame] | 803 | // Note: Placing incoming unpinned nodes at exactly the same point |
| 804 | // (center of the view) causes them to explode outwards when |
| 805 | // the force layout kicks in. So, we spread them out a bit |
| 806 | // initially, to provide a more serene layout convergence. |
| 807 | // Additionally, if the node is a host, we place it near |
| 808 | // the device it is connected to. |
| 809 | |
| 810 | function spread(s) { |
| 811 | return Math.floor((Math.random() * s) - s/2); |
| 812 | } |
| 813 | |
| 814 | function randDim(dim) { |
| 815 | return dim / 2 + spread(dim * 0.7071); |
| 816 | } |
| 817 | |
| 818 | function rand() { |
| 819 | return { |
| 820 | x: randDim(network.view.width()), |
| 821 | y: randDim(network.view.height()) |
| 822 | }; |
| 823 | } |
| 824 | |
| 825 | function near(node) { |
| 826 | var min = 12, |
| 827 | dx = spread(12), |
| 828 | dy = spread(12); |
| 829 | return { |
| 830 | x: node.x + min + dx, |
| 831 | y: node.y + min + dy |
| 832 | }; |
| 833 | } |
| 834 | |
| 835 | function getDevice(cp) { |
| 836 | var d = network.lookup[cp.device]; |
| 837 | return d || rand(); |
| 838 | } |
| 839 | |
| 840 | xy = (node.class === 'host') ? near(getDevice(node.cp)) : rand(); |
| 841 | $.extend(node, xy); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 842 | } |
| 843 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 844 | function iconUrl(d) { |
| 845 | return 'img/' + d.type + '.png'; |
| 846 | } |
| 847 | |
| 848 | // returns the newly computed bounding box of the rectangle |
| 849 | function adjustRectToFitText(n) { |
| 850 | var text = n.select('text'), |
| 851 | box = text.node().getBBox(), |
| 852 | lab = config.labels; |
| 853 | |
| 854 | text.attr('text-anchor', 'middle') |
| 855 | .attr('y', '-0.8em') |
| 856 | .attr('x', lab.imgPad/2); |
| 857 | |
| 858 | // translate the bbox so that it is centered on [x,y] |
| 859 | box.x = -box.width / 2; |
| 860 | box.y = -box.height / 2; |
| 861 | |
| 862 | // add padding |
| 863 | box.x -= (lab.padLR + lab.imgPad/2); |
| 864 | box.width += lab.padLR * 2 + lab.imgPad; |
| 865 | box.y -= lab.padTB; |
| 866 | box.height += lab.padTB * 2; |
| 867 | |
| 868 | return box; |
| 869 | } |
| 870 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 871 | function mkSvgClass(d) { |
| 872 | return d.fixed ? d.svgClass + ' fixed' : d.svgClass; |
| 873 | } |
| 874 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 875 | function hostLabel(d) { |
| 876 | var idx = (hostLabelIndex < d.labels.length) ? hostLabelIndex : 0; |
| 877 | return d.labels[idx]; |
| 878 | } |
| 879 | function deviceLabel(d) { |
| 880 | var idx = (deviceLabelIndex < d.labels.length) ? deviceLabelIndex : 0; |
| 881 | return d.labels[idx]; |
| 882 | } |
| 883 | function niceLabel(label) { |
| 884 | return (label && label.trim()) ? label : '.'; |
| 885 | } |
| 886 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 887 | function updateDeviceLabel(d) { |
| 888 | var label = niceLabel(deviceLabel(d)), |
| 889 | node = d.el, |
| 890 | box; |
| 891 | |
| 892 | node.select('text') |
| 893 | .text(label) |
| 894 | .style('opacity', 0) |
| 895 | .transition() |
| 896 | .style('opacity', 1); |
| 897 | |
| 898 | box = adjustRectToFitText(node); |
| 899 | |
| 900 | node.select('rect') |
| 901 | .transition() |
| 902 | .attr(box); |
| 903 | |
| 904 | node.select('image') |
| 905 | .transition() |
| 906 | .attr('x', box.x + config.icons.xoff) |
| 907 | .attr('y', box.y + config.icons.yoff); |
| 908 | } |
| 909 | |
| 910 | function updateHostLabel(d) { |
| 911 | var label = hostLabel(d), |
| 912 | host = d.el; |
| 913 | |
| 914 | host.select('text').text(label); |
| 915 | } |
| 916 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 917 | function updateDeviceState(nodeData) { |
| 918 | nodeData.el.classed('online', nodeData.online); |
| 919 | updateDeviceLabel(nodeData); |
| 920 | // TODO: review what else might need to be updated |
| 921 | } |
| 922 | |
Simon Hunt | 3f03d4a | 2014-11-10 20:14:37 -0800 | [diff] [blame] | 923 | function updateLinkState(linkData) { |
| 924 | updateLinkWidth(linkData); |
| 925 | // TODO: review what else might need to be updated |
| 926 | // update label, if showing |
| 927 | } |
| 928 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 929 | function updateHostState(hostData) { |
| 930 | updateHostLabel(hostData); |
| 931 | // TODO: review what else might need to be updated |
| 932 | } |
| 933 | |
| 934 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 935 | function updateNodes() { |
| 936 | node = nodeG.selectAll('.node') |
| 937 | .data(network.nodes, function (d) { return d.id; }); |
| 938 | |
| 939 | // operate on existing nodes, if necessary |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 940 | // update host labels |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 941 | //node .foo() .bar() ... |
| 942 | |
| 943 | // operate on entering nodes: |
| 944 | var entering = node.enter() |
| 945 | .append('g') |
| 946 | .attr({ |
| 947 | id: function (d) { return safeId(d.id); }, |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 948 | class: mkSvgClass, |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 949 | transform: function (d) { return translate(d.x, d.y); }, |
| 950 | opacity: 0 |
| 951 | }) |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 952 | .call(network.drag) |
Thomas Vachuska | 3266abf | 2014-11-13 09:28:46 -0800 | [diff] [blame] | 953 | .on('mouseover', function (d) { |
| 954 | console.log(d); |
| 955 | if (d.class === 'host') { |
| 956 | requestTraffic(d); |
| 957 | } |
| 958 | }) |
| 959 | .on('mouseout', function (d) { |
| 960 | console.log(d); |
| 961 | if (d.class === 'host') { |
| 962 | cancelTraffic(d); |
| 963 | } |
| 964 | }) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 965 | //.on('mouseover', function (d) {}) |
| 966 | .transition() |
| 967 | .attr('opacity', 1); |
| 968 | |
| 969 | // augment device nodes... |
| 970 | entering.filter('.device').each(function (d) { |
| 971 | var node = d3.select(this), |
| 972 | icon = iconUrl(d), |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 973 | label = niceLabel(deviceLabel(d)), |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 974 | box; |
| 975 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 976 | // provide ref to element from backing data.... |
| 977 | d.el = node; |
| 978 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 979 | node.append('rect') |
| 980 | .attr({ |
| 981 | 'rx': 5, |
| 982 | 'ry': 5 |
| 983 | }); |
| 984 | |
| 985 | node.append('text') |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 986 | .text(label) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 987 | .attr('dy', '1.1em'); |
| 988 | |
| 989 | box = adjustRectToFitText(node); |
| 990 | |
| 991 | node.select('rect') |
| 992 | .attr(box); |
| 993 | |
| 994 | if (icon) { |
| 995 | var cfg = config.icons; |
| 996 | node.append('svg:image') |
| 997 | .attr({ |
| 998 | x: box.x + config.icons.xoff, |
| 999 | y: box.y + config.icons.yoff, |
| 1000 | width: cfg.w, |
| 1001 | height: cfg.h, |
| 1002 | 'xlink:href': icon |
| 1003 | }); |
| 1004 | } |
| 1005 | |
| 1006 | // debug function to show the modelled x,y coordinates of nodes... |
| 1007 | if (debug('showNodeXY')) { |
| 1008 | node.select('rect').attr('fill-opacity', 0.5); |
| 1009 | node.append('circle') |
| 1010 | .attr({ |
| 1011 | class: 'debug', |
| 1012 | cx: 0, |
| 1013 | cy: 0, |
| 1014 | r: '3px' |
| 1015 | }); |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1016 | } |
| 1017 | }); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1018 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1019 | // augment host nodes... |
| 1020 | entering.filter('.host').each(function (d) { |
| 1021 | var node = d3.select(this), |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1022 | box; |
| 1023 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1024 | // provide ref to element from backing data.... |
| 1025 | d.el = node; |
| 1026 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1027 | node.append('circle') |
| 1028 | .attr('r', 8); // TODO: define host circle radius |
| 1029 | |
| 1030 | // TODO: are we attaching labels to hosts? |
| 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, |
| 1221 | meta = d3.event.sourceEvent.metaKey; |
| 1222 | |
| 1223 | if (el) { |
| 1224 | n = d3.select(el); |
| 1225 | } else { |
| 1226 | node.each(function(d) { |
| 1227 | if (d == obj) { |
| 1228 | n = d3.select(el = this); |
| 1229 | } |
| 1230 | }); |
| 1231 | } |
| 1232 | if (!n) return; |
| 1233 | |
| 1234 | if (meta && n.classed('selected')) { |
| 1235 | deselectObject(obj.id); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1236 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | if (!meta) { |
| 1241 | deselectAll(); |
| 1242 | } |
| 1243 | |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1244 | selections[obj.id] = { obj: obj, el: el }; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1245 | selectOrder.push(obj.id); |
| 1246 | |
| 1247 | n.classed('selected', true); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1248 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | function deselectObject(id) { |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1252 | var obj = selections[id], |
| 1253 | idx; |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1254 | if (obj) { |
| 1255 | d3.select(obj.el).classed('selected', false); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1256 | delete selections[id]; |
Simon Hunt | c31d569 | 2014-11-12 13:27:18 -0800 | [diff] [blame] | 1257 | idx = $.inArray(id, selectOrder); |
| 1258 | if (idx >= 0) { |
| 1259 | selectOrder.splice(idx, 1); |
| 1260 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1261 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | function deselectAll() { |
| 1265 | // deselect all nodes in the network... |
| 1266 | node.classed('selected', false); |
| 1267 | selections = {}; |
| 1268 | selectOrder = []; |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1269 | updateDetailPane(); |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1270 | } |
| 1271 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1272 | // FIXME: this click handler does not get unloaded when the view does |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1273 | $('#view').on('click', function(e) { |
| 1274 | if (!$(e.target).closest('.node').length) { |
| 1275 | if (!e.metaKey) { |
| 1276 | deselectAll(); |
| 1277 | } |
| 1278 | } |
| 1279 | }); |
| 1280 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1281 | // update the state of the detail pane, based on current selections |
| 1282 | function updateDetailPane() { |
| 1283 | var nSel = selectOrder.length; |
| 1284 | if (!nSel) { |
| 1285 | detailPane.hide(); |
| 1286 | } else if (nSel === 1) { |
| 1287 | singleSelect(); |
| 1288 | } else { |
| 1289 | multiSelect(); |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | function singleSelect() { |
| 1294 | requestDetails(); |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1295 | // NOTE: detail pane will be shown from showDetails event callback |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | function multiSelect() { |
Simon Hunt | b53e068 | 2014-11-12 13:32:01 -0800 | [diff] [blame] | 1299 | populateMultiSelect(); |
| 1300 | // TODO: Add multi-select actions ... |
| 1301 | } |
| 1302 | |
| 1303 | function addSep(tbody) { |
| 1304 | var tr = tbody.append('tr'); |
| 1305 | $('<hr>').appendTo(tr.append('td').attr('colspan', 2)); |
| 1306 | } |
| 1307 | |
| 1308 | function addProp(tbody, label, value) { |
| 1309 | var tr = tbody.append('tr'); |
| 1310 | |
| 1311 | tr.append('td') |
| 1312 | .attr('class', 'label') |
| 1313 | .text(label + ' :'); |
| 1314 | |
| 1315 | tr.append('td') |
| 1316 | .attr('class', 'value') |
| 1317 | .text(value); |
| 1318 | } |
| 1319 | |
| 1320 | function populateMultiSelect() { |
| 1321 | detailPane.empty(); |
| 1322 | |
| 1323 | var title = detailPane.append("h2"), |
| 1324 | table = detailPane.append("table"), |
| 1325 | tbody = table.append("tbody"); |
| 1326 | |
| 1327 | title.text('Multi-Select...'); |
| 1328 | |
| 1329 | selectOrder.forEach(function (d, i) { |
| 1330 | addProp(tbody, i+1, d); |
| 1331 | }); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | function populateDetails(data) { |
| 1335 | detailPane.empty(); |
| 1336 | |
| 1337 | var title = detailPane.append("h2"), |
| 1338 | table = detailPane.append("table"), |
| 1339 | tbody = table.append("tbody"); |
| 1340 | |
| 1341 | $('<img src="img/' + data.type + '.png">').appendTo(title); |
| 1342 | $('<span>').attr('class', 'icon').text(data.id).appendTo(title); |
| 1343 | |
| 1344 | data.propOrder.forEach(function(p) { |
| 1345 | if (p === '-') { |
| 1346 | addSep(tbody); |
| 1347 | } else { |
| 1348 | addProp(tbody, p, data.props[p]); |
| 1349 | } |
| 1350 | }); |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | // ============================== |
| 1354 | // Test harness code |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1355 | |
| 1356 | function prepareScenario(view, ctx, dbg) { |
| 1357 | var sc = scenario, |
| 1358 | urlSc = sc.evDir + ctx + sc.evScenario; |
| 1359 | |
| 1360 | if (!ctx) { |
| 1361 | view.alert("No scenario specified (null ctx)"); |
| 1362 | return; |
| 1363 | } |
| 1364 | |
| 1365 | sc.view = view; |
| 1366 | sc.ctx = ctx; |
| 1367 | sc.debug = dbg; |
| 1368 | sc.evNumber = 0; |
| 1369 | |
| 1370 | d3.json(urlSc, function(err, data) { |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1371 | var p = data && data.params || {}, |
| 1372 | desc = data && data.description || null, |
Simon Hunt | fc274c9 | 2014-11-11 11:05:46 -0800 | [diff] [blame] | 1373 | intro = data && data.title; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1374 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1375 | if (err) { |
| 1376 | view.alert('No scenario found:\n\n' + urlSc + '\n\n' + err); |
| 1377 | } else { |
| 1378 | sc.params = p; |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1379 | if (desc) { |
| 1380 | intro += '\n\n ' + desc.join('\n '); |
| 1381 | } |
| 1382 | view.alert(intro); |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1383 | } |
| 1384 | }); |
| 1385 | |
| 1386 | } |
| 1387 | |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1388 | |
| 1389 | function para(sel, text) { |
| 1390 | sel.append('p').text(text); |
| 1391 | } |
| 1392 | |
Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1393 | // ============================== |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1394 | // View life-cycle callbacks |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1395 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1396 | function preload(view, ctx, flags) { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1397 | var w = view.width(), |
| 1398 | h = view.height(), |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1399 | fcfg = config.force, |
| 1400 | fpad = fcfg.pad, |
| 1401 | forceDim = [w - 2*fpad, h - 2*fpad]; |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1402 | |
Simon Hunt | bb282f5 | 2014-11-10 11:08:19 -0800 | [diff] [blame] | 1403 | // TODO: set trace api |
| 1404 | //trace = onos.exported.webSockTrace; |
| 1405 | |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1406 | // NOTE: view.$div is a D3 selection of the view's div |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1407 | var viewBox = '0 0 ' + config.logicalSize + ' ' + config.logicalSize; |
| 1408 | svg = view.$div.append('svg').attr('viewBox', viewBox); |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1409 | setSize(svg, view); |
| 1410 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1411 | // add blue glow filter to svg layer |
| 1412 | d3u.appendGlow(svg); |
| 1413 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1414 | // group for the topology |
| 1415 | topoG = svg.append('g') |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1416 | .attr('id', 'topo-G') |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1417 | .attr('transform', fcfg.translate()); |
| 1418 | |
| 1419 | // subgroups for links and nodes |
| 1420 | linkG = topoG.append('g').attr('id', 'links'); |
| 1421 | nodeG = topoG.append('g').attr('id', 'nodes'); |
| 1422 | |
| 1423 | // selection of nodes and links |
| 1424 | link = linkG.selectAll('.link'); |
| 1425 | node = nodeG.selectAll('.node'); |
| 1426 | |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1427 | function chrg(d) { |
| 1428 | return fcfg.charge[d.class] || -12000; |
| 1429 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1430 | function ldist(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1431 | return fcfg.linkDistance[d.type] || 50; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1432 | } |
| 1433 | function lstrg(d) { |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1434 | // 0.0 - 1.0 |
| 1435 | return fcfg.linkStrength[d.type] || 1.0; |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1436 | } |
| 1437 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1438 | function selectCb(d, self) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1439 | selectObject(d, self); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | function atDragEnd(d, self) { |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1443 | // once we've finished moving, pin the node in position |
| 1444 | d.fixed = true; |
| 1445 | d3.select(self).classed('fixed', true); |
| 1446 | if (config.useLiveData) { |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1447 | sendUpdateMeta(d); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1448 | } |
| 1449 | } |
| 1450 | |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1451 | function sendUpdateMeta(d) { |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1452 | sendMessage('updateMeta', { |
| 1453 | id: d.id, |
| 1454 | 'class': d.class, |
Simon Hunt | 902c992 | 2014-11-11 11:59:31 -0800 | [diff] [blame] | 1455 | 'memento': { |
| 1456 | x: Math.floor(d.x), |
| 1457 | y: Math.floor(d.y) |
| 1458 | } |
Thomas Vachuska | d1be50d | 2014-11-08 16:10:20 -0800 | [diff] [blame] | 1459 | }); |
| 1460 | } |
| 1461 | |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1462 | // set up the force layout |
| 1463 | network.force = d3.layout.force() |
| 1464 | .size(forceDim) |
| 1465 | .nodes(network.nodes) |
| 1466 | .links(network.links) |
Simon Hunt | 7cd48f3 | 2014-11-09 23:42:50 -0800 | [diff] [blame] | 1467 | .gravity(0.4) |
| 1468 | .friction(0.7) |
| 1469 | .charge(chrg) |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1470 | .linkDistance(ldist) |
| 1471 | .linkStrength(lstrg) |
Simon Hunt | c7ee066 | 2014-11-05 16:44:37 -0800 | [diff] [blame] | 1472 | .on('tick', tick); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1473 | |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1474 | network.drag = d3u.createDragBehavior(network.force, selectCb, atDragEnd); |
Simon Hunt | 0c6d419 | 2014-11-12 12:07:10 -0800 | [diff] [blame] | 1475 | |
| 1476 | // create mask layer for when we lose connection to server. |
| 1477 | mask = view.$div.append('div').attr('id','topo-mask'); |
| 1478 | para(mask, 'Oops!'); |
| 1479 | para(mask, 'Web-socket connection to server closed...'); |
| 1480 | para(mask, 'Try refreshing the page.'); |
Simon Hunt | 1a9eff9 | 2014-11-07 11:06:34 -0800 | [diff] [blame] | 1481 | } |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1482 | |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1483 | function load(view, ctx, flags) { |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1484 | // resize, in case the window was resized while we were not loaded |
| 1485 | resize(view, ctx, flags); |
| 1486 | |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1487 | // cache the view token, so network topo functions can access it |
| 1488 | network.view = view; |
Simon Hunt | 56d5185 | 2014-11-09 13:03:35 -0800 | [diff] [blame] | 1489 | config.useLiveData = !flags.local; |
| 1490 | |
| 1491 | if (!config.useLiveData) { |
| 1492 | prepareScenario(view, ctx, flags.debug); |
| 1493 | } |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1494 | |
| 1495 | // set our radio buttons and key bindings |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1496 | view.setRadio(btnSet); |
| 1497 | view.setKeys(keyDispatch); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1498 | |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1499 | // Load map data asynchronously; complete startup after that.. |
| 1500 | loadGeoJsonData(); |
| 1501 | } |
| 1502 | |
| 1503 | // TODO: move these to config/state portion of script |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1504 | var geoJsonUrl = 'json/map/continental_us.json', // TODO: Paul |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1505 | geoJson; |
| 1506 | |
| 1507 | function loadGeoJsonData() { |
| 1508 | d3.json(geoJsonUrl, function (err, data) { |
| 1509 | if (err) { |
| 1510 | // fall back to USA map background |
| 1511 | loadStaticMap(); |
| 1512 | } else { |
| 1513 | geoJson = data; |
| 1514 | loadGeoMap(); |
| 1515 | } |
| 1516 | |
| 1517 | // finally, connect to the server... |
| 1518 | if (config.useLiveData) { |
| 1519 | webSock.connect(); |
| 1520 | } |
| 1521 | }); |
| 1522 | } |
| 1523 | |
| 1524 | function showBg() { |
| 1525 | return config.options.showBackground ? 'visible' : 'hidden'; |
| 1526 | } |
| 1527 | |
| 1528 | function loadStaticMap() { |
| 1529 | fnTrace('loadStaticMap', config.backgroundUrl); |
| 1530 | var w = network.view.width(), |
| 1531 | h = network.view.height(); |
| 1532 | |
| 1533 | // load the background image |
| 1534 | bgImg = svg.insert('svg:image', '#topo-G') |
| 1535 | .attr({ |
| 1536 | id: 'topo-bg', |
| 1537 | width: w, |
| 1538 | height: h, |
| 1539 | 'xlink:href': config.backgroundUrl |
| 1540 | }) |
| 1541 | .style({ |
| 1542 | visibility: showBg() |
| 1543 | }); |
| 1544 | } |
| 1545 | |
| 1546 | function loadGeoMap() { |
| 1547 | fnTrace('loadGeoMap', geoJsonUrl); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1548 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1549 | // extracts the topojson data into geocoordinate-based geometry |
| 1550 | var topoData = topojson.feature(geoJson, geoJson.objects.states); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1551 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1552 | // see: http://bl.ocks.org/mbostock/4707858 |
| 1553 | geoMapProjection = d3.geo.mercator(); |
| 1554 | var path = d3.geo.path().projection(geoMapProjection); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1555 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1556 | geoMapProjection |
| 1557 | .scale(1) |
| 1558 | .translate([0, 0]); |
Simon Hunt | d3b7d51 | 2014-11-12 15:48:41 -0800 | [diff] [blame] | 1559 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1560 | // [[x1,y1],[x2,y2]] |
| 1561 | var b = path.bounds(topoData); |
| 1562 | // TODO: why 1.75? |
| 1563 | var s = 1.75 / Math.max((b[1][0] - b[0][0]) / config.logicalSize, (b[1][1] - b[0][1]) / config.logicalSize); |
| 1564 | 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] | 1565 | |
Paul Greyson | 6cb8ca0 | 2014-11-12 18:09:02 -0800 | [diff] [blame] | 1566 | geoMapProjection |
| 1567 | .scale(s) |
| 1568 | .translate(t); |
| 1569 | |
| 1570 | bgImg = svg.insert("g", '#topo-G'); |
| 1571 | bgImg.attr('id', 'map').selectAll('path') |
| 1572 | .data(topoData.features) |
| 1573 | .enter() |
| 1574 | .append('path') |
| 1575 | .attr('d', path); |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1576 | } |
| 1577 | |
Simon Hunt | f67722a | 2014-11-10 09:32:06 -0800 | [diff] [blame] | 1578 | function resize(view, ctx, flags) { |
Simon Hunt | 934c3ce | 2014-11-05 11:45:07 -0800 | [diff] [blame] | 1579 | setSize(svg, view); |
Simon Hunt | 99c1384 | 2014-11-06 18:23:12 -0800 | [diff] [blame] | 1580 | |
| 1581 | // TODO: hook to recompute layout, perhaps? work with zoom/pan code |
| 1582 | // adjust force layout size |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | // ============================== |
| 1587 | // View registration |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1588 | |
Simon Hunt | 2524891 | 2014-11-04 11:25:48 -0800 | [diff] [blame] | 1589 | onos.ui.addView('topo', { |
Simon Hunt | 142d003 | 2014-11-04 20:13:09 -0800 | [diff] [blame] | 1590 | preload: preload, |
| 1591 | load: load, |
| 1592 | resize: resize |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1593 | }); |
| 1594 | |
Simon Hunt | 61d0404 | 2014-11-11 17:27:16 -0800 | [diff] [blame] | 1595 | detailPane = onos.ui.addFloatingPanel('topo-detail'); |
| 1596 | |
Simon Hunt | 195cb38 | 2014-11-03 17:50:51 -0800 | [diff] [blame] | 1597 | }(ONOS)); |