2017-10-07 15:10:31 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Safe Eyes is a utility to remind you to take break frequently
|
|
|
|
# to protect your eyes from eye strain.
|
|
|
|
|
|
|
|
# Copyright (C) 2017 Gobinath
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
RPC server and client implementation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from threading import Thread
|
|
|
|
from xmlrpc.server import SimpleXMLRPCServer
|
|
|
|
from xmlrpc.client import ServerProxy
|
|
|
|
|
2017-11-11 16:53:24 +01:00
|
|
|
|
2020-03-18 13:33:11 +01:00
|
|
|
class RPCServer:
|
2017-10-07 15:10:31 +02:00
|
|
|
"""
|
|
|
|
An aynchronous RPC server.
|
|
|
|
"""
|
|
|
|
def __init__(self, port, context):
|
|
|
|
self.__running = False
|
|
|
|
logging.info('Setting up an RPC server on port %d', port)
|
|
|
|
self.__server = SimpleXMLRPCServer(("localhost", port), logRequests=False, allow_none=True)
|
2017-10-10 16:14:23 +02:00
|
|
|
self.__server.register_function(context['api']['show_settings'], 'show_settings')
|
|
|
|
self.__server.register_function(context['api']['show_about'], 'show_about')
|
|
|
|
self.__server.register_function(context['api']['enable_safeeyes'], 'enable_safeeyes')
|
|
|
|
self.__server.register_function(context['api']['disable_safeeyes'], 'disable_safeeyes')
|
|
|
|
self.__server.register_function(context['api']['take_break'], 'take_break')
|
2018-01-29 02:16:02 +01:00
|
|
|
self.__server.register_function(context['api']['status'], 'status')
|
2017-10-10 16:14:23 +02:00
|
|
|
self.__server.register_function(context['api']['quit'], 'quit')
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""
|
|
|
|
Start the RPC server.
|
|
|
|
"""
|
|
|
|
if not self.__running:
|
|
|
|
self.__running = True
|
|
|
|
logging.info('Start the RPC server')
|
|
|
|
server_thread = Thread(target=self.__server.serve_forever)
|
|
|
|
server_thread.start()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""
|
|
|
|
Stop the server.
|
|
|
|
"""
|
|
|
|
if self.__running:
|
|
|
|
logging.info('Stop the RPC server')
|
|
|
|
self.__running = False
|
|
|
|
self.__server.shutdown()
|
|
|
|
|
2017-11-11 16:53:24 +01:00
|
|
|
|
2020-03-18 13:33:11 +01:00
|
|
|
class RPCClient:
|
2017-10-07 15:10:31 +02:00
|
|
|
"""
|
|
|
|
An RPC client to communicate with the RPC server.
|
|
|
|
"""
|
|
|
|
def __init__(self, port):
|
|
|
|
self.port = port
|
2017-10-12 21:54:00 +02:00
|
|
|
self.proxy = ServerProxy('http://localhost:%d/' % self.port, allow_none=True)
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def show_settings(self):
|
|
|
|
"""
|
|
|
|
Show the settings dialog.
|
|
|
|
"""
|
2017-10-12 21:54:00 +02:00
|
|
|
self.proxy.show_settings()
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def show_about(self):
|
|
|
|
"""
|
|
|
|
Show the about dialog.
|
|
|
|
"""
|
2017-10-12 21:54:00 +02:00
|
|
|
self.proxy.show_about()
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def enable_safeeyes(self):
|
|
|
|
"""
|
|
|
|
Enable Safe Eyes.
|
|
|
|
"""
|
2017-10-12 21:54:00 +02:00
|
|
|
self.proxy.enable_safeeyes()
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def disable_safeeyes(self):
|
|
|
|
"""
|
|
|
|
Disable Safe Eyes.
|
|
|
|
"""
|
2018-01-29 02:16:02 +01:00
|
|
|
self.proxy.disable_safeeyes(None)
|
2017-10-07 15:10:31 +02:00
|
|
|
|
|
|
|
def take_break(self):
|
|
|
|
"""
|
|
|
|
Take a break now.
|
|
|
|
"""
|
2017-10-12 21:54:00 +02:00
|
|
|
self.proxy.take_break()
|
2017-10-07 15:10:31 +02:00
|
|
|
|
2018-01-29 02:16:02 +01:00
|
|
|
def status(self):
|
|
|
|
"""
|
|
|
|
Return the status of Safe Eyes
|
|
|
|
"""
|
|
|
|
return self.proxy.status()
|
|
|
|
|
2017-10-07 15:10:31 +02:00
|
|
|
def quit(self):
|
|
|
|
"""
|
|
|
|
Quit Safe Eyes.
|
|
|
|
"""
|
2017-10-12 21:54:00 +02:00
|
|
|
self.proxy.quit()
|