commit do client
[remotepc] / pcremote-client-n8x0 / debian / pcremote-client / usr / share / pcremote-client / connection / iconnection.py
1 # -*- coding: utf-8 -*-
2
3 #  ****************************************************************************
4 #  Copyright (c) 2008 INdT/Fucapi.
5 #  This program is free software: you can redistribute it and/or modify
6 #  it under the terms of the GNU Lesser General Public License as published by
7 #  the Free Software Foundation, either version 3 of the License, or
8 #  (at your option) any later version.
9
10 #  This program is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #  GNU Lesser General Public License for more details.
14
15 #  You should have received a copy of the GNU Lesser General Public License
16 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #  ============================================================================
19 #  Project Name : PC Remote
20 #  Author       : André Portela
21 #  Email        : andre_portela_@hotmail.com
22 #  Reviewer     : Jônatas Isvi
23 #  Email        : jonatas.nona@gmail.com
24 #  Version      : 1.0
25 #  Description  : Interface Class, connection manager
26 #  ============================================================================
27
28 from wirelessconnectionmanager import *
29 from bluetoothconnectionmanager import *
30 from exceptions import *
31
32
33 # connections aliases
34 _btconst   = ['bluetooth', 'BLUETOOTH', 'blue']
35 _wificonst = ['wireless', 'WIRELESS', 'wifi']
36
37 class Iconnection:
38         def __init__(self, string):
39                 self.string = string
40                 if(self.string in _btconst):
41                         self.obj = BluetoothConnectionManager()
42                 elif(self.string in _wificonst):
43                         self.obj = WirelessConnectionManager()
44                 else:
45                         raise IconnectionError, "Undefined type."
46         
47         
48         # ********************************************************************************
49         # Generic methods -> Wireless and Bluetooth                                      *
50         # ********************************************************************************
51
52         # create a socket with defined protocol
53         def create_socket(self, protocol=None):
54                 self.obj.create_socket(protocol)
55
56         # connect device
57         def connect(self):
58                 self.obj.connect()
59
60         # accept the connection
61         def accept(self):
62                 return self.obj.accept()
63
64         # send a message to device
65         def send_message(self, msg=None):
66                 self.obj.send_message(msg)
67         
68         # received a message 
69         def received_message(self):
70                 return self.obj.received_message()
71         
72         # bind the connection
73         def bind(self):
74                 self.obj.bind()
75
76         # listen the connection
77         def listen(self):
78                 self.obj.listen()
79
80         # close connection
81         def close(self):
82                 self.obj.close()
83
84         # set the port to communicate
85         def set_port(self, port):
86                 self.obj.set_port(port)
87
88         # get the port to communicate
89         def get_port(self):
90                 return self.obj.get_port()
91
92         # set the device address
93         def set_address(self, address):
94                 self.obj.set_address(address)
95
96         # get the device address
97         def get_address(self):
98                 return self.obj.get_address()
99
100         # get the client address
101         def get_client_address(self):
102                 return self.obj.get_client_address()
103
104         # ************************************************************************************
105         # Bluetooth methods - All methods for bluetooth services                             *
106         # ************************************************************************************
107         
108         # fast way to create a simple server
109         def bt_create_server(self, protocol, port):
110                 if self.string in _btconst:
111                         return self.obj.create_server(protocol, port)
112                 else:
113                         raise IconnectionError, "Only method used by Bluetooth connections."
114
115         # fast way to create a simple client
116         def bt_create_client(self, protocol, address, port):
117                 if self.string in _btconst:
118                         return self.obj.create_client(protocol, address, port)
119                 else:
120                         raise IconnectionError, "Only method used by Bluetooth connections."
121
122         # search for all devices
123         def bt_find_devices(self, time=8):
124                 if self.string in _btconst:
125                         return self.obj.find_devices(time)
126                 else:
127                         raise IconnectionError, "Only method used by Bluetooth connections."
128
129         # search only devices names
130         def bt_find_devices_only_names(self):
131                 if self.string in _btconst:
132                         return self.obj.find_devices_only_names()
133                 else:   
134                         raise IconnectionError, "Only method used by Bluetooth connections."
135
136         
137         # search the device port
138         def bt_find_port(self, addr):
139                 if self.string in _btconst:
140                         return self.obj.find_port(addr)
141                 else:   
142                         raise IconnectionError, "Only method used by Bluetooth connections."
143
144
145         # search device services
146         def bt_find_services(self, service=None, addr=None):
147                 if self.string in _btconst:
148                         return self.obj.find_services(service, addr)
149                 else:
150                         raise IconnectionError, "Only method used by Bluetooth connections."
151
152
153         # search the device indicated by name
154         def bt_find_device_address_by_name(self, device_name=None):
155                 if self.string in _btconst:
156                         return self.obj.find_device_address_by_name(device_name)
157                 else:
158                         raise IconnectionError, "Only method used by Bluetooth connections."
159
160
161
162         # ***********************************************************************************
163         # Wireless method - All methods for wireless services                               * 
164         # ***********************************************************************************