Skip to content
This repository
tree: bb21d2857b
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 892 lines (743 sloc) 31.25 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
"""
bjson/connection.py
Asynchronous Bidirectional JSON-RPC protocol implementation over TCP/IP
Copyright (c) 2010 David Martinez Marti
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

"""
# Local changes:

import errno
import logging
import inspect
import socket, traceback, sys, threading
from types import MethodType, FunctionType

from bjsonrpc.proxies import Proxy
from bjsonrpc.request import Request
from bjsonrpc.exceptions import EofError, ServerError
from bjsonrpc import bjsonrpc_options

import bjsonrpc.jsonlib as json
import select


_log = logging.getLogger(__name__)


class RemoteObject(object):
    """
Represents a object in the server-side (or client-side when speaking from
the point of view of the server) . It remembers its name in the server-side
to allow calls to the original object.
Parameters:
**conn**
Connection object which holds the socket to the other end
of the communications
**obj**
JSON object (Python dictionary) holding the values recieved.
It is used to retrieve the properties to create the remote object.
(Initially only used to get object name)
Example::
list = conn.call.newList()
for i in range(10): list.notify.add(i)
print list.call.getitems()
Attributes:
**name**
name of the object in the server-side
**call**
Synchronous Proxy. It forwards your calls to it to the other end, waits
the response and returns the value.

**method**
Asynchronous Proxy. It forwards your calls to it to the other end and
inmediatelly returns a *request.Request* instance.

**pipe**
Asynchronous Proxy for "pipe" calls with multiple returns, like
method but you can check request.value multiple times, and must
call request.close() when you're done.

**notify**
Notification Proxy. It forwards your calls to it to the other end and
tells the server to not response even if there's any error in the call.
Returns *None*.
"""
    
    name = None
    call = None
    method = None
    notify = None
    pipe = None
    
    @property
    def connection(self):
        """
Public property to get the internal connection object.
"""
        return self._conn
    
    def __init__(self, conn, obj):
        self._conn = conn
        self.name = obj['__remoteobject__']
        
        self.call = Proxy(self._conn, obj=self.name, sync_type=0)
        self.method = Proxy(self._conn, obj=self.name, sync_type=1)
        self.notify = Proxy(self._conn, obj=self.name, sync_type=2)
        self.pipe = Proxy(self._conn, obj=self.name, sync_type=3)
    
    def __del__(self):
        self._close()
        
    def _close(self):
        """
Internal close method called both by __del__() and public
method close()
"""
        self.call.__delete__()
        self.name = None
        
    def close(self):
        """
Closes/deletes the remote object. The server may or may not delete
it at this time, but after this call we don't longer have any access to it.
This method is automatically called when Python deletes this instance.
"""
        return self._close()
        
        

class Connection(object): # TODO: Split this class in simple ones
    """
Represents a communiation tunnel between two parties.
**sck**
Connected socket to use. Should be an instance of *socket.socket* or
something compatible.
**address**
Address of the other peer in (host,port) form. It is only used to
inform handlers about the peer address.
**handler_factory**
Class type inherited from BaseHandler which holds the public methods.
It defaults to *NullHandler* meaning no public methods will be
avaliable to the other end.

**Members:**

**call**
Synchronous Proxy. It forwards your calls to it to the other end, waits
the response and returns the value
**method**
Asynchronous Proxy. It forwards your calls to it to the other end and
inmediatelly returns a *request.Request* instance.
**notify**
Notification Proxy. It forwards your calls to it to the other end and
tells the server to not response even if there's any error in the call.
Returns *None*.
"""
    _maxtimeout = {
        'read' : 60, # default maximum read timeout.
        'write' : 60, # default maximum write timeout.
    }
    call = None
    method = None
    notify = None
    pipe = None
    
    @classmethod
    def setmaxtimeout(cls, operation, value):
        """
Set the maximum timeout in seconds for **operation** operation.
Parameters:
**operation**
The operation which has to be configured. Can be either 'read'
or 'write'.
**value**
The timeout in seconds as a floating number. If is None, will
block until succeed. If is 0, will be nonblocking.
"""
        assert(operation in ['read', 'write'])
        cls._maxtimeout[operation] = value
    
    @classmethod
    def getmaxtimeout(cls, operation):
        """
Get the maximum timeout in seconds for **operation** operation.
Parameters:
**operation**
The operation which has to be configured. Can be either 'read'
or 'write'.
**(return value)**
The timeout in seconds as a floating number or None.
"""
        if operation not in cls._maxtimeout:
            return None
            
        return cls._maxtimeout[operation]
    
    
    def __init__(self, sck, address = None, handler_factory = None):
        self._debug_socket = False
        self._debug_dispatch = False
        self._buffer = b''
        self._sck = sck
        self._address = address
        self._handler = handler_factory
        self.connection_status = "open"
        if self._handler:
            self.handler = self._handler(self)
            
        self._id = 0
        self._requests = {}
        self._objects = {}

        self.scklock = threading.Lock()
        self.call = Proxy(self, sync_type=0)
        self.method = Proxy(self, sync_type=1)
        self.notify = Proxy(self, sync_type=2)
        self.pipe = Proxy(self, sync_type=3)
        self._wbuffer = b''
        self.write_lock = threading.RLock()
        self.read_lock = threading.RLock()
        self.getid_lock = threading.Lock()
        self.reading_event = threading.Event()
        self.threaded = bjsonrpc_options['threaded']
        self.write_thread_queue = []
        self.write_thread_semaphore = threading.Semaphore(0)
        self.write_thread = threading.Thread(target=self.write_thread)
        self.write_thread.daemon = True
        self.write_thread.start()

    @property
    def socket(self):
        """
public property that holds the internal socket used.
"""
        return self._sck
        
    def get_id(self):
        """
Retrieves a new ID counter. Each connection has a exclusive ID counter.
It is mainly used to create internal id's for calls.
"""
        self.getid_lock.acquire()
        # Prevent two threads to execute this code simultaneously
        self._id += 1
        ret = self._id
        self.getid_lock.release()
        
        return ret
        
    def load_object(self, obj):
        """
Helper function for JSON loads. Given a dictionary (javascript object) returns
an apropiate object (a specific class) in certain cases.
It is mainly used to convert JSON hinted classes back to real classes.
Parameters:
**obj**
Dictionary-like object to test.
**(return value)**
Either the same dictionary, or a class representing that object.
"""
        
        if '__remoteobject__' in obj:
            return RemoteObject(self, obj)
            
        if '__objectreference__' in obj:
            return self._objects[obj['__objectreference__']]
            
        if '__functionreference__' in obj:
            name = obj['__functionreference__']
            if '.' in name:
                objname, methodname = name.split('.')
                obj = self._objects[objname]
            else:
                obj = self.handler
                methodname = name
            method = obj.get_method(methodname)
            return method
        
        return obj
        
    def addrequest(self, request):
        """
Adds a request to the queue of requests waiting for response.
"""
        assert(isinstance(request, Request))
        assert(request.request_id not in self._requests)
        self._requests[request.request_id] = request

    def delrequest(self, req_id):
        """
Removes a request to the queue of requests waiting for response.
"""
        del self._requests[req_id]

    def dump_object(self, obj):
        """
Helper function to convert classes and functions to JSON objects.
Given a incompatible object called *obj*, dump_object returns a
JSON hinted object that represents the original parameter.
Parameters:
**obj**
Object, class, function,etc which is incompatible with JSON
serialization.
**(return value)**
A valid serialization for that object using JSON class hinting.
"""
        # object of unknown type
        if type(obj) is FunctionType or type(obj) is MethodType :
            conn = getattr(obj, '_conn', None)
            if conn != self:
                raise TypeError("Tried to serialize as JSON a handler for "
                "another connection!")
            return self._dump_functionreference(obj)
            
        if not isinstance(obj, object):
            raise TypeError("JSON objects must be new-style classes")
            
        if not hasattr(obj, '__class__'):
            raise TypeError("JSON objects must be instances, not types")
            
        if obj.__class__.__name__ == 'Decimal': # Probably is just a float.
            return float(obj)
            
        if isinstance(obj, RemoteObject):
            return self._dump_objectreference(obj)
            
        if hasattr(obj, 'get_method'):
            return self._dump_remoteobject(obj)
            
        raise TypeError("Python object %s laks a 'get_method' and "
            "is not serializable!" % repr(obj))

    def _dump_functionreference(self, obj):
        """ Converts obj to a JSON hinted-class functionreference"""
        return { '__functionreference__' : obj.__name__ }

    def _dump_objectreference(self, obj):
        """ Converts obj to a JSON hinted-class objectreference"""
        return { '__objectreference__' : obj.name }
        
    def _dump_remoteobject(self, obj):
        """
Converts obj to a JSON hinted-class remoteobject, creating
a RemoteObject if necessary
"""
        
        # An object can be remotely called if :
        # - it derives from object (new-style classes)
        # - it is an instance
        # - has an internal function _get_method to handle remote calls
        if not hasattr(obj, '__remoteobjects__'):
            obj.__remoteobjects__ = {}
            
        if self in obj.__remoteobjects__:
            instancename = obj.__remoteobjects__[self]
        else:
            classname = obj.__class__.__name__
            instancename = "%s_%04x" % (classname.lower(), self.get_id())
            self._objects[instancename] = obj
            obj.__remoteobjects__[self] = instancename
        return { '__remoteobject__' : instancename }

    def _format_exception(self, obj, method, args, kw, exc):
        etype, evalue, etb = exc
        funargs = ", ".join(
            [repr(x) for x in args] +
            ["%s=%r" % (k, kw[k]) for k in kw]
            )
        if len(funargs) > 40:
            funargs = funargs[:37] + "..."
                
        _log.error("(%s) In Handler method %s.%s(%s) ",
                   obj.__class__.__module__,
                   obj.__class__.__name__,
                   method,
                   funargs
            )
        _log.debug("\n".join([ "%s::%s:%d %s" % (
            filename, fnname,
            lineno, srcline)
            for filename, lineno, fnname, srcline
            in traceback.extract_tb(etb)[1:] ]))
        _log.error("Unhandled error: %s: %s", etype.__name__, evalue)
        del etb
        return '%s: %s' % (etype.__name__, evalue)

    def _dispatch_delete(self, objectname):
        try:
            self._objects[objectname]._shutdown()
        except Exception:
            _log.error("Error when shutting down the object %s:",
                       type(self._objects[objectname]))
            _log.debug(traceback.format_exc())
        del self._objects[objectname]

    def _extract_params(self, request):
        req_method = request.get("method")
        req_args = request.get("params", [])
        if type(req_args) is dict:
            req_kwargs = req_args
            req_args = []
        else:
            req_kwargs = request.get("kwparams", {})
        if req_kwargs:
            req_kwargs = dict((str(k), req_kwargs[k]) for k in req_kwargs)
        return req_method, req_args, req_kwargs
        
    def _find_object(self, req_method, req_args, req_kwargs):
        if '.' in req_method: # local-object.
            objectname, req_method = req_method.split('.')[:2]
            if objectname not in self._objects:
                raise ValueError("Invalid object identifier")
            elif req_method == '__delete__':
                self._dispatch_delete(objectname)
            else:
                return self._objects[objectname]
        else:
            return self.handler
        
    def _find_method(self, req_object, req_method, req_args, req_kwargs):
        """
Finds the method to process one request.
"""
        try:
            req_function = req_object.get_method(req_method)
            return req_function
        except ServerError as err:
            return str(err)
        except Exception:
            err = self._format_exception(req_object, req_method,
                                         req_args, req_kwargs,
                                         sys.exc_info())
            return err

    def dispatch_until_empty(self):
        """
Calls *read_and_dispatch* method until there are no more messages to
dispatch in the buffer.
Returns the number of operations that succeded.
This method will never block waiting. If there aren't
any more messages that can be processed, it returns.
"""
        ready_to_read = select.select(
                    [self._sck], # read
                    [], [], # write, errors
                    0 # timeout
                    )[0]
                    
        if not ready_to_read: return 0
            
        newline_idx = 0
        count = 0
        while newline_idx != -1:
            if not self.read_and_dispatch(timeout=0):
                break
            count += 1
            newline_idx = self._buffer.find(b'\n')
        return count
            
    def read_and_dispatch(self, timeout=None, thread=True, condition=None):
        """
Read one message from socket (with timeout specified by the optional
argument *timeout*) and dispatches that message.
Parameters:
**timeout** = None
Timeout in seconds of the read operation. If it is None
(or ommitted) then the read will wait
until new data is available.
**(return value)**
True, in case of the operation has suceeded and **one** message
has been dispatched. False, if no data or malformed data has
been received.
"""
        self.read_lock.acquire()
        self.reading_event.set()
        try:
            if condition:
                if condition() == False:
                    return False
            if thread:
                dispatch_item = self.dispatch_item_threaded
            else:
                dispatch_item = self.dispatch_item_single
            
            data = self.read(timeout=timeout)
            if not data:
                return False
            try:
                item = json.loads(data, self)
                if type(item) is list: # batch call
                    for i in item:
                        dispatch_item(i)
                elif type(item) is dict: # std call
                    if 'result' in item:
                        self.dispatch_item_single(item)
                    else:
                        dispatch_item(item)
                else: # Unknown format :-(
                    _log.debug("Received message with unknown format type: %s" , type(item))
                    return False
            except Exception:
                _log.debug(traceback.format_exc())
                return False
            return True
        finally:
            self.reading_event.clear()
            self.read_lock.release()
            
    def dispatch_item_threaded(self, item):
        """
If threaded mode is activated, this function creates a new thread per
each item received and returns without blocking.
"""
        if self.threaded:
            th1 = threading.Thread(target = self.dispatch_item_single, args = [ item ] )
            th1.start()
            return True
        else:
            return self.dispatch_item_single(item)
        
    def _send(self, response):
        txtResponse = None
        try:
            txtResponse = json.dumps(response, self)
        except Exception as e:
            _log.error("An unexpected error ocurred when trying to create the message: %r", e)
            response = {
                'result': None,
                'error': "InternalServerError: " + repr(e),
                'id': item['id']
                }
            txtResponse = json.dumps(response, self)
        try:
            self.write(txtResponse)
        except TypeError:
            _log.debug("response was: %r", response)
            raise

    def _send_response(self, item, response):
        if item['id']:
            ret = { 'result': response, 'error': None, 'id': item['id'] }
            self._send(ret)

    def _send_error(self, item, err):
        if item['id']:
            ret = { 'result': None, 'error': err, 'id': item['id'] }
            self._send(ret)

    def dispatch_item_single(self, item):
        """
Given a JSON item received from socket, determine its type and
process the message.
"""
        assert(type(item) is dict)
        item.setdefault('id', None)
        
        if 'method' in item:
            method, args, kw = self._extract_params(item)
            obj = self._find_object(method, args, kw)
            if obj is None: return
            fn = self._find_method(obj, method, args, kw)
            try:
                if inspect.isgeneratorfunction(fn):
                    for response in fn(*args, **kw):
                        self._send_response(item, response)
                elif callable(fn):
                    self._send_response(item, fn(*args, **kw))
                elif fn:
                    self._send_error(item, fn)
            except ServerError as exc:
                self._send_error(item, str(exc))
            except Exception:
                err = self._format_exception(obj, method, args, kw,
                                             sys.exc_info())
                self._send_error(item, err)
        elif 'result' in item:
            assert(item['id'] in self._requests)
            request = self._requests[item['id']]
            request.setresponse(item)
        else:
            self._send_error(item, 'Unknown format')
        return True
    
    def proxy(self, sync_type, name, args, kwargs):
        """
Call method on server.

sync_type ::
= 0 .. call method, wait, get response.
= 1 .. call method, inmediate return of object.
= 2 .. call notification and exit.
= 3 .. call method, inmediate return of non-auto-close object.
"""
       
        data = {}
        data['method'] = name

        if sync_type in [0, 1, 3]:
            data['id'] = self.get_id()
            
        if len(args) > 0:
            data['params'] = args
            
        if len(kwargs) > 0:
            if len(args) == 0:
                data['params'] = kwargs
            else:
                data['kwparams'] = kwargs
            
        if sync_type == 2: # short-circuit for speed!
            self.write(json.dumps(data, self))
            return None
                    
        req = Request(self, data)
        if sync_type == 0:
            return req.value
        if sync_type == 3:
            req.auto_close = False
        return req

    def close(self):
        """
Close the connection and the socket.
"""
        if self.connection_status == "closed": return
        item = {
            'abort' : True,
            'event' : threading.Event()
        }
        self.write_thread_queue.append(item)
        self.write_thread_semaphore.release() # notify new item.
        item['event'].wait(1)
        if not item['event'].isSet():
            _log.warning("write thread doesn't process our abort command")
        try:
            self.handler._shutdown()
        except Exception:
            _log.error("Error when shutting down the handler: %s",
                       traceback.format_exc())
        try:
            self._sck.shutdown(socket.SHUT_RDWR)
        except socket.error:
            pass
        self._sck.close()
        self.connection_status = "closed"
    
    def write_line(self, data):
        """
Write a line *data* to socket. It appends a **newline** at
the end of the *data* before sending it.
The string MUST NOT contain **newline** otherwise an AssertionError will
raise.
Parameters:
**data**
String containing the data to be sent.
"""
        assert('\n' not in data)
        self.write_lock.acquire()
        try:
            try:
                data = data.encode('utf-8')
            except AttributeError:
                pass
            if self._debug_socket:
                _log.debug("<:%d: %s", len(data), data.decode('utf-8')[:130])

            self._wbuffer += data + b'\n'
            sbytes = 0
            while self._wbuffer:
                try:
                    sbytes = self._sck.send(self._wbuffer)
                except IOError:
                    _log.debug("Read socket error: IOError (timeout: %r)",
                        self._sck.gettimeout())
                    _log.debug(traceback.format_exc(0))
                    return 0
                except socket.error:
                    _log.debug("Read socket error: socket.error (timeout: %r)",
                        self._sck.gettimeout())
                    _log.debug(traceback.format_exc(0))
                    return 0
                except:
                    raise
                if sbytes == 0:
                    break
                self._wbuffer = self._wbuffer[sbytes:]
            if self._wbuffer:
                _log.warning("%d bytes left in write buffer", len(self._wbuffer))
            return len(self._wbuffer)
        finally:
            self.write_lock.release()
            


    def read_line(self):
        """
Read a line of *data* from socket. It removes the `\\n` at
the end before returning the value.
If the original packet contained `\\n`, the message will be decoded
as two or more messages.
Returns the line of *data* received from the socket.
"""
        
        self.read_lock.acquire()
        try:
            data = self._readn()
            if len(data) and self._debug_socket:
                _log.debug(">:%d: %s", len(data), data.decode('utf-8')[:130])
            return data.decode('utf-8')
        finally:
            self.read_lock.release()
            
    def settimeout(self, operation, timeout):
        """
configures a timeout for the connection for a given operation.
operation is one of "read" or "write"
"""
        if operation in self._maxtimeout:
            maxtimeout = self._maxtimeout[operation]
        else:
            maxtimeout = None
            
        if maxtimeout is not None:
            if timeout is None or timeout > maxtimeout:
                timeout = maxtimeout
            
        self._sck.settimeout(timeout)
            
    
    def write_thread(self):
        abort = False
        while not abort:
            self.write_thread_semaphore.acquire()
            try:
                item = self.write_thread_queue.pop(0)
            except IndexError: # pop from empty list?
                _log.warning("write queue was empty??")
                continue
            abort = item.get("abort", False)
            event = item.get("event")
            write_data = item.get("write_data")
            if write_data: item["result"] = self.write_now(write_data)
            if event: event.set()
        if self._debug_socket:
            _log.debug("Writing thread finished.")
            
            
    def write(self, data, timeout = None):
        item = {
            'write_data' : data
        }
        self.write_thread_queue.append(item)
        self.write_thread_semaphore.release() # notify new item.

    def write_now(self, data, timeout = None):
        """
Standard function to write to the socket
which by default points to write_line
"""
        #self.scklock.acquire()
        self.settimeout("write", timeout)
        ret = None
        #try:
        ret = self.write_line(data)
        #finally:
        # self.scklock.release()
        return ret
    
    def read(self, timeout = None):
        """
Standard function to read from the socket
which by default points to read_line
"""
        ret = None
        self.scklock.acquire()
        self.settimeout("read", timeout)
        try:
            ret = self.read_line()
        finally:
            self.scklock.release()
        return ret

    def _readn(self):
        """
Internal function which reads from socket waiting for a newline
"""
        streambuffer = self._buffer
        pos = streambuffer.find(b'\n')
        #_log.debug("read...")
        #retry = 0
        while pos == -1:
            data = b''
            try:
                data = self._sck.recv(2048)
            except IOError as inst:
                _log.debug("Read socket error: IOError%r (timeout: %r)",
                    inst.args, self._sck.gettimeout())
                if inst.errno == errno.EAGAIN:
                    if self._sck.gettimeout() == 0: # if it was too fast
                        self._sck.settimeout(5)
                        continue
                        #time.sleep(0.5)
                        #retry += 1
                        #if retry < 10:
                        # _log.debug("Retry %s", retry)
                        # continue
                #_log.debug(traceback.format_exc(0))
                return b''
            except socket.error as inst:
                _log.error("Read socket error: socket.error%r (timeout: %r)",
                    inst.args, self._sck.gettimeout())
                #_log.debug(traceback.format_exc(0))
                return b''
            except:
                raise
            if not data:
                raise EofError(len(streambuffer))
            #_log.debug("readbuf+: %r", data)
            streambuffer += data
            pos = streambuffer.find(b'\n')

        self._buffer = streambuffer[pos + 1:]
        streambuffer = streambuffer[:pos]
        #_log.debug("read: %r", buffer)
        return streambuffer
        
    def serve(self):
        """
Basic function to put the connection serving. Usually is better to
use server.Server class to do this, but this would be useful too if
it is run from a separate Thread.
"""
        try:
            while True:
                self.read_and_dispatch()
        finally:
            self.close()
Something went wrong with that request. Please try again.