-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclientAPI.js
More file actions
167 lines (149 loc) · 5.37 KB
/
clientAPI.js
File metadata and controls
167 lines (149 loc) · 5.37 KB
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
var things = require('things-js');
const uuidv1 = require('uuid/v1'); //Timestamp
const display_debug_messages = true;
function DFS(pubsubURL){
this.pubsub = new things.Pubsub(pubsubURL);
}
DFS.prototype.constructor = DFS;
DFS.prototype.readFile = function (path, arg1, arg2){
var self = this;
var requestID = uuidv1();
var options, callback;
if (typeof arg1 === 'function') {
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function')
{
options = arg1;
callback = arg2;
}
else
{
throw 'readFile(path, [options], callback): Invalid parameters'
}
var metadataRequest = {id: requestID, file: path, type: 'read'};
debug("metadata:", metadataRequest);
//obtain metadata from master
// The master node will use this channel to return metadata for this request
self.pubsub.subscribe(requestID, function(response){
var readRequest = {id: requestID, type: 'read', file: path, sender: 'clientAPI'};
if (response.sender == 'master')
{
debug('master response:', response);
var contactNodeTopic = "'" + response.contactNodeID + "'";
//communicate with contact node
self.pubsub.publish(contactNodeTopic, readRequest)
.then(function(outcome){
if (outcome === true){
debug('published to data node successfully',
JSON.stringify(response.contactNodeID));
}
else console.log('ERROR publishing to data node');
})
}
else if(response.sender === 'dataNode')
{
if (response.status == 'alternativeNode')
{
// The node's copy is corrupted, resend the request to alternative node.
var contactNodeTopic = "'" + response.altNodeID + "'";
//communicate with contact node
self.pubsub.publish(contactNodeTopic, readRequest);
}
else
{
debug('dataNode response:', response);
callback(response.status, response.data);
self.pubsub.unsubscribe(requestID);
}
}
})
.then(function(){
//Trigger metadata request to master
self.pubsub.publish('metadata', metadataRequest);
})
}
DFS.prototype.appendFile = function appendFile(path, data, arg1, arg2)
{
var self = this;
var requestID = uuidv1();
var options, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
options = arg1;
callback = arg2;
}
else {
throw "appendFile(path, data[,options], callback): You need to provide a callback function";
}
self.pubsub.subscribe(requestID, function(response){
if (response.sender == 'master')
{
debug('master response:', response);
var appendRequest =
{id: requestID, type: 'append', file: path, data: data, sender: 'clientAPI'};
var contactNodeTopic = "'" + response.contactNodeID + "'";
//communicate with contact node
self.pubsub.publish(contactNodeTopic, appendRequest);
}
else if (response.sender == 'dataNode')
{
debug('dataNode response:', response);
callback(response.status, response.data);
self.pubsub.unsubscribe(self.requestID);
}
})
.then(function(){
var metadataRequest = {id: requestID, file: path, type: 'append'};
self.pubsub.publish('metadata', metadataRequest);
})
}
DFS.prototype.writeFile = function writeFile(path, data, arg1, arg2)
{
var self = this;
var requestID = uuidv1();
var options, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
options = arg1;
callback = arg2;
}
else {
throw "writeFile(path, data[,options], callback): You need to provide a callback function";
}
self.pubsub.subscribe(requestID, function(response){
if (response.sender == 'master')
{
debug('master response:', response);
var writeRequest =
{id: requestID, type: 'write', file: path, data: data, sender: 'clientAPI'};
var contactNodeTopic = "'" + response.contactNodeID + "'";
//communicate with contact node
self.pubsub.publish(contactNodeTopic, writeRequest);
}
else if (response.sender == 'dataNode')
{
debug('dataNode response:', response);
callback(response.status, response.data);
self.pubsub.unsubscribe(self.requestID);
}
})
.then(function(){
var metadataRequest = {id: requestID, file: path, type: 'write'};
self.pubsub.publish('metadata', metadataRequest);
})
}
function debug(...args)
{
if (display_debug_messages)
{
var length = args.length;
for (var i=0; i< length; i++)
console.log(args[i]);
}
}
module.exports = DFS;