multiple clients

This commit is contained in:
Alexey Lavrenuke 2014-10-14 19:44:23 +04:00
parent 3de3945c62
commit 7e9d6a6a7b
4 changed files with 37 additions and 22 deletions

View File

@ -18,7 +18,7 @@ from decode import decode_aggregate, decode_monitoring
from cache import DataCacher from cache import DataCacher
class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener): class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener):
''' web online plugin ''' '''Interactive report plugin '''
SECTION = "web" SECTION = "web"
@staticmethod @staticmethod
@ -77,7 +77,7 @@ class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener):
address = socket.gethostname() address = socket.gethostname()
self.log.info("Starting local HTTP server for online view at port: http://%s:%s/", address, self.port) self.log.info("Starting local HTTP server for online view at port: http://%s:%s/", address, self.port)
self.server.serve() self.server.serve()
self.server.send({'reload': True}) self.server.reload()
def aggregate_second(self, data): def aggregate_second(self, data):

View File

@ -10,35 +10,39 @@ from tornado import template
from pyjade.ext.tornado import patch_tornado from pyjade.ext.tornado import patch_tornado
patch_tornado() patch_tornado()
from tornadio2 import SocketConnection, TornadioRouter, SocketServer from tornadio2 import SocketConnection, TornadioRouter, SocketServer, event
from threading import Thread from threading import Thread
class Client(SocketConnection): class Client(SocketConnection):
CONNECTION = None CONNECTIONS = set()
def on_open(self, info): def on_open(self, info):
print 'Client connected' print 'Client connected', self
Client.CONNECTION = self self.CONNECTIONS.add(self)
def on_message(self, msg): def on_message(self, msg):
print 'Got', msg print 'Got', msg
def on_close(self): def on_close(self):
print 'Client disconnected' print 'Client disconnected'
Client.CONNECTION = None self.CONNECTIONS.remove(self)
@event('heartbeat')
def on_heartbeat(self):
pass
class MainHandler(tornado.web.RequestHandler): class MainHandler(tornado.web.RequestHandler):
cacher = None def initialize(self, template, reportUUID, cacher):
def initialize(self, template, reportUUID):
self.template = template self.template = template
self.reportUUID = reportUUID self.reportUUID = reportUUID
self.cacher = cacher
def get(self): def get(self):
if MainHandler.cacher is not None: if self.cacher is not None:
cached_data = { cached_data = {
'data': MainHandler.cacher.get_all_data(), 'data': self.cacher.get_all_data(),
'uuid': self.reportUUID, 'uuid': self.reportUUID,
} }
else: else:
@ -54,15 +58,14 @@ class ReportServer(object):
self.reportUUID = uuid.uuid4().hex self.reportUUID = uuid.uuid4().hex
self.app = tornado.web.Application( self.app = tornado.web.Application(
router.apply_routes([ router.apply_routes([
(r"/", MainHandler, dict(template='index.jade', reportUUID=self.reportUUID)), (r"/", MainHandler, dict(template='index.jade', reportUUID=self.reportUUID, cacher=cacher)),
(r"/brief\.html$", MainHandler, dict(template='brief.jade', reportUUID=self.reportUUID)), (r"/brief\.html$", MainHandler, dict(template='brief.jade', reportUUID=self.reportUUID, cacher=cacher)),
(r"/monitoring\.html$", MainHandler, dict(template='monitoring.jade', reportUUID=self.reportUUID)), (r"/monitoring\.html$", MainHandler, dict(template='monitoring.jade', reportUUID=self.reportUUID, cacher=cacher)),
]), ]),
template_path=os.path.join(os.path.dirname(__file__), "templates"), template_path=os.path.join(os.path.dirname(__file__), "templates"),
static_path=os.path.join(os.path.dirname(__file__), "static"), static_path=os.path.join(os.path.dirname(__file__), "static"),
debug=True, debug=True,
) )
MainHandler.cacher = cacher
def serve(self): def serve(self):
def run_server(): def run_server():
@ -71,9 +74,13 @@ class ReportServer(object):
th.start() th.start()
def send(self, data): def send(self, data):
if Client.CONNECTION is not None: for connection in Client.CONNECTIONS:
data['uuid'] = self.reportUUID data['uuid'] = self.reportUUID
Client.CONNECTION.send(json.dumps(data)) connection.send(json.dumps(data))
def reload(self):
for connection in Client.CONNECTIONS:
connection.emit('reload')
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -85,17 +85,20 @@ app.controller "TankReport", ($scope, $element) ->
conn.on 'connect', () => conn.on 'connect', () =>
console.log("Connection opened...") console.log("Connection opened...")
$scope.status = "Connected" $scope.status = "Connected"
setInterval((
() ->conn.emit('heartbeat')
), 3000)
$scope.$apply() $scope.$apply()
conn.on 'disconnect', () => conn.on 'disconnect', () =>
console.log("Connection closed...") console.log("Connection closed...")
$scope.status = "Disonnected" $scope.status = "Disonnected"
$scope.$apply() $scope.$apply()
conn.on 'reload', () =>
location.reload(true)
conn.on 'message', (msg) => conn.on 'message', (msg) =>
tankData = JSON.parse msg tankData = JSON.parse msg
if tankData.uuid and $scope.uuid != tankData.uuid if tankData.uuid and $scope.uuid != tankData.uuid
location.reload(true) location.reload(true)
if tankData.reload
location.reload(true)
else else
$scope.updateData(tankData.data) $scope.updateData(tankData.data)

View File

@ -155,6 +155,9 @@
return function() { return function() {
console.log("Connection opened..."); console.log("Connection opened...");
$scope.status = "Connected"; $scope.status = "Connected";
setInterval((function() {
return conn.emit('heartbeat');
}), 3000);
return $scope.$apply(); return $scope.$apply();
}; };
})(this)); })(this));
@ -165,14 +168,16 @@
return $scope.$apply(); return $scope.$apply();
}; };
})(this)); })(this));
conn.on('reload', (function(_this) {
return function() {
return location.reload(true);
};
})(this));
return conn.on('message', (function(_this) { return conn.on('message', (function(_this) {
return function(msg) { return function(msg) {
var tankData; var tankData;
tankData = JSON.parse(msg); tankData = JSON.parse(msg);
if (tankData.uuid && $scope.uuid !== tankData.uuid) { if (tankData.uuid && $scope.uuid !== tankData.uuid) {
location.reload(true);
}
if (tankData.reload) {
return location.reload(true); return location.reload(true);
} else { } else {
return $scope.updateData(tankData.data); return $scope.updateData(tankData.data);