2 # -*- coding: utf-8 -*-
3 # vim: set expandtab tabstop=4 shiftwidth=4 :
4 #----------------------------------------------------------------------------
6 #----------------------------------------------------------------------------
7 # This file is part of etherled.
9 # etherled is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the Free
11 # Software Foundation; either version 2 of the License, or (at your option)
14 # etherled is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 # You should have received a copy of the GNU General Public License along
20 # with etherled; if not, write to the Free Software Foundation, Inc., 59
21 # Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #----------------------------------------------------------------------------
23 # Creado: sáb oct 29 00:45:52 ART 2005
24 # Autores: Leandro Lucarella <llucare@fi.uba.ar>
25 #----------------------------------------------------------------------------
27 from sets import ImmutableSet as frozenset
39 # Variables soportadas
40 supported_vars = frozenset([VAR_OFF, VAR_MATRIX, VAR_PAUSE, VAR_DELAY])
47 def __init__(self, type, var=None, id=None, data=None):
48 if isinstance(type, str):
56 def fromStr(self, string):
57 header = ord(string[0])
58 self.type = header >> 7 # bit 7
59 self.var = (header & 0x78) >> 3 # bits 6 5 4 3
60 self.id = (header & 0x07) # bits 2 1 0
61 self.data = string[1:] or None
64 header = self._header_to_int()
66 if self.data is not None:
68 return chr(header) + data
71 return "Packet(type=%d, var=%d, id=%d, data=%s)" \
72 % (self.type, self.var, self.id, repr(self.data))
78 return self.type == p.type and self.var == p.var and self.id == p.id
80 def _header_to_int(self):
81 return (self.type << 7) + (self.var << 3) + self.id
86 def _setType(self, type):
87 if type != TYPE_GET and type != TYPE_SET:
88 raise ValueError, "type debe ser Packet.GET o Packet.SET"
94 def _setVar(self, var):
95 if var not in supported_vars:
96 raise ValueError, "var puede ser uno de %s" % tuple(supported_vars)
102 def _setId(self, id):
103 if id < 0 or id > MAX_ID:
104 raise ValueError, "id debe estar entre 0 y %d" % MAX_ID
107 type = property(_getType, _setType, doc="Tipo de operación")
108 var = property(_getVar, _setVar, doc="Variable con la cual operar")
109 id = property(_getId, _setId, doc="Identificador del paquete")
111 class ClientPacket(Packet):
113 def __init__(self, type, var=None, id=None, data=None):
114 if isinstance(type, str):
117 Packet.__init__(self, type, var, id, data)
118 if type == TYPE_GET and data is not None:
119 raise ValueError, "El paquete a enviar por el cliente no " \
120 "puede contener datos si es de tipo GET"
122 def fromStr(self, string):
123 Packet.fromStr(self, string)
124 if self.type == TYPE_SET and self.data is not None:
125 raise ValueError, "El paquete recibido por el cliente no " \
126 "puede contener datos si es de tipo SET"
128 class ServerPacket(Packet):
130 def __init__(self, type, var=None, id=None, data=None):
131 if isinstance(type, str):
134 Packet.__init__(self, type, var, id, data)
135 if type == TYPE_SET and data is not None:
136 raise ValueError, "El paquete a enviar por el servidor no " \
137 "puede contener datos si es de tipo SET"
139 def fromStr(self, string):
140 Packet.fromStr(self, string)
141 if self.type == TYPE_GET and self.data is not None:
142 raise ValueError, "El paquete recibido por el cliente no " \
143 "puede contener datos si es de tipo GET"
146 if __name__ == '__main__':
148 assert str(Packet(TYPE_GET, VAR_OFF, 0)) == chr(0x00)
149 assert str(Packet(TYPE_GET, VAR_OFF, 7)) == chr(0x07)
150 assert str(Packet(TYPE_GET, VAR_MATRIX, 1)) == chr(0x09)
151 assert str(Packet(TYPE_GET, VAR_MATRIX, 6)) == chr(0x0E)
152 assert str(Packet(TYPE_GET, VAR_PAUSE, 2)) == chr(0x12)
153 assert str(Packet(TYPE_GET, VAR_PAUSE, 5)) == chr(0x15)
154 assert str(Packet(TYPE_GET, VAR_DELAY, 3)) == chr(0x1B)
155 assert str(Packet(TYPE_GET, VAR_DELAY, 4)) == chr(0x1C)
156 assert str(Packet(TYPE_GET, VAR_DELAY, 7, 'hola')) == chr(0x1F) + 'hola'
157 p = Packet(TYPE_GET, VAR_MATRIX, 0, 'hola')
158 assert Packet(str(p)) == p
160 assert str(Packet(TYPE_SET, VAR_OFF, 0)) == chr(0x80)
161 assert str(Packet(TYPE_SET, VAR_OFF, 7)) == chr(0x87)
162 assert str(Packet(TYPE_SET, VAR_MATRIX, 1)) == chr(0x89)
163 assert str(Packet(TYPE_SET, VAR_MATRIX, 6)) == chr(0x8E)
164 assert str(Packet(TYPE_SET, VAR_PAUSE, 2)) == chr(0x92)
165 assert str(Packet(TYPE_SET, VAR_PAUSE, 5)) == chr(0x95)
166 assert str(Packet(TYPE_SET, VAR_DELAY, 3)) == chr(0x9B)
167 assert str(Packet(TYPE_SET, VAR_DELAY, 4)) == chr(0x9C)
168 assert str(Packet(TYPE_SET, VAR_DELAY, 7, 'hola')) == chr(0x9F) + 'hola'
169 p = Packet(TYPE_SET, VAR_MATRIX, 0, 'hola')
170 assert Packet(str(p)) == p