[Openlanhouse-development] SF.net SVN: openlanhouse:[295] trunk/openlh-client/src/OpenlhClient
Status: Pre-Alpha
Brought to you by:
n3rd3x
From: <n3...@us...> - 2008-10-11 19:24:34
|
Revision: 295 http://openlanhouse.svn.sourceforge.net/openlanhouse/?rev=295&view=rev Author: n3rd3x Date: 2008-10-11 19:24:28 +0000 (Sat, 11 Oct 2008) Log Message: ----------- * removed OpenlhClient/network.py Modified Paths: -------------- trunk/openlh-client/src/OpenlhClient/Makefile.am trunk/openlh-client/src/OpenlhClient/__init__.py Removed Paths: ------------- trunk/openlh-client/src/OpenlhClient/network.py Modified: trunk/openlh-client/src/OpenlhClient/Makefile.am =================================================================== --- trunk/openlh-client/src/OpenlhClient/Makefile.am 2008-10-11 19:21:16 UTC (rev 294) +++ trunk/openlh-client/src/OpenlhClient/Makefile.am 2008-10-11 19:24:28 UTC (rev 295) @@ -7,7 +7,6 @@ login.py \ globals.py \ main.py \ - network.py \ utils.py \ xmlpickler.py \ config.py \ Modified: trunk/openlh-client/src/OpenlhClient/__init__.py =================================================================== --- trunk/openlh-client/src/OpenlhClient/__init__.py 2008-10-11 19:21:16 UTC (rev 294) +++ trunk/openlh-client/src/OpenlhClient/__init__.py 2008-10-11 19:24:28 UTC (rev 295) @@ -17,4 +17,4 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. __all__ = ('login', 'server_info', 'ui', 'config' - 'globals', 'utils', 'main', 'prefs', 'network', 'dbus_manager') + 'globals', 'utils', 'main', 'prefs', 'dbus_manager') Deleted: trunk/openlh-client/src/OpenlhClient/network.py =================================================================== --- trunk/openlh-client/src/OpenlhClient/network.py 2008-10-11 19:21:16 UTC (rev 294) +++ trunk/openlh-client/src/OpenlhClient/network.py 2008-10-11 19:24:28 UTC (rev 295) @@ -1,412 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# -# Copyright (C) 2008 Wilson Pinto Júnior (N3RD3X) <n3...@gu...> -# -# 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/>. - -import sys -import re -import gobject -import socket -import time -import logging -import traceback - -from threading import RLock, Lock -from os import path as ospath -from OpenlhClient import xmlpickler - -try: - import gnutls.crypto - import gnutls.connection - - HAS_GNUTLS = True -except: - HAS_GNUTLS = False - -class ResponseError(Exception): - pass - -class Response(gobject.GObject): - waiting = True - done = False - - __gsignals__ = {'done': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - (gobject.TYPE_PYOBJECT,)), - } - - def __init__(self, id): - self.__gobject_init__() - self.id = id - - def get_value(self): - if hasattr(self, 'value'): - return self.value - else: - raise ResponseError('Response is not done') - - def set_value(self, value): - self.value = value - self.done = True - self.waiting = False - self.emit('done', self.value) - -class Client(gobject.GObject): - - __gsignals__ = {'connected':(gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - (gobject.TYPE_STRING,)), - - 'disconnected':(gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - ()), - - } - - address_family = socket.AF_INET - socket_type = socket.SOCK_STREAM - - rbufsize = -1 - wbufsize = 0 - receive = False - handshake = True - current = {} - current['type'] = None - current['data'] = [] - currid = 1 - dispatch_func = None - recvfile_func = None - open_responses = {} - io_session_handler_id = 0 - - def __init__(self, server, port, cert, key, hash_id): - - self.__gobject_init__() - - self._server = server - self._port = port - - self.cert = gnutls.crypto.X509Certificate(open(cert).read()) - self.key = gnutls.crypto.X509PrivateKey(open(key).read()) - self.hash_id = hash_id - self.cred = gnutls.connection.X509Credentials(self.cert, self.key) - - self.socket = socket.socket(self.address_family, self.socket_type) - self.session = gnutls.connection.ClientSession(self.socket, self.cred) - self.logger = logging.getLogger('connection.server') - self.send_lock = RLock() - - def parse_headers(self, data): - - ##### RESPONSE ##### - - ### HEAD XMLRESPONSE - if data.startswith('-----BEGIN XMLRESPONSE'): - regex = re.compile(r'-----BEGIN XMLRESPONSE'+ - ' ID=(?P<id>\d+) SIZE=(?P<size>\d+)-----') - - try: - out = regex.match(data) - - if out: - self.current['id'] = int(out.group('id')) - self.current['size'] = int(out.group('size')) - self.current['type'] = 'XMLRESPONSE' - self.current['size_remaining'] = self.current['size'] - self.current['data'] = [] - - except Exception, error: - self.logger.error(error) - - ### END XMLRESPONSE - elif (self.current['type'] == 'XMLRESPONSE' and - data == '-----END XMLRESPONSE-----'): - - if self.current['size_remaining'] != 0: - self.logger.warning('size_remaining != 0') - - outdata = ''.join(self.current['data']) - - self.current['type'] = None - self.current['data'] = [] - - response = xmlpickler.loads(outdata)[0][0] - - if self.open_responses.has_key(self.current['id']): - self.open_responses[self.current['id']].set_value(response) - self.open_responses.pop(self.current['id']) - - ##### REQUEST ##### - - ### HEAD XMLREQUEST - elif data.startswith('-----BEGIN XMLREQUEST '): - - regex = re.compile(r'-----BEGIN XMLREQUEST ' + - 'ID=(?P<id>\d+) SIZE=(?P<size>\d+)-----') - - try: - out = regex.match(data) - - if out: - self.current['id'] = int(out.group('id')) - self.current['size'] = int(out.group('size')) - self.current['type'] = 'XMLREQUEST' - self.current['size_remaining'] = self.current['size'] - self.current['data'] = [] - - self.logger.debug('parsing head request, id:%d, size:%d' % - (self.current['id'], self.current['size'])) - - except Exception, error: - self.logger.error(error) - - ### END XMLREQUEST - elif (self.current['type'] == 'XMLREQUEST' and - data == '-----END XMLREQUEST-----'): - - self.logger.debug('done request, id:%d, size:%d' % - (self.current['id'], self.current['size'])) - - if self.current['size_remaining'] != 0: - self.logger.warning('size_remaining != 0') - - outdata = ''.join(self.current['data']) - self.current['type'] = None - self.current['data'] = [] - id = self.current['id'] - - params, method = xmlpickler.loads(outdata) - - if self.dispatch_func: - response = self.dispatch_func(method, params) - - else: - response = None - - self.send_response(id, response) - - ##SENDFILE - elif data.startswith('-----BEGIN SENDFILE '): - regex = re.compile(r'-----BEGIN SENDFILE ID=(?P<id>\d+) ' + - 'METHOD=(?P<method>[\w.]+) SIZE=(?P<size>\d+)-----') - - try: - out = regex.match(data) - - if out: - self.current['id'] = int(out.group('id')) - self.current['method'] = out.group('method') - self.current['size'] = int(out.group('size')) - self.current['type'] = 'SENDFILE' - self.current['size_remaining'] = self.current['size'] - self.current['data'] = [] - - except Exception, error: - self.logger.error(error) - - ### END SENDFILE - elif (self.current['type'] == 'SENDFILE' and - data == '-----END SENDFILE-----'): - - data = ''.join(self.current['data']) - method = self.current['method'] - self.current['data'] = [] - self.current['type'] = None - - self.logger.debug('done sent file, method:%s, size:%d' % - (self.current['method'], self.current['size'])) - - if self.current['size_remaining'] != 0: - self.logger.warning('size_remaining != 0') - - if self.recvfile_func and callable(self.recvfile_func): - self.recvfile_func(method, data) - - def parse_data(self, data): - ##JOIN DATA TODO: ADD OVERFLOOD CONTROL - if not self.current['type']: - self.logger.debug('error no type defined') - return - - self.current['data'].append(data) - self.current['size_remaining'] -= len(self.current['data'][-1]) - - def handler(self, session, *args): - try: - data = self.session.recv(1024) - - if data == 0 or data == '' or data == 'CLOSE': - raise SystemError('Disconnected from server') - - if data.startswith('-----') and data.endswith('-----'): - - try: - self.parse_headers(data) - except Exception, error: - traceback.print_exc() - self.logger.error(error) - - elif data: - self.parse_data(data) - - return True - - except Exception, error: - self.logger.error(error) - self.stop() - - return False - - def start(self): - try: - self.session.connect((self._server, self._port)) - - if self.handshake: - self.session.handshake() - - self.emit('connected', (self._server, self._port)) - - self.rfile = self.session.makefile('rb', self.rbufsize) - self.wfile = self.session.makefile('wb', self.wbufsize) - - self.send_hash_id() - self.io_session_handler_id = gobject.io_add_watch(self.session, - gobject.IO_IN, self.handler) - - return True - - except Exception, error: - try: - (code, msg) = error - except ValueError: - msg = str(error) - - self.logger.error(msg) - return False - - def stop(self): - self.logger.info('connection closed') - self.receive = False - - if self.io_session_handler_id: - gobject.source_remove(self.io_session_handler_id) - - try: - self.session.send('CLOSE') - except: - pass - - try: - self.session.shutdown() - except: - pass - - try: - self.session.close() - except: - pass - - try: - if not self.wfile.closed: - self.wfile.flush() - - self.wfile.close() - self.rfile.close() - - except: - pass - - self.emit('disconnected') - - def send_hash_id(self): - self.session.send('-----HASH_ID=%s-----' % self.hash_id) - - def request(self, method, params=()): - - if not isinstance(params, tuple): - params = (params,) - - xmlout = xmlpickler.dumps(params, method) - size = len(xmlout) - - head = "-----BEGIN XMLREQUEST ID=%d SIZE=%d-----" % (self.currid, size) - self.send_lock.acquire() - self.session.send(head) - self.session.send(xmlout) - self.session.send("-----END XMLREQUEST-----") - - self.send_lock.release() - - response = Response(self.currid) - self.open_responses[self.currid] = response - self.currid += 1 - - return response - - def send_response(self, id, response): - """ - Send Response to peer - """ - - self.logger.info('sending response number %d' % id) - - try: - output = xmlpickler.dumps((response,), methodresponse=1) - head = '-----BEGIN XMLRESPONSE ID=%d SIZE=%d-----' % (id, len(output)) - self.send_lock.acquire() - self.session.send(head) - self.session.send(output) - self.session.send("-----END XMLRESPONSE-----") - - except Exception, error: - self.logger.error(error) - - self.send_lock.release() - - def send_file(self, method, filepath): - - self.logger.info('Sending %s file to server' % filepath) - - assert ospath.exists(filepath), 'File not Found' - - f = open(filepath) - size = ospath.getsize(filepath) - - head = "-----BEGIN SENDFILE ID=%d METHOD=%s SIZE=%d-----" % (self.currid, - method, size) - - self.currid += 1 - self.send_lock.acquire() - - try: - self.session.send(head) - - while True: - data = f.read(1024) - if data != "": - self.session.send(data) - else: - break - - self.session.send("-----END SENDFILE-----") - - self.logger.info('Done: File sent %s to server' % filepath) - - except Exception, error: - self.logger.error(error) - - self.send_lock.release() - This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |