Illyoung Choi | 59820ed | 2019-06-24 17:01:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | (function () { |
| 18 | 'use strict'; |
| 19 | |
| 20 | const socketio = require('socket.io'); |
| 21 | const ioWildcard = require('socketio-wildcard'); |
| 22 | const client = require('../types/client.js'); |
| 23 | const eventrouter = require('./eventrouter.js'); |
| 24 | const logger = require('../config/logger.js'); |
| 25 | |
| 26 | let io; |
| 27 | const createSocketIO = (server) => { |
| 28 | // INSTANTIATE SOCKET.IO |
| 29 | io = socketio.listen(server); |
| 30 | io.use(ioWildcard()); |
| 31 | |
| 32 | // set io to eventrouter |
| 33 | //eventrouter.setIO(io); |
| 34 | |
| 35 | // LISTEN TO "CONNECTION" EVENT (FROM SOCKET.IO) |
| 36 | io.on('connection', (socket) => { |
| 37 | let query = socket.handshake.query; |
| 38 | logger.log('debug', `connect ${JSON.stringify(query)}`); |
| 39 | let added = false; |
| 40 | |
| 41 | // make a client |
| 42 | let c = client.Client.fromObj(query); |
| 43 | c.setSocket(socket); |
| 44 | |
| 45 | if(!c.validate()) { |
| 46 | logger.log('warn', `client validation failed - ${JSON.stringify(query)}`); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // register the client for management |
| 51 | if(eventrouter.addClient(c)) { |
| 52 | // Send a greeting message to the client |
| 53 | socket.emit(eventrouter.serviceEvents.GREETING, { |
| 54 | to: c.getId(), |
| 55 | message: 'Welcome to CORD Workflow Control Service' |
| 56 | }); |
| 57 | |
| 58 | added = true; |
| 59 | } |
| 60 | else { |
| 61 | logger.log('warn', `client could not be added - ${JSON.stringify(query)}`); |
| 62 | socket.disconnect(true); |
| 63 | } |
| 64 | |
| 65 | // set a disconnect event handler |
| 66 | socket.on('disconnect', (reason) => { |
| 67 | logger.log('debug', `disconnect ${reason} ${JSON.stringify(query)}`); |
| 68 | if(added) { |
| 69 | eventrouter.removeClient(c.getId()); |
| 70 | } |
| 71 | }); |
| 72 | }); |
| 73 | }; |
| 74 | |
| 75 | const destroySocketIO = () => { |
| 76 | io.close(); |
| 77 | }; |
| 78 | |
| 79 | const getSocketIO = () => io; |
| 80 | |
| 81 | module.exports = { |
| 82 | create: createSocketIO, |
| 83 | destroy: destroySocketIO, |
| 84 | get: getSocketIO |
| 85 | }; |
| 86 | |
| 87 | // USAGE |
| 88 | // const socketIo = require('./controllers/websocket.js'); |
| 89 | // const socket = socketIo.get(); |
| 90 | // socket.emit('eventName', data); |
| 91 | |
| 92 | })(); |