-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
246 lines (209 loc) · 5.76 KB
/
index.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
const querystring = require('querystring')
const net = require('net')
const domain = require('./lib/domain')
const tracker = require('./lib/tracker')
const noop = () => {}
/**
* Constructor
*
* @param {Array} trackers An array of mogile trackers
* @param {Number} retries Number of times to retry an operation
*/
const Mogile = function(trackers, retries) {
//default set trackers 127.0.0.1:7001
trackers = trackers || ['127.0.0.1:7001']
// The tracker hosts
this.trackers = []
for (let i = 0; i < trackers.length; i++) {
this.trackers.push(new tracker.factory(trackers[i]))
}
// The current tracker being used
this.current_tracker = null
// The number of times to retry an operation
this.retries = (typeof retries !== 'undefined') ? retries : 1
// The default encoding for connections
this.encoding = 'ascii'
}
/**
* Factory class
*
* Returns a new instance of Mogile.
*
* @param {Array} trackers An array of mogile trackers
* @param {Number} retries Number of times to retry an operation
* @return {Mogile}
*/
Mogile.createClient = function(trackers, retries) {
return new Mogile(trackers, retries)
}
/**
* Domain factory method
*
* Returns a new instance of Domain.
*
* @return {Domain}
*/
Mogile.domain = Mogile.prototype.domain = function(name) {
return domain.factory(this, name)
}
/**
* Gets a list of all the domains in the file system
*
* @param {Function} callback Function to call with an array of all domains
* @return {Promise}
*/
Mogile.prototype.getDomains = function() {
return new Promise((resolve, reject) => {
this.send('default', 'GET_DOMAINS', {}, (err, results) => {
if (err) {
reject(err)
}
else {
const domains = []
for (let i = 1; i <= results['domains']; i++) {
const dom = 'domain' + i
const classes = {}
for (let j = 1; j <= results[dom + 'classes']; j++) {
classes[results[`${dom}class${j}name`]] = results[`${dom}class${j}mindevcount`] - 0
}
domains.push({
name: results[dom],
classes: classes
})
}
resolve(domains)
}
})
})
}
/**
* Create domain
*
* @param {String} name domain name
* @return {Promise}
*/
Mogile.prototype.createDomain = function(name) {
name = name || null
return new Promise((resolve, reject) => {
this.send(name, 'CREATE_DOMAIN', null, (err, response) => {
if (err) {
reject(err)
} else {
resolve(response)
}
})
})
}
/**
* delete domain
*
* @param {String} name domain name
* @return {Promise}
*/
Mogile.prototype.deleteDomain = function(name) {
name = name || null
return new Promise((resolve, reject) => {
this.send(name, 'DELETE_DOMAIN', null, (err, response) => {
if (err) {
reject(err)
} else {
resolve(response)
}
})
})
}
/**
* Sends a command to mogile
*
* @param {String} domain The storage domain
* @param {String} cmd The command to send
* @param {Object} args The command arguments
* @param {Function} callback Function to call when the operation is complete
* @return {Function}
*/
Mogile.prototype.send = function(domain, cmd, args, callback) {
args = args || {}
callback = callback || noop
args.domain = domain
const command = `${cmd} ${querystring.stringify(args)}\n`
let tries = 0
const sendf = () => {
this.sendCommand(command, (err, results) => {
if (err) {
if (++tries > this.retries) {
// Mark the tracker dead
return callback(err)
} else {
return sendf()
}
} else {
// All responses should start with OK or ERR, followed by a space, and then some kind
// of message. The message will be formatted as a URL query string, without any spaces.
const parts = results.split(' ')
// Having fewer than 2 parts is some kind of communications error, since the tracker
// will always return 2 string separated by a space.
if (parts.length !== 2) {
return callback(new Error(`Got invalid response from tracker: ${results}`))
}
// Responses starting with ERR are errors returned by the tracker. For instance
// if the key is unknown.
if (parts[0] === 'ERR') {
return callback(parts[1])
}
return callback(null, querystring.parse(parts[1].replace('\r\n', '')))
}
})
}
sendf()
}
Mogile.prototype.sendCommand = function(cmd, callback) {
const trackers = this.getLiveTrackers()
callback = callback || noop
if (!trackers.length) {
callback(new Error('No live trackers found'))
}
let i = 0
const sendf = () => {
this.current_tracker = trackers[i]
const connection = net.createConnection(this.current_tracker.getPort(), this.current_tracker.getHost())
connection.setEncoding(this.encoding)
connection.on('error', (err) => {
i++
if (i === this.trackers.length) {
callback(err)
} else {
sendf()
}
})
connection.on('connect', () => {
connection.write(cmd, this.encoding, () => {
connection.on('data', (response) => {
connection.end()
callback(null, response)
})
})
})
connection.setTimeout(900000)
connection.on('timeout', () => {
connection.end()
process.exit(0)
})
}
sendf()
}
/**
* Returns all the trackers in the alive state
*
* @return {Array}
*/
Mogile.prototype.getLiveTrackers = function() {
const live_trackers = []
for (let i = 0; i < this.trackers.length; i++) {
if (this.trackers[i].isAlive()) {
live_trackers.push(this.trackers[i])
}
}
return live_trackers
}
// Export the Mogile class
module.exports = Mogile