310d175095902c9003c67b5440342085fc39f3eb
[remotepc] / pcremote-server-desktop-60 / connection / .svn / text-base / iconnection.py.svn-base
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 #  Package      : connection
26 #  Description  : Iconnection Interface Class
27 #  ============================================================================
28
29 from wirelessconnectionmanager import *
30 from bluetoothconnectionmanager import *
31 from exceptions import *
32
33 # connections aliases
34 _btconst   = ['bluetooth', 'BLUETOOTH', 'blue']
35 _wificonst = ['wireless', 'WIRELESS', 'wifi']
36
37 class Iconnection:
38     
39     """ Iconnection
40     Interface for wireless and bluetooth connections.
41     Manages all commonalities operations between entities.
42     """
43     def __init__(self, string):
44         self.string = string
45         if(self.string in _btconst):
46             self.obj = BluetoothConnectionManager()
47         elif(self.string in _wificonst):
48             self.obj = WirelessConnectionManager()
49         else:
50             raise IconnectionError, "Undefined type."
51         
52         
53     # +---------------------------------------------+ 
54     # | Generic methods -> Wireless and Bluetooth   |
55     # +---------------------------------------------+
56
57     # create a socket with defined protocol
58     def create_socket(self, protocol=None):
59         self.obj.create_socket(protocol)
60
61     # connect device
62     def connect(self):
63         self.obj.connect()
64
65     # accept the connection
66     def accept(self):
67         return self.obj.accept()
68
69     # send a message to device
70     def send_message(self, msg=None):
71         self.obj.send_message(msg)
72         
73     # received a message 
74     def received_message(self):
75         return self.obj.received_message()
76         
77     # bind the connection
78     def bind(self):
79         self.obj.bind()
80
81     # listen the connection
82     def listen(self):
83         self.obj.listen()
84
85     # close connection
86     def close(self):
87         self.obj.close()
88
89     # set the port to communicate
90     def set_port(self, port):
91         self.obj.set_port(port)
92
93     # get the port to communicate
94     def get_port(self):
95         return self.obj.get_port()
96
97     # set the device address
98     def set_address(self, address):
99         self.obj.set_address(address)
100
101     # get the device address
102     def get_address(self):
103         return self.obj.get_address()
104
105     # get the client address
106     def get_client_address(self):
107         return self.obj.get_client_address()
108
109     # +------------------------------------------+
110     # | Bluetooth: particular behaviors          |
111     # +------------------------------------------+
112         
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)
117         else:
118             raise IconnectionError, "Only method used by Bluetooth connections."
119
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)
124         else:
125             raise IconnectionError, "Only method used by Bluetooth connections."
126
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)
131         else:
132             raise IconnectionError, "Only method used by Bluetooth connections."
133
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()
138         else:   
139             raise IconnectionError, "Only method used by Bluetooth connections."
140
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)
145         else:   
146             raise IconnectionError, "Only method used by Bluetooth connections."
147
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)
152         else:
153             raise IconnectionError, "Only method used by Bluetooth connections."
154
155
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)
160         else:
161             raise IconnectionError, "Only method used by Bluetooth connections."
162
163
164
165     # +---------------------------------+
166     # | Wireless: particular behaviors  | 
167     # +---------------------------------+