Skip to content

Commit e76e522

Browse files
committed
local topic host
1 parent d91f8bf commit e76e522

17 files changed

Lines changed: 375 additions & 16 deletions

examples/test-peer-party.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ async function main(){
4242
await peer1.loadIdentity()
4343
await peer2.loadIdentity()
4444

45+
console.log('peer identity', peer1.identity)
46+
4547
//peer1.comms.remoteIdentity = peer2.identity
4648
peer2.comms.remoteIdentity = peer1.identity
4749

examples/test-service-node-host.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,16 @@ async function main(){
3131

3232
const path = '/data/datparty/srv-party'
3333

34+
3435
let party = new Dataparty.TingoParty({
3536
path,
3637
model: BouncerClientModels,
3738
serverModels: BouncerServerModels,
3839
config: new Dataparty.Config.JsonFileConfig({basePath: '/data/datparty/'})
3940
})
4041

42+
party.topics = new Dataparty.LocalTopicHost()
43+
4144
const service = new ExampleService({ name: '@dataparty/example', version: '0.0.1' })
4245

4346
const build = await service.compile(Path.join(__dirname,'/dataparty'), true)
@@ -46,13 +49,14 @@ async function main(){
4649

4750
const runner = new Dataparty.ServiceRunnerNode({
4851
party, service,
49-
sendFullErrors: false
52+
sendFullErrors: false,
53+
useNative: false
5054
})
5155

5256
const host = new Dataparty.ServiceHost({
5357
runner,
5458
trust_proxy: true,
55-
wsEnabled: true
59+
wsEnabled: true,
5660
})
5761

5862
await party.start()

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@
123123
"minami": "^1.2.3",
124124
"mongodb-client-encryption": "^2.2.1",
125125
"parcel": "^2.3.1",
126+
"path-browserify": "^1.0.1",
126127
"process": "^0.11.10",
128+
"punycode": "2.3.0",
127129
"querystring-es3": "^0.2.1",
128130
"stream-browserify": "^3.0.0",
129131
"stream-http": "^3.2.0",

public/example.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,47 @@
55
</script>
66
</head>
77

8+
<body>
9+
10+
11+
<script>
12+
13+
async function init(){
14+
15+
//new Dataparty.Config.MemoryConfig()
16+
17+
config = new Dataparty.Config.LocalStorageConfig({
18+
basePath:'demo',
19+
cloud: {
20+
uri: 'http://172.16.3.1:4001'
21+
}
22+
})
23+
24+
25+
const remoteIdentity = await Dataparty.Comms.RestComms.HttpGet( config.read('cloud.uri') + '/identity')
26+
27+
console.log('cloud.uri -',config.read('cloud.uri'))
28+
console.log('\tremoteIdentity', remoteIdentity)
29+
30+
let party = new Dataparty.PeerParty({
31+
comms: new Dataparty.Comms.WebsocketComms({
32+
uri:'ws://172.16.3.1:4001/ws',
33+
discoverRemoteIdentity: false,
34+
remoteIdentity: remoteIdentity,
35+
session: Math.random().toString(36).slice(2)
36+
}),
37+
config: config
38+
})
39+
40+
41+
window.party = party
42+
}
43+
44+
init()
45+
46+
47+
</script>
48+
49+
</body>
50+
851
</html>

src/comms/peer-comms.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const debug = require('debug')('dataparty.comms.peercomms')
2-
2+
const uuidv4 = require('uuid/v4')
33
const HttpMocks = require('node-mocks-http')
44

55
const SocketOp = require('./op/socket-op')
@@ -22,6 +22,7 @@ class PeerComms extends SocketComms {
2222
constructor({remoteIdentity, discoverRemoteIdentity, host, party, socket, ...options}){
2323
super({remoteIdentity, discoverRemoteIdentity, party, ...options})
2424

25+
this.uuid = uuidv4()
2526
this.socket = socket || null
2627

2728
this.host = host //! Is comms host\
@@ -166,7 +167,7 @@ class PeerComms extends SocketComms {
166167
await this.socketInit()
167168
}
168169

169-
this.socket.on('close', this.onclose.bind(this))
170+
this.socket.on('close', this.close.bind(this))
170171

171172
if(this.host){
172173
debug('host mode comms')
@@ -186,12 +187,21 @@ class PeerComms extends SocketComms {
186187
}
187188

188189
async stop(){
190+
debug('stop')
189191
this.close()
190192
}
191193

192-
close(){
194+
async close(){
195+
debug('close')
196+
197+
if(this.party.topics){
198+
await this.party.topics.destroyNode(this)
199+
}
200+
193201
debug('closing connection')
194202
this.socket.destroy()
203+
204+
this.onclose()
195205
}
196206

197207

@@ -210,7 +220,34 @@ class PeerComms extends SocketComms {
210220

211221
return this.handleCallOp(op)
212222

223+
} else if (op.op === 'advertise' && this.state === PeerComms.STATES.AUTHED) {
224+
225+
if(this.party.topics){
226+
await this.party.topics.advertise(this, op.topic)
227+
}
228+
229+
} else if (op.op === 'subscribe' && this.state === PeerComms.STATES.AUTHED) {
230+
231+
if(this.party.topics){
232+
await this.party.topics.subscribe(this, op.topic)
233+
}
234+
235+
} else if (op.op === 'unsubscribe' && this.state === PeerComms.STATES.AUTHED) {
236+
237+
if(this.party.topics){
238+
await this.party.topics.unsubscribe(this, op.topic)
239+
}
240+
241+
} else if (op.op === 'publish' && this.state === PeerComms.STATES.AUTHED) {
242+
243+
if(this.party.topics){
244+
await this.party.topics.publish(this, op.topic, op.msg)
245+
}
246+
213247
} else {
248+
debug('⚠️ op not implemented ⚠️')
249+
debug(op.input)
250+
214251
op.result='not implemented'
215252
op.setState(HostOp.STATES.Finished_Fail)
216253
}

src/comms/websocket-comms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const debug = require('debug')('dataparty.comms.websocket')
22

3-
const WebSocket = require('ws')
3+
const WebSocket = global.WebSocket ? global.WebSocket : require('ws')
44
const EventEmitter = require('eventemitter3')
55

66

src/index-browser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Comms = require('./comms')
22
const Party = require('./party/index-browser')
3+
const Topics = require('./topics')
34

45
const MemoryConfig = require('./config/memory')
56
const LocalStorageConfig = require('./config/local-storage')
@@ -13,7 +14,8 @@ const Config = {
1314
let lib = {
1415
Comms,
1516
Config,
16-
...Party
17+
...Party,
18+
...Topics
1719
}
1820

1921

src/index-embedded.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
const Comms = require('./comms')
22
const Config = require('./config')
33
const Party = require('./party/index-embedded')
4+
const Topics = require('./topics')
45
const Bouncer = require('./bouncer')
56

67
module.exports = {
78
Comms,
89
Config,
910
Bouncer,
10-
...Party
11+
...Party,
12+
...Topics
1113
}

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const Comms = require('./comms')
22
const Config = require('./config')
33
const Party = require('./party')
4+
const Topics = require('./topics')
45
const Bouncer = require('./bouncer')
56
const Service = require('./service')
67
const Sandbox = require('./sandbox')
@@ -10,6 +11,7 @@ module.exports = {
1011
Config,
1112
Bouncer,
1213
...Party,
14+
...Topics,
1315
...Service,
14-
...Sandbox
15-
}
16+
...Sandbox,
17+
}

src/party/peer/peer-party.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ const debug = require('debug')('dataparty.peer-party')
55
const Qb = require('../qb')
66
const IParty = require('../iparty')
77

8+
const LocalTopicHost = require('../../topics/local-topic-host')
9+
810
/**
911
* @class
1012
* @alias module:dataparty.LocalParty
@@ -29,8 +31,10 @@ class PeerParty extends IParty {
2931
this.hostRunner = null
3032

3133
if(this.comms.host){
34+
debug('host')
3235
this.hostParty = hostParty
3336
this.hostRunner = hostRunner
37+
this.topics = new LocalTopicHost()
3438
}
3539
}
3640

0 commit comments

Comments
 (0)