This repository was archived by the owner on Dec 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathserver.ts
111 lines (94 loc) · 2.74 KB
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { EventEmitter } from 'events'
import { Config } from '../../config'
import { Bootnode, BootnodeLike, KeyLike } from '../../types'
import { parseKey, parseBootnodes } from '../../util/parse'
import { Protocol } from '../protocol/protocol'
export interface ServerOptions {
/* Config */
config: Config
/* How often (in ms) to discover new peers (default: 30000) */
refreshInterval?: number
/* Private key to use for server */
key?: KeyLike
/* List of bootnodes to use for discovery */
bootnodes?: BootnodeLike
}
/**
* Base class for transport specific server implementations.
* @memberof module:net/server
*/
export class Server extends EventEmitter {
public config: Config
public key: Buffer | undefined
public bootnodes: Bootnode[] = []
protected refreshInterval: number
protected protocols: Set<Protocol>
public started: boolean
/**
* Create new server
* @param {ServerOptions}
*/
constructor(options: ServerOptions) {
super()
this.config = options.config
this.key = options.key ? parseKey(options.key) : undefined
this.bootnodes = options.bootnodes ? parseBootnodes(options.bootnodes) : []
this.refreshInterval = options.refreshInterval ?? 30000
this.protocols = new Set()
this.started = false
}
get name(): string {
return this.constructor.name
}
/**
* Check if server is running
*/
get running(): boolean {
return this.started
}
/**
* Start server. Returns a promise that resolves once server has been started.
* @return Resolves with true if server successfully started
*/
async start(): Promise<boolean> {
if (this.started) {
return false
}
const protocols: Protocol[] = Array.from(this.protocols)
await Promise.all(protocols.map((p) => p.open()))
this.started = true
this.config.logger.info(`Started ${this.name} server.`)
return true
}
/**
* Stop server. Returns a promise that resolves once server has been stopped.
*/
async stop(): Promise<boolean> {
this.started = false
this.config.logger.info(`Stopped ${this.name} server.`)
return this.started
}
/**
* Specify which protocols the server must support
* @param protocols protocol classes
* @return True if protocol added successfully
*/
addProtocols(protocols: Protocol[]): boolean {
if (this.started) {
this.config.logger.error('Cannot require protocols after server has been started')
return false
}
protocols.forEach((p) => this.protocols.add(p))
return true
}
/**
* Ban peer for a specified time
* @protected
* @param peerId id of peer
* @param maxAge how long to ban peer
* @return {Promise}
*/
ban(_peerId: string, _maxAge: number) {
// don't do anything by default
}
}