~ubuntu-branches/ubuntu/oneiric/emesene/oneiric-proposed

« back to all changes in this revision

Viewing changes to emesene/e3/msn/Socket.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2011-03-03 14:49:13 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110303144913-0adl9cmw2s35lvzo
Tags: 2.0~git20110303-0ubuntu1
* New upstream git revision (LP: #728469).
* Remove debian/watch, debian/emesene.xpm, debian/install and
  debian/README.source files.
* Remove 21_svn2451_fix_avatar and 20_dont_build_own_libmimic patches.
* debian/control: modify python to python (>= 2.5) in Build-Depends field.
* debian/control: remove python-libmimic from Recommends field.
* debian/control: modify python-gtk2 (>= 2.10) to python-gtk2 (>= 2.12) in
  Depends field.
* debian/control: add python-appindicator and python-xmpp to Recommends
  field.
* debian/control: add python-papyon (>= 0.5.4) and python-webkit to Depends
  field.
* debian/control: update Description field.
* debian/control: add python-setuptools to Build-Depends field.
* debian/control: move python-dbus and python-notify to Depends field.
* Update debian/copyright file.
* Update debian/links file.
* debian/menu: update description field.
* Bump Standards-Version to 3.9.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
'''defines a class that handles the send and receive operations of a socket
 
2
in a thread'''
 
3
 
 
4
import Queue
 
5
import socket
 
6
import select
 
7
import StringIO
 
8
import threading
 
9
 
 
10
import logging
 
11
log = logging.getLogger('msn.Socket')
 
12
 
 
13
class Socket(threading.Thread):
 
14
    '''a socket that runs on a thread, it reads the data and put it on the 
 
15
    output queue, the data to be sent is added to the input queue'''
 
16
 
 
17
    def __init__(self, host, port):
 
18
        '''class constructor'''
 
19
        threading.Thread.__init__(self)
 
20
 
 
21
        self.host = host
 
22
        self.port = port
 
23
 
 
24
        self.input = Queue.Queue()
 
25
        self.output = Queue.Queue()
 
26
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 
27
        self.setDaemon(True)
 
28
 
 
29
    def send(self, data):
 
30
        '''add data to the input queue'''
 
31
        self.input.put(data)
 
32
 
 
33
    def quit(self):
 
34
        '''close the thread'''
 
35
        self.send('quit')
 
36
 
 
37
    def run(self):
 
38
        '''the main method of the socket, wait until there is something to 
 
39
        send or there is something to read, if there is something to send, get 
 
40
        it from the input Queue, wait until we can send it and send it, if 
 
41
        there is something to read, read it and put it on the output queue'''
 
42
        self.socket.connect((self.host, self.port))
 
43
        input_ = None
 
44
 
 
45
        while True:
 
46
            # see if we can send or read something
 
47
            (iwtd, owtd) = select.select([self], [self], [])[:2]
 
48
 
 
49
            # if we can read, we try to read
 
50
            if iwtd:
 
51
                if not self._receive():
 
52
                    # nothing received, socket closed
 
53
                    break
 
54
                # do not write until everything is read
 
55
                continue
 
56
 
 
57
            try:
 
58
                input_ = self.input.get(True, 0.3)
 
59
 
 
60
                if input_ == 'quit':
 
61
                    break
 
62
            except Queue.Empty:
 
63
                # nothing to send
 
64
                continue
 
65
 
 
66
            if owtd and input_:
 
67
                # try to get something to send, wait 0.3 seconds
 
68
                try:
 
69
                    self.socket.send(input_)
 
70
                    log.debug('>>> ' + str(input_))
 
71
                except socket.error:
 
72
                    self._on_socket_error()
 
73
                    break
 
74
 
 
75
        log.debug('closing socket thread')
 
76
        self.socket.close()
 
77
 
 
78
    def fileno(self):
 
79
        '''method that is used by select'''
 
80
        return self.socket.fileno()
 
81
 
 
82
    def _receive(self):
 
83
        '''receive data from the socket'''
 
84
        data = self._readline()
 
85
        # if we got something add it to the output queue
 
86
        if data:
 
87
            log.debug('<<< ' + data)
 
88
            self.output.put(data)
 
89
            return True
 
90
        return False
 
91
 
 
92
    def _readline(self):
 
93
        '''read until new line'''
 
94
        output = StringIO.StringIO()
 
95
 
 
96
        try:
 
97
            chunk = self.socket.recv(1)
 
98
        except socket.error:
 
99
            self._on_socket_error()
 
100
            return None
 
101
 
 
102
        while chunk != '\n' and chunk != '':
 
103
            output.write(chunk)
 
104
            try:
 
105
                chunk = self.socket.recv(1)
 
106
            except socket.error:
 
107
                self._on_socket_error()
 
108
 
 
109
        if chunk == '\n':
 
110
            output.write(chunk)
 
111
 
 
112
        output.seek(0)
 
113
        return output.read()
 
114
 
 
115
    def receive_fixed_size(self, size):
 
116
        '''receive a fixed size of bytes, return it as string'''
 
117
        output = StringIO.StringIO()
 
118
 
 
119
        while output.len < size:
 
120
            try:
 
121
                output.write(self.socket.recv(size - output.len))
 
122
            except socket.error:
 
123
                self._on_socket_error()
 
124
 
 
125
        output.seek(0)
 
126
 
 
127
        return output.read()
 
128
 
 
129
    def _on_socket_error(self):
 
130
        '''send a message that the socket was closed'''
 
131
        self.output.put(0)