Merge remote-tracking branch 'refs/remotes/upstream/master'
This commit is contained in:
commit
8ec093f7e0
3 changed files with 43 additions and 18 deletions
|
@ -99,6 +99,24 @@ class HelperClient(object):
|
||||||
|
|
||||||
return self.send_request(request, callback, timeout)
|
return self.send_request(request, callback, timeout)
|
||||||
|
|
||||||
|
def get_non(self, path, callback=None, timeout=None, **kwargs): # pragma: no cover
|
||||||
|
"""
|
||||||
|
Perform a GET on a certain path.
|
||||||
|
|
||||||
|
:param path: the path
|
||||||
|
:param callback: the callback function to invoke upon response
|
||||||
|
:param timeout: the timeout of the request
|
||||||
|
:return: the response
|
||||||
|
"""
|
||||||
|
request = self.mk_request_non(defines.Codes.GET, path)
|
||||||
|
request.token = generate_random_token(2)
|
||||||
|
|
||||||
|
for k, v in kwargs.items():
|
||||||
|
if hasattr(request, k):
|
||||||
|
setattr(request, k, v)
|
||||||
|
|
||||||
|
return self.send_request(request, callback, timeout)
|
||||||
|
|
||||||
def observe(self, path, callback, timeout=None, **kwargs): # pragma: no cover
|
def observe(self, path, callback, timeout=None, **kwargs): # pragma: no cover
|
||||||
"""
|
"""
|
||||||
Perform a GET with observe on a certain path.
|
Perform a GET with observe on a certain path.
|
||||||
|
@ -243,4 +261,19 @@ class HelperClient(object):
|
||||||
request.uri_path = path
|
request.uri_path = path
|
||||||
return request
|
return request
|
||||||
|
|
||||||
|
def mk_request_non(self, method, path):
|
||||||
|
"""
|
||||||
|
Create a request.
|
||||||
|
|
||||||
|
:param method: the CoAP method
|
||||||
|
:param path: the path of the request
|
||||||
|
:return: the request
|
||||||
|
"""
|
||||||
|
request = Request()
|
||||||
|
request.destination = self.server
|
||||||
|
request.code = method.number
|
||||||
|
request.uri_path = path
|
||||||
|
request.type = defines.Types["NON"]
|
||||||
|
return request
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -141,10 +141,10 @@ class BlockLayer(object):
|
||||||
item.num += 1
|
item.num += 1
|
||||||
item.byte += item.size
|
item.byte += item.size
|
||||||
if len(item.payload) <= item.byte:
|
if len(item.payload) <= item.byte:
|
||||||
m = 0
|
item.m = 0
|
||||||
else:
|
else:
|
||||||
m = 1
|
item.m = 1
|
||||||
request.block1 = (item.num, m, item.size)
|
request.block1 = (item.num, item.m, item.size)
|
||||||
elif transaction.response.block2 is not None:
|
elif transaction.response.block2 is not None:
|
||||||
|
|
||||||
num, m, size = transaction.response.block2
|
num, m, size = transaction.response.block2
|
||||||
|
|
|
@ -16,14 +16,18 @@ from coapthon.messages.request import Request
|
||||||
from coapthon.messages.response import Response
|
from coapthon.messages.response import Response
|
||||||
from coapthon.resources.resource import Resource
|
from coapthon.resources.resource import Resource
|
||||||
from coapthon.serializer import Serializer
|
from coapthon.serializer import Serializer
|
||||||
from coapthon.utils import Tree
|
from coapthon.utils import Tree, create_logging
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
|
|
||||||
__author__ = 'Giacomo Tanganelli'
|
__author__ = 'Giacomo Tanganelli'
|
||||||
|
|
||||||
|
|
||||||
|
if not os.path.isfile("logging.conf"):
|
||||||
|
create_logging()
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
logging.config.fileConfig("logging.conf", disable_existing_loggers=False)
|
||||||
|
|
||||||
|
|
||||||
class CoAP(object):
|
class CoAP(object):
|
||||||
|
@ -79,16 +83,10 @@ class CoAP(object):
|
||||||
# Join group
|
# Join group
|
||||||
if addrinfo[0] == socket.AF_INET: # IPv4
|
if addrinfo[0] == socket.AF_INET: # IPv4
|
||||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
|
|
||||||
# Allow multiple copies of this program on one machine
|
|
||||||
# (not strictly needed)
|
|
||||||
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
self._socket.bind((defines.ALL_COAP_NODES, self.server_address[1]))
|
self._socket.bind(('', self.server_address[1]))
|
||||||
mreq = struct.pack("4sl", socket.inet_aton(defines.ALL_COAP_NODES), socket.INADDR_ANY)
|
mreq = struct.pack("4sl", socket.inet_aton(defines.ALL_COAP_NODES), socket.INADDR_ANY)
|
||||||
self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
self._socket.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
|
||||||
self._unicast_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
self._unicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
||||||
self._unicast_socket.bind(self.server_address)
|
|
||||||
else:
|
else:
|
||||||
self._socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
self._socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
|
|
||||||
|
@ -104,7 +102,6 @@ class CoAP(object):
|
||||||
self._unicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
self._unicast_socket = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
|
||||||
self._unicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
self._unicast_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
self._unicast_socket.bind(self.server_address)
|
self._unicast_socket.bind(self.server_address)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if addrinfo[0] == socket.AF_INET: # IPv4
|
if addrinfo[0] == socket.AF_INET: # IPv4
|
||||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
self._socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
@ -252,10 +249,7 @@ class CoAP(object):
|
||||||
logger.debug("send_datagram - " + str(message))
|
logger.debug("send_datagram - " + str(message))
|
||||||
serializer = Serializer()
|
serializer = Serializer()
|
||||||
message = serializer.serialize(message)
|
message = serializer.serialize(message)
|
||||||
if self.multicast:
|
self._socket.sendto(message, (host, port))
|
||||||
self._unicast_socket.sendto(message, (host, port))
|
|
||||||
else:
|
|
||||||
self._socket.sendto(message, (host, port))
|
|
||||||
|
|
||||||
def add_resource(self, path, resource):
|
def add_resource(self, path, resource):
|
||||||
"""
|
"""
|
||||||
|
@ -279,8 +273,6 @@ class CoAP(object):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
res = None
|
res = None
|
||||||
if res is None:
|
if res is None:
|
||||||
if len(paths) != i:
|
|
||||||
return False
|
|
||||||
resource.path = actual_path
|
resource.path = actual_path
|
||||||
self.root[actual_path] = resource
|
self.root[actual_path] = resource
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in a new issue