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
class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener):
''' web online plugin '''
'''Interactive report plugin '''
SECTION = "web"
@staticmethod
@ -77,7 +77,7 @@ class OnlineReportPlugin(AbstractPlugin, Thread, AggregateResultListener):
address = socket.gethostname()
self.log.info("Starting local HTTP server for online view at port: http://%s:%s/", address, self.port)
self.server.serve()
self.server.send({'reload': True})
self.server.reload()
def aggregate_second(self, data):

View File

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

View File

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

View File

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