Implement basic functionalities for workflow control.
- Manage join/leave of clients
- All clients communicate via socket.io
- Probes emit events
- Managers register workflows (by using a workflow essence)
- Send kickstart request to Managers to launch workflows
- Route events to workflow runs
- Queue events to not lose events between workflow tasks
- Fixed some issues found while working on testcases
- Set to perform coverage and unittest and generate outputs to files
Change-Id: I678723edc20df9247d63a4bf6380785ab8b2b221
diff --git a/src/controllers/websocket.js b/src/controllers/websocket.js
new file mode 100644
index 0000000..3f3216a
--- /dev/null
+++ b/src/controllers/websocket.js
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2019-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+(function () {
+ 'use strict';
+
+ const socketio = require('socket.io');
+ const ioWildcard = require('socketio-wildcard');
+ const client = require('../types/client.js');
+ const eventrouter = require('./eventrouter.js');
+ const logger = require('../config/logger.js');
+
+ let io;
+ const createSocketIO = (server) => {
+ // INSTANTIATE SOCKET.IO
+ io = socketio.listen(server);
+ io.use(ioWildcard());
+
+ // set io to eventrouter
+ //eventrouter.setIO(io);
+
+ // LISTEN TO "CONNECTION" EVENT (FROM SOCKET.IO)
+ io.on('connection', (socket) => {
+ let query = socket.handshake.query;
+ logger.log('debug', `connect ${JSON.stringify(query)}`);
+ let added = false;
+
+ // make a client
+ let c = client.Client.fromObj(query);
+ c.setSocket(socket);
+
+ if(!c.validate()) {
+ logger.log('warn', `client validation failed - ${JSON.stringify(query)}`);
+ return;
+ }
+
+ // register the client for management
+ if(eventrouter.addClient(c)) {
+ // Send a greeting message to the client
+ socket.emit(eventrouter.serviceEvents.GREETING, {
+ to: c.getId(),
+ message: 'Welcome to CORD Workflow Control Service'
+ });
+
+ added = true;
+ }
+ else {
+ logger.log('warn', `client could not be added - ${JSON.stringify(query)}`);
+ socket.disconnect(true);
+ }
+
+ // set a disconnect event handler
+ socket.on('disconnect', (reason) => {
+ logger.log('debug', `disconnect ${reason} ${JSON.stringify(query)}`);
+ if(added) {
+ eventrouter.removeClient(c.getId());
+ }
+ });
+ });
+ };
+
+ const destroySocketIO = () => {
+ io.close();
+ };
+
+ const getSocketIO = () => io;
+
+ module.exports = {
+ create: createSocketIO,
+ destroy: destroySocketIO,
+ get: getSocketIO
+ };
+
+ // USAGE
+ // const socketIo = require('./controllers/websocket.js');
+ // const socket = socketIo.get();
+ // socket.emit('eventName', data);
+
+})();
\ No newline at end of file