JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / common / unix.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import socket
12 from vstf.common import constants
13 from vstf.common import message
14
15
16 class UdpServer(object):
17     def __init__(self):
18         super(UdpServer, self).__init__()
19         try:
20             os.unlink(constants.sockaddr)
21         except OSError:
22             if os.path.exists(constants.sockaddr):
23                 raise Exception("socket not found %s" % constants.sockaddr)    
24         self.conn=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)    
25     
26     def listen(self,backlog=5):
27         self.conn.listen(backlog)
28         
29     def accept(self):
30         return self.conn.accept()
31     
32     def bind(self, addr=constants.sockaddr):
33         return self.conn.bind(addr)
34        
35 #     def send(self, data, addr):
36 #         return message.sendto(self.conn.sendto, data, addr)
37         
38 #     def recv(self, size=constants.buff_size):
39 #         return message.recv(self.conn.recvfrom)
40     
41     def close(self):
42         self.conn.close()
43
44
45 class UdpClient(object):
46     def __init__(self):
47         super(UdpClient, self).__init__()
48         if not os.path.exists(constants.sockaddr):
49             raise Exception("socket not found %s" % constants.sockaddr)    
50         self.conn=socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
51      
52     def connect(self, addr=constants.sockaddr):
53         return self.conn.connect(addr)
54        
55     def send(self, data):
56         message.send(self.conn.send, data)
57         
58     def recv(self):
59         return message.recv(self.conn.recv)
60     
61     def close(self):
62         self.conn.close()