This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
293 lines (233 loc) · 8.88 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Modules
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const cors = require('cors');
const WebSocketServer = require('ws').Server;
const router = express.Router();
const path = require('path');
// JSON files
const output_json = require('./data/output-gate-json.json');
const endpoints = require('./data/endpoints.json');
const sampleRound = require('./data/sample-round.json');
// initialize API and start listening for requests
var app = express();
app.use(cors());
app.use(bodyParser.json());
app.use('/', router);
app.use(express.static(__dirname));
app.use(express.static(__dirname + '/styles/stylesheet.css'));
app.use(express.static(__dirname + '/scripts/functions.js'));
app.listen(process.env.port || 2500);
console.log("Express server listening on port 2500");
// Initialize the websocket server
var sock = new WebSocketServer({ port: 2501 });
/*
Description: Reply to the user with the agents response.
Input: string - agent response JSON
Output: none.
Effects: send json object containing sender and message through websocket
to the browser.
*/
sock.broadcast = function broadcast(data) {
// sends the message to all users
sock.clients.forEach(function each(client) {
if(client.readyState === 1 ) {
// send the message/sender to the client
client.send(JSON.stringify(data));
}
});
};
// Manage the connection of a new client
sock.on('connection', function connection(client) {
console.log("CONNECTION OK...");
// if the client sends a message, or if the round button was clicked
client.on('message', function incoming(info) {
data = JSON.parse(info);
// clause for setting the ingredients display with the most current ingredients
if(data.purpose == "updateIngredients") {
request.get(endpoints.env_orch + "/viewTotals", (error, res, body) => {
var info = JSON.parse(body);
var message = {purpose: "updateIngredients", data: info};
sock.broadcast(message);
});
// If the start button was clicked
} else if(data.purpose == "newRound") {
// gather all utility data
var new_round = sampleRound;
var celia_data, watson_data, human_data;
// reset both info displays with null information
var message = {purpose: "roundTotal", roundTotal: true, newRound: true};
sock.broadcast(message);
// set duration data
new_round.durations.round = parseInt(data.data.start);
new_round.durations.post = parseInt(data.data.end);
new_round.durations.warmUp = parseInt(data.data.pre);
console.log(new_round);
request.get(endpoints.utility_generator + "/generateUtility/seller", (error, res, body) => {
celia_data = JSON.parse(body);
// Set Celia's round information
new_round.agents[0].utilityFunction = celia_data;
new_round.agents[0].protocol = endpoints.celia.protocol;
new_round.agents[0].host = endpoints.celia.host;
new_round.agents[0].port = endpoints.celia.port;
// Get the agent utility data from the utility generator
request.get(endpoints.utility_generator + "/generateUtility/seller", (error, res, body) => {
// Parse the string data into a JSON object
watson_data = JSON.parse(body);
// Set Watson's round information
new_round.agents[1].utilityFunction = watson_data;
new_round.agents[1].protocol = endpoints.watson.protocol;
new_round.agents[1].host = endpoints.watson.host;
new_round.agents[1].port = endpoints.watson.port;
// Get the human utility data
request.get(endpoints.utility_generator + "/generateUtility/buyer", (error, res, body) => {
// Parse the string data into a JSON object
human_data = JSON.parse(body);
// Set the Human's round information
new_round.human.utilityFunction = human_data;
// send /startRound request with new json
request.post(endpoints.env_orch + "/startRound", {
// formatted JSON object
json: new_round
// Error handler for POST request
}, (error, res) => {
if (error) {
console.error(error);
return;
}
console.log(`statusCode: ${res.statusCode}`);
});
});
});
});
// If just a message to the agents, follow here
} else if(data.purpose == "message") {
// gather format for new message
var message = output_json;
// try and set the agents name, if given
const lower_transcript = data.data.toLowerCase();
if (lower_transcript.startsWith('watson') || lower_transcript.startsWith('@watson')) {
message.addressee = 'Watson';
}
else if (lower_transcript.startsWith('celia') || lower_transcript.startsWith('@celia')) {
message.addressee = 'Celia';
}
else {
message.addressee = '';
}
// set all other json data
message.speaker = "Human";
message.text = data.data;
message.timestamp = Date.now();
message.role = data.role;
console.log(message);
// HTTP post request to send user message.
request.post(endpoints.env_orch + endpoints.output, {
// formatted JSON object
json: message
// Error handler for POST request
}, (error, res) => {
if (error) {
console.error(error);
return;
}
console.log(`statusCode: ${res.statusCode}`);
});
}
});
});
// Display index.html on http://localhost:2500/
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'index.html'));
});
/*
Description: Send the agent response to be displayed.
Input: JSON - example found in README.
Output: JSON - {“msgType” = “submitTranscript”,“Status” = “OK”}, sent to sender
Effects: display agent message to user
*/
app.post(endpoints.input, function(req, res) {
console.log("received message");
var json_content = req.body;
if(json_content.speaker === "Human") {
var json = {msgType: 'submitTranscript', Status: 'OK'};
res.send(json);
return;
}
// Send to broadcast method for displaying in UI
sock.broadcast(json_content);
// send 'ack'
var json = {msgType: 'submitTranscript', Status: 'OK'};
res.send(json);
});
/*
Description: Display results of the round.
Input: JSON object
Output: JSON - {“msgType” = “submitTranscript”,“Status” = “OK”}, sent to sender
Effects: display round results to user
*/
app.post("/receiveRoundTotals", function(req, res) {
// collect and organize JSON data into separate variables
var json_content = req.body;
// Grab celias utility
var celiaUtility = json_content.roundTotals.Celia;
var message = {purpose: "roundTotal", roundTotal: true, newRound: false, id: "Celia", data: celiaUtility};
sock.broadcast(message);
// Grab watsons utility
var watsonUtility = json_content.roundTotals.Watson;
var message = {purpose: "roundTotal", roundTotal: true, newRound: false, id: "Watson", data: watsonUtility};
sock.broadcast(message);
// Grab the human utility
var humanUtility = json_content.roundTotals.Human;
var message = {purpose: "roundTotal", newRound: false, id: "Human", data: humanUtility};
sock.broadcast(message);
// send 'ack'
var json = {Status: 'OK'};
res.send(json);
});
// Dummy endpoints for E.O.
app.post('/receiveRejection', (_, res) => res.json({status: 'Acknowledged'}));
app.post('/startRound', (_, res) => res.json({status: 'Acknowledged'}));
app.post('/endRound', (_, res) => res.json({status: 'Acknowledged'}));
app.post('/setUtility', (_, res) => res.json({status: 'Acknowledged'}));
app.post('/sendRoundMetadata', (_, res) => res.json({status: 'Acknowledged'}));
// Psuedo routing api for communication between user and agents
var dummyRoute = express();
dummyRoute.use(cors());
dummyRoute.use(bodyParser.json());
dummyRoute.listen(3500);
// Redirect user/agent messages through dummyRoute and toward destination
dummyRoute.post(endpoints.output, function(req, res) {
var json_content = req.body;
// message from user to agents
if (json_content.speaker === "Human") {
request.post(endpoints.agent_message + endpoints.input, {
// formatted JSON object
json: json_content
// Error handler for POST request
}, (error, res) => {
if (error) {
console.error(error);
return;
}
console.log(`statusCode: ${res.statusCode}`);
});
// message from agent to user
} else {
request.post(endpoints.chatUI_server + endpoints.input, {
// formatted JSON object
json: json_content
// Error handler for POST request
}, (error, res) => {
if (error) {
console.error(error);
return;
}
console.log(`statusCode: ${res.statusCode}`);
});
}
// send acknowledgment
var json = {Status: 'OK'};
res.send(json);
});