1 | import Accounts from '../services/accounts'
|
2 | import Config from '../services/config'
|
3 | import ForwardingRoutingTable from '../services/forwarding-routing-table'
|
4 | import reduct = require('reduct')
|
5 | import { Relation } from './relation'
|
6 | import CcpSender from './ccp-sender'
|
7 | import CcpReceiver from './ccp-receiver'
|
8 |
|
9 | export interface PeerOpts {
|
10 | deps: reduct.Injector,
|
11 | accountId: string,
|
12 | sendRoutes: boolean,
|
13 | receiveRoutes: boolean
|
14 | }
|
15 |
|
16 | export default class Peer {
|
17 | private config: Config
|
18 | private accounts: Accounts
|
19 | private accountId: string
|
20 | private ccpSender?: CcpSender
|
21 | private ccpReceiver?: CcpReceiver
|
22 |
|
23 | constructor ({ deps, accountId, sendRoutes, receiveRoutes }: PeerOpts) {
|
24 | this.config = deps(Config)
|
25 | this.accounts = deps(Accounts)
|
26 | this.accountId = accountId
|
27 |
|
28 | const plugin = this.accounts.getPlugin(accountId)
|
29 | const forwardingRoutingTable = deps(ForwardingRoutingTable)
|
30 |
|
31 | if (sendRoutes) {
|
32 | this.ccpSender = new CcpSender({
|
33 | accountId,
|
34 | plugin,
|
35 | forwardingRoutingTable,
|
36 | getOwnAddress: () => this.accounts.getOwnAddress(),
|
37 | getAccountRelation: this.getAccountRelation,
|
38 | routeExpiry: this.config.routeExpiry,
|
39 | routeBroadcastInterval: this.config.routeBroadcastInterval
|
40 | })
|
41 | }
|
42 |
|
43 | if (receiveRoutes) {
|
44 | this.ccpReceiver = new CcpReceiver({ accountId, plugin })
|
45 | }
|
46 | }
|
47 |
|
48 | getAccountId () {
|
49 | return this.accountId
|
50 | }
|
51 |
|
52 | getReceiver () {
|
53 | return this.ccpReceiver
|
54 | }
|
55 |
|
56 | getSender () {
|
57 | return this.ccpSender
|
58 | }
|
59 |
|
60 | private getAccountRelation = (accountId: string): Relation => {
|
61 | return accountId ? this.accounts.getInfo(accountId).relation : 'local'
|
62 | }
|
63 | }
|