Skip to content
Snippets Groups Projects
Commit 9c7d5deb authored by jkerdreu's avatar jkerdreu
Browse files

Fin des discussions

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/branches/0.7@2353 b32b6428-25c9-4566-ad07-03861ab6144f
parent f2719ea4
Branches
No related tags found
No related merge requests found
from gevent import monkey; monkey.patch_all()
from xaal.lib import tools,Engine,Device,UUID,helpers
from xaal.lib import tools,Engine,Device,helpers
from xaal.monitor import Monitor
from bottle import default_app,debug,get,request,response,redirect,static_file
......
......@@ -5,10 +5,10 @@
from . import tools
from . import config
from . import bindings
from .core import Engine,Timer
from .network import NetworkConnector
from .devices import Device, Attribute, Attributes
from .messages import Message,MessageFactory,MessageType
from .uuids import UUID
from .exceptions import *
import uuid
from .exceptions import UUIDError
class UUID(uuid.UUID):
@staticmethod
def load(value):
return Address(value)
@staticmethod
def random_base(digit=2):
"""zeros the last digits of a random uuid, usefull w/ you want to forge some addresses
two digit is great.
"""
if (digit > 0) and (digit < 13):
tmp = str(uuid.uuid1())
st = "%s%s" % (tmp[:-digit],'0'*digit)
return UUID(st)
else:
raise UUIDError
@staticmethod
def random():
tmp = uuid.uuid1().int
return UUID(int=tmp)
def __add__(self,value):
tmp = self.int + value
return UUID(int=tmp)
def __sub__(self,value):
tmp = self.int - value
return UUID(int=tmp)
@property
def str(self):
return str(self)
......@@ -6,6 +6,7 @@ but can be usefull for xaal packages developpers
import logging
import logging.handlers
import os
import time
import coloredlogs
from . import config,Engine
......@@ -18,6 +19,17 @@ def singleton(class_):
return instances[class_]
return getinstance
def timeit(method):
def timed(*args, **kw):
logger = logging.getLogger(__name__)
ts = time.time()
result = method(*args, **kw)
te = time.time()
logger.debug('%r (%r, %r) %2.6f sec' % (method.__name__, args, kw, te-ts))
return result
return timed
def set_console_title(value):
# set xterm title
print("\x1B]0;xAAL => %s\x07" % value )
......
......@@ -22,7 +22,7 @@
from . import tools
from . import config
from .uuids import UUID
from .bindings import UUID
from .exceptions import MessageError,MessageParserError
#import cbor
......@@ -53,9 +53,9 @@ class MessageFactory(object):
:return: return an xAAL msg ciphered and serialized in CBOR
:rtype: CBOR
"""
result = []
# Format security layer
result = []
result.append(msg.version)
result.append(msg.timestamp[0])
result.append(msg.timestamp[1])
......@@ -69,15 +69,6 @@ class MessageFactory(object):
buf.append(msg.action)
if msg.body:
buf.append(msg.body)
"""
# for body, we need to marshall for special object, ie UUIDs
body = copy.copy(msg.body)
if body:
for k in body:
if hasattr(body[k],'bytes'):
body.update({k:body[k].bytes})
buf.append(body)
"""
clear = cbor.dumps(buf)
# Additionnal Data == cbor serialization of the targets array
ad = result[3]
......@@ -102,7 +93,7 @@ class MessageFactory(object):
except:
raise MessageParserError("Unable to parse CBOR data")
# Instanciate Message
# Instanciate Message, parse the security layer
msg = Message()
try:
msg.version = data_rx[0]
......@@ -135,7 +126,7 @@ class MessageFactory(object):
except :
raise MessageParserError("Unable to decrypt msg")
# Decode payload
# Decode application layer (payload)
try:
payload = cbor.loads(clear)
except:
......@@ -150,7 +141,6 @@ class MessageFactory(object):
if len(payload) == 5:
msg.body = payload[4]
# Sanity check incomming message
if not tools.is_valid_address(msg.source):
raise MessageParserError("Wrong message source [%s]" % msg.source)
......@@ -160,11 +150,11 @@ class MessageFactory(object):
# MSG builder
#####################################################
def build_msg(self,dev=None,targets=[], msgtype=None,action=None,body=None):
""" the build method takes in parameters :\n
-A device\n
-The list of targets of the message\n
-The type of the message\n
-The action of the message\n
""" the build method takes in parameters :
-A device
-The list of targets of the message
-The type of the message
-The action of the message
-A body if it's necessary (None if not)
it will return a message encoded in CBOR and Ciphered.
"""
......@@ -211,7 +201,6 @@ class MessageType(Enum):
NOTIFY = 0
REQUEST = 1
REPLY = 2
ERROR = 3
class Message(object):
......
......@@ -30,7 +30,7 @@ import sys
from configobj import ConfigObj
from . import config
from .uuids import UUID
from .bindings import UUID
XAAL_DEVTYPE_PATTERN = '^[a-zA-Z][a-zA-Z0-9_-]*\.[a-zA-Z][a-zA-Z0-9_-]*$'
......@@ -119,15 +119,3 @@ def reduce_addr(addr):
tmp = addr.str
return tmp[:5] + '..' + tmp[-5:]
def timeit(method):
def timed(*args, **kw):
import time
import logging
logger = logging.getLogger(__name__)
ts = time.time()
result = method(*args, **kw)
te = time.time()
logger.debug('%r (%r, %r) %2.6f sec' % (method.__name__, args, kw, te-ts))
return result
return timed
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment