2017-02-11 19:23:30 +00:00
|
|
|
# use unicode encoding for all literals by default (for python2.x)
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-02-11 19:06:12 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015-2017, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
|
|
|
|
|
|
|
"""
|
|
|
|
This file is part of transWhat
|
|
|
|
|
|
|
|
transWhat 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
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
transwhat 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 transWhat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
import asyncore, socket
|
2015-02-27 16:06:30 +00:00
|
|
|
import logging
|
2015-09-03 18:04:29 +00:00
|
|
|
import sys
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
class IOChannel(asyncore.dispatcher):
|
2015-09-03 18:04:29 +00:00
|
|
|
def __init__(self, host, port, callback, closeCallback):
|
2013-05-31 18:53:21 +00:00
|
|
|
asyncore.dispatcher.__init__(self)
|
|
|
|
|
|
|
|
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.connect((host, port))
|
2015-09-03 18:04:29 +00:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
self.callback = callback
|
2015-09-03 18:04:29 +00:00
|
|
|
self.closeCallback = closeCallback
|
2016-04-10 20:48:04 +00:00
|
|
|
self.buffer = bytes("")
|
2013-05-31 18:53:21 +00:00
|
|
|
|
|
|
|
def sendData(self, data):
|
|
|
|
self.buffer += data
|
|
|
|
|
|
|
|
def handle_connect(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def handle_close(self):
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def handle_read(self):
|
|
|
|
data = self.recv(65536)
|
|
|
|
self.callback(data)
|
|
|
|
|
|
|
|
def handle_write(self):
|
|
|
|
sent = self.send(self.buffer)
|
|
|
|
self.buffer = self.buffer[sent:]
|
|
|
|
|
2015-09-03 18:04:29 +00:00
|
|
|
def handle_close(self):
|
|
|
|
self.logger.info('Connection to backend closed, terminating.')
|
|
|
|
self.close()
|
|
|
|
self.closeCallback()
|
|
|
|
|
2013-05-31 18:53:21 +00:00
|
|
|
def writable(self):
|
|
|
|
return (len(self.buffer) > 0)
|
|
|
|
|
|
|
|
def readable(self):
|
|
|
|
return True
|