-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventHandler.js
More file actions
130 lines (100 loc) · 3.37 KB
/
Copy patheventHandler.js
File metadata and controls
130 lines (100 loc) · 3.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
import debug from './debug';
import {DEV} from './header';
import {FILE_TABLE} from './fileTable';
import {broadcast} from './connection';
import {diffFileTable} from './utility';
import { config } from './config';
import { Actions } from './actions';
import { _map } from './code/map-reduce';
var fs = require('fs');
export class EventHandler {
constructor(){
}
static handleEvent(data){
switch (data.event)
{
case 'DOWN_FILE': debug.log(event + ' handled', DEV);
var s_addr = 'http://192.168.1.2:3000/';
socket = io_client(s_addr);
var delivery = dl.listen(socket);
delivery.on('receive.success',function(file){
fs.writeFile(PATH+file.name, file.buffer, function(err){
if(err){
console.log('File could not be saved: ' + err);
}else{
console.log('File ' + file.name + " saved");
};
});
});
break;
case 'FORCE_UPDATE': debug.log(data.event + ' handled', DEV);
break;
case 'SEND_FILE_TABLE':
Actions.sendFileTable(data.ip);
debug.log(data.event + ' handled', DEV);
break;
case 'UPDATE_LOCAL_FILE_TABLE':
debug.log(data.event + ' broadcasted', DEV);
break;
case 'UPDATE_LOCAL_FILE': debug.log(data.event + ' broadcasted', DEV);
break;
case 'UPDATE_FILE_TBL':
console.log("Received File Table");
diffFileTable(data.fileTable);
debug.log(data.event + ' handled', DEV);
break;
case 'EXECUTE':
fs.writeFile("./code/map-reduce.js", data.code, function(err) {
if(err) {
return console.log(err);
}
console.log("Job Completed (Result): ", _map());
});
debug.log(data.event + ' handled', DEV);
break;
default: debug.log(event + ' handled', DEV);
}
}
static broadcastEvent(event, obj){
switch (event)
{
case 'DOWN_FILE': debug.log(event + ' broadcasted', DEV);
break;
case 'REQ_FILE_TABLE': debug.log(event + ' broadcasted', DEV);
broadcast('event', {event:'SEND_FILE_TABLE', ip: config.my_addr})
break;
case 'FORCE_UPDATE': debug.log(event + ' broadcasted', DEV);
break;
case 'EXECUTE':
fs.readFile('./code/map-reduce.js', 'utf8', function(err, data) {
if (err) throw err;
broadcast('event', {event:'EXECUTE' ,source: config.my_addr, code: data});
});
debug.log(event + ' broadcasted', DEV);
break;
case 'BRDCST_FILE_TBL':
broadcast('event', {event:'UPDATE_FILE_TBL' ,fileTable: FILE_TABLE.GLOBAL});
debug.log(event + ' broadcasted', DEV);
break;
case 'USR_MSG':
broadcast('message', obj.message);
debug.log(event + ' broadcasted', DEV);
break;
default: debug.log(event + ' broadcasted', DEV);
}
}
updateLocalFile(obj){
var doesExist = false;
var local_file = {
F_ID : obj.F_ID,
F_NAME : obj.FNAME,
FS_DWNLD_TIME : obj.FS_DWNLD_TIME,
}
FILE_TABLE.LOCAL.map((file) => {
if(file.F_NAME == obj.F_NAME){
file.FS_DWNLD_TIME = obj.FS_DWNLD_TIME;
}
});
FILE_TABLE.LOCAL.push(local_file);
}
}