1 # -*- coding: utf-8 -*-
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.
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.
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/>.
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
25 # Package : connection
26 # Description : Iconnection Interface Class
27 # ============================================================================
29 from wirelessconnectionmanager import *
30 from bluetoothconnectionmanager import *
31 from exceptions import *
34 _btconst = ['bluetooth', 'BLUETOOTH', 'blue']
35 _wificonst = ['wireless', 'WIRELESS', 'wifi']
40 Interface for wireless and bluetooth connections.
41 Manages all commonalities operations between entities.
43 def __init__(self, string):
45 if(self.string in _btconst):
46 self.obj = BluetoothConnectionManager()
47 elif(self.string in _wificonst):
48 self.obj = WirelessConnectionManager()
50 raise IconnectionError, "Undefined type."
53 # +---------------------------------------------+
54 # | Generic methods -> Wireless and Bluetooth |
55 # +---------------------------------------------+
57 # create a socket with defined protocol
58 def create_socket(self, protocol=None):
59 self.obj.create_socket(protocol)
65 # accept the connection
67 return self.obj.accept()
69 # send a message to device
70 def send_message(self, msg=None):
71 self.obj.send_message(msg)
74 def received_message(self):
75 return self.obj.received_message()
81 # listen the connection
89 # set the port to communicate
90 def set_port(self, port):
91 self.obj.set_port(port)
93 # get the port to communicate
95 return self.obj.get_port()
97 # set the device address
98 def set_address(self, address):
99 self.obj.set_address(address)
101 # get the device address
102 def get_address(self):
103 return self.obj.get_address()
105 # get the client address
106 def get_client_address(self):
107 return self.obj.get_client_address()
109 # +------------------------------------------+
110 # | Bluetooth: particular behaviors |
111 # +------------------------------------------+
113 # fast way to create a simple server
114 def bluetooth_create_server(self, protocol, port):
115 if self.string in _btconst:
116 return self.obj.create_server(protocol, port)
118 raise IconnectionError, "Only method used by Bluetooth connections."
120 # fast way to create a simple client
121 def bluetooth_create_client(self, protocol, address, port):
122 if self.string in _btconst:
123 return self.obj.create_client(protocol, address, port)
125 raise IconnectionError, "Only method used by Bluetooth connections."
127 # search for all devices
128 def bluetooth_find_devices(self, time=8):
129 if self.string in _btconst:
130 return self.obj.find_devices(time)
132 raise IconnectionError, "Only method used by Bluetooth connections."
134 # search only devices names
135 def bluetooth_find_devices_only_names(self):
136 if self.string in _btconst:
137 return self.obj.find_devices_only_names()
139 raise IconnectionError, "Only method used by Bluetooth connections."
141 # search the device port
142 def bluetooth_find_port(self, addr):
143 if self.string in _btconst:
144 return self.obj.find_port(addr)
146 raise IconnectionError, "Only method used by Bluetooth connections."
148 # search device services
149 def bluetooth_find_services(self, service=None, addr=None):
150 if self.string in _btconst:
151 return self.obj.find_services(service, addr)
153 raise IconnectionError, "Only method used by Bluetooth connections."
156 # search the device indicated by name
157 def bluetooth_find_device_address_by_name(self, device_name=None):
158 if self.string in _btconst:
159 return self.obj.find_device_address_by_name(device_name)
161 raise IconnectionError, "Only method used by Bluetooth connections."
165 # +---------------------------------+
166 # | Wireless: particular behaviors |
167 # +---------------------------------+