Skip to content
Snippets Groups Projects
Commit 7fe7c623 authored by ptangu01's avatar ptangu01
Browse files

format code style pep8 but not on methods yet

git-svn-id: https://redmine.imt-atlantique.fr/svn/xaal/code/Python/trunk@1360 b32b6428-25c9-4566-ad07-03861ab6144f
parent 93d2e626
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
#
# Copyright 2014, Jérôme Colin, Jérôme Kerdreux, Philippe Tanguy, Telecom Bretagne.
#
#
# Copyright 2014, Jérôme Colin, Jérôme Kerdreux, Philippe Tanguy,
# Telecom Bretagne.
#
# This file is part of xAAL.
#
#
# xAAL is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# xAAL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
#
# You should have received a copy of the GNU Lesser General Public License
# along with xAAL. If not, see <http://www.gnu.org/licenses/>.
#
......@@ -28,15 +29,16 @@ import codecs
import logging
logger=logging.getLogger(__name__)
logger = logging.getLogger(__name__)
class NetworkConnector():
UDP_MAX_SIZE=65507
UDP_MAX_SIZE = 65507
def __init__(self,address,port,hops):
self.connect(address,port,hops)
def __init__(self, address, port, hops):
self.connect(address, port, hops)
def connect(self,address,port,hops,bind_addr = ''):
def connect(self, address, port, hops, bind_addr=''):
self.__address = address
self.__port = port
self.__hops = hops
......@@ -46,45 +48,60 @@ class NetworkConnector():
self.networkError(e)
def __connect(self):
logger.info("Connecting to %s:%s" % (self.__address,self.__port))
logger.info("Connecting to %s:%s" % (self.__address, self.__port))
# TBD add bind_addr attrib
bind_addr = ''
self.__sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
#self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) #formac os ???
self.__sock = socket.socket(
socket.AF_INET,
socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
# self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
# #formac os ???
self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.__sock.bind((bind_addr, self.__port))
mreq = struct.pack('4sl', socket.inet_aton(self.__address), socket.INADDR_ANY)
self.__sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
self.__sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, self.__hops)
mreq = struct.pack(
'4sl',
socket.inet_aton(
self.__address),
socket.INADDR_ANY)
self.__sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
mreq)
self.__sock.setsockopt(
socket.IPPROTO_IP,
socket.IP_MULTICAST_TTL,
self.__hops)
def disconnect(self):
logger.info("Disconnecting from the bus")
self.__sock.close()
def setHops(self,value):
def setHops(self, value):
self.__hops = value
def getHops(self):
return self.__hops
def receive(self):
packt=self.__sock.recv(self.UDP_MAX_SIZE)
packt = self.__sock.recv(self.UDP_MAX_SIZE)
return packt
def getData(self):
r = select.select([self.__sock,],[],[],0.05)
r = select.select([self.__sock, ], [], [], 0.05)
if r[0]:
return self.receive()
return None
def send(self,message):
def send(self, message):
try:
self.__sock.sendto(codecs.encode(message), (self.__address, self.__port))
self.__sock.sendto(
codecs.encode(message), (self.__address, self.__port))
except socket.error as e:
self.networkError(e)
def networkError(self,msg):
def networkError(self, msg):
logger.info("Network error, reconnect..%s" % msg)
import time
time.sleep(5)
......@@ -92,7 +109,6 @@ class NetworkConnector():
def getAddr(self):
return self.__address
def getPort(self):
return self.__port
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment