blob: 7e86000aa1d1ba2b757fbfc38d7c89396f7b079e [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
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 Scandoloe3ed0162016-12-01 10:09:12 -080019(function () {
20 'use strict';
21
22 const socketIo = require('./websocket.js');
Matteo Scandolo606751e2017-01-13 13:07:02 -080023 const config = require('../config/config.js');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080024 const logger = require('../config/logger.js');
Matteo Scandolo606751e2017-01-13 13:07:02 -080025 const request = require('superagent');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080026 const socket = socketIo.get();
Matteo Scandolo606751e2017-01-13 13:07:02 -080027 const _ = require('lodash');
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080028
29 var redis = require('redis');
Matteo Scandolo292bd622017-01-17 12:07:34 -080030
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080031 var client = redis.createClient({
Matteo Scandolo606751e2017-01-13 13:07:02 -080032 host: config.redis.host,
33 port: config.redis.port
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080034 });
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 Scandolo292bd622017-01-17 12:07:34 -080048 client.on('pmessage', function (pattern, channel, message) {
Matteo Scandoloa263b672017-11-20 15:49:55 -080049 if (channel === 'Diag') {
50 return;
51 }
52
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080053
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 Scandoloe3ed0162016-12-01 10:09:12 -080058 }
59 catch(e) {
60 // send the event also if it is not JSON
61 msg = message;
Matteo Scandoloa263b672017-11-20 15:49:55 -080062 }
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 Scandoloe3ed0162016-12-01 10:09:12 -080071 }
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080072 });
73
Matteo Scandolo292bd622017-01-17 12:07:34 -080074 // subscribe to all channels
75 client.psubscribe('*');
Matteo Scandolo606751e2017-01-13 13:07:02 -080076
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080077})();