summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/common/unix.py
blob: f74944e221359f5d3b2fc952630cc106e80deb3e (plain)
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
import os
import socket
from vstf.common import constants
from vstf.common import message


class UdpServer(object):
    def __init__(self):
        super(UdpServer, self).__init__()
        try:
            os.unlink(constants.sockaddr)
        except OSError:
            if os.path.exists(constants.sockaddr):
                raise Exception("socket not found %s" % constants.sockaddr)    
        self.conn=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)    
    
    def listen(self,backlog=5):
        self.conn.listen(backlog)
        
    def accept(self):
        return self.conn.accept()
    
    def bind(self, addr=constants.sockaddr):
        return self.conn.bind(addr)
       
#     def send(self, data, addr):
#         return message.sendto(self.conn.sendto, data, addr)
        
#     def recv(self, size=constants.buff_size):
#         return message.recv(self.conn.recvfrom)
    
    def close(self):
        self.conn.close()


class UdpClient(object):
    def __init__(self):
        super(UdpClient, self).__init__()
        if not os.path.exists(constants.sockaddr):
            raise Exception("socket not found %s" % constants.sockaddr)    
        self.conn=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
     
    def connect(self, addr=constants.sockaddr):
        return self.conn.connect(addr)
       
    def send(self, data):
        message.send(self.conn.send, data)
        
    def recv(self):
        return message.recv(self.conn.recv)
    
    def close(self):
        self.conn.close()