Matteo Scandolo | bd13aab | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | |
| 22 | const socketIo = require('./websocket.js'); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 23 | const config = require('../config/config.js'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 24 | const logger = require('../config/logger.js'); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 25 | const request = require('superagent'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 26 | const socket = socketIo.get(); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 27 | const _ = require('lodash'); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 28 | |
| 29 | var redis = require('redis'); |
Matteo Scandolo | 292bd62 | 2017-01-17 12:07:34 -0800 | [diff] [blame] | 30 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 31 | var client = redis.createClient({ |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 32 | host: config.redis.host, |
| 33 | port: config.redis.port |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 34 | }); |
| 35 | |
| 36 | client.on('error', function (err) { |
| 37 | logger.log('error', err); |
| 38 | }); |
| 39 | |
| 40 | client.on('ready', function () { |
| 41 | logger.log('info', 'Redis connected'); |
| 42 | }); |
| 43 | |
| 44 | client.on('subscribe', function (channel) { |
| 45 | logger.log('debug', `Subscribed to channel: ${channel}`); |
| 46 | }); |
| 47 | |
Matteo Scandolo | 292bd62 | 2017-01-17 12:07:34 -0800 | [diff] [blame] | 48 | client.on('pmessage', function (pattern, channel, message) { |
Matteo Scandolo | a263b67 | 2017-11-20 15:49:55 -0800 | [diff] [blame] | 49 | if (channel === 'Diag') { |
| 50 | return; |
| 51 | } |
| 52 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 53 | |
| 54 | let msg; |
| 55 | try { |
| 56 | msg = JSON.parse(message); |
| 57 | // TODO find the user that needs to be notified for msg.object update |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 58 | } |
| 59 | catch(e) { |
| 60 | // send the event also if it is not JSON |
| 61 | msg = message; |
Matteo Scandolo | a263b67 | 2017-11-20 15:49:55 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (msg.deleted) { |
| 65 | logger.log('warn', 'Remove on: ' + channel + ': ' + message); |
| 66 | socket.emit('remove', {model: channel, msg: msg, deleted: true}); |
| 67 | } |
| 68 | else { |
| 69 | logger.log('warn', 'Update on: ' + channel + ': ' + message); |
| 70 | socket.emit('update', {model: channel, msg: msg}); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 71 | } |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 72 | }); |
| 73 | |
Matteo Scandolo | 292bd62 | 2017-01-17 12:07:34 -0800 | [diff] [blame] | 74 | // subscribe to all channels |
| 75 | client.psubscribe('*'); |
Matteo Scandolo | 606751e | 2017-01-13 13:07:02 -0800 | [diff] [blame] | 76 | |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 77 | })(); |