1 |
|
2 |
|
3 | fbpGraph = require 'fbp-graph'
|
4 | common = require './common'
|
5 | debug = require('debug')('fbp-spec:protocol')
|
6 | Promise = require 'bluebird'
|
7 |
|
8 | exports.sendGraph = (client, graph , callback) ->
|
9 | main = false
|
10 | return callback new Error "Graph not defined" if not graph
|
11 |
|
12 | graphId = graph.name or graph.properties.id
|
13 | graphId = "fixture.#{common.randomString(10)}" if not graphId
|
14 | graph.name = graphId
|
15 |
|
16 | unless graph instanceof fbpGraph.Graph
|
17 |
|
18 | fbpGraph.graph.loadJSON graph, (err, g) ->
|
19 | return callback err if err
|
20 | exports.sendGraph client, g, callback
|
21 | return
|
22 |
|
23 | debug 'sendgraph', graphId
|
24 |
|
25 | Promise.resolve()
|
26 | .then(() -> client.protocol.graph.send(graph, main))
|
27 | .then(() -> graphId)
|
28 | .nodeify(callback)
|
29 | return
|
30 |
|
31 | exports.startNetwork = (client, graphId, callback) ->
|
32 | debug 'startnetwork', graphId
|
33 |
|
34 | Promise.resolve()
|
35 | .then(() -> client.protocol.network.start(
|
36 | graph: graphId
|
37 | ))
|
38 | .nodeify(callback)
|
39 | return
|
40 |
|
41 | exports.stopNetwork = (client, graphId, callback) ->
|
42 | debug 'stopnetwork', graphId
|
43 |
|
44 | Promise.resolve()
|
45 | .then(() -> client.protocol.network.stop(
|
46 | graph: graphId
|
47 | ))
|
48 | .nodeify(callback)
|
49 | return
|
50 |
|
51 | exports.sendPackets = (client, graphId, packets, callback) ->
|
52 | debug 'sendpackets', graphId, packets
|
53 |
|
54 | Promise.all(Object.keys(packets).map((port) ->
|
55 | return client.protocol.runtime.packet
|
56 | event: 'data'
|
57 | port: port
|
58 | payload: packets[port]
|
59 | graph: graphId
|
60 | ))
|
61 | .nodeify(callback)
|
62 | return
|
63 |
|
64 | exports.getComponents = getComponents = (client, callback) ->
|
65 | debug 'get components'
|
66 |
|
67 | Promise.resolve()
|
68 | .then(() -> client.protocol.component.list())
|
69 | .then((componentList) ->
|
70 | components = {}
|
71 | for component in componentList
|
72 | components[component.name] = component
|
73 | return components
|
74 | )
|
75 | .nodeify(callback)
|
76 | return
|
77 |
|
78 | exports.getCapabilities = (client, callback) ->
|
79 | def = client.definition
|
80 | return callback null, def.capabilities if def?.capabilities?.length
|
81 | Promise.resolve()
|
82 | .then(() -> client.protocol.runtime.getruntime())
|
83 | .then((definition) -> definition.capabilities)
|
84 | .nodeify(callback)
|
85 | return
|
86 |
|
87 | exports.getComponentTests = (client, callback) ->
|
88 | debug 'get component tests'
|
89 |
|
90 | Promise.resolve()
|
91 | .then(() -> client.protocol.component.list())
|
92 | .then((components) ->
|
93 | return Promise.all(components.map((component) ->
|
94 | client.protocol.component.getsource
|
95 | name: component.name
|
96 | ))
|
97 | )
|
98 | .then((sources) ->
|
99 | tests = {}
|
100 | for source in sources
|
101 | continue unless source.tests
|
102 | name = if source.library then "#{source.library}/#{source.name}" else source.name
|
103 | tests[name] = source.tests
|
104 | return tests
|
105 | )
|
106 | .nodeify(callback)
|
107 | return
|