Commit 79ef7f60 authored by Federico Sismondi's avatar Federico Sismondi

big refact of project structure, cleaned up imports

parent 6ee5d4a1
from __future__ import absolute_import from __future__ import absolute_import
from .agent_cli import Agent from .agent_cli import Agent
...@@ -165,9 +165,8 @@ For more information: README.md ...@@ -165,9 +165,8 @@ For more information: README.md
self.name_option = click.Option( self.name_option = click.Option(
param_decls=["--name"], param_decls=["--name"],
default=str(uuid.uuid1()), required=True,
required=False, help="Agent identity, normally associated with the IUT role (coap_client, comi_server, etc)")
help="Agent identity (default: random generated)")
self.dump_option = click.Option( self.dump_option = click.Option(
param_decls=["--dump"], param_decls=["--dump"],
......
from __future__ import absolute_import
from . import core
from . import serialconn
from . import tun
from . import base
\ No newline at end of file
...@@ -5,7 +5,7 @@ import json ...@@ -5,7 +5,7 @@ import json
import logging import logging
from multiprocessing import Process from multiprocessing import Process
from utils.messages import Message as EventBusMessage from ..utils.messages import Message as EventBusMessage
from amqp.exceptions import UnexpectedFrame from amqp.exceptions import UnexpectedFrame
from kombu import Connection, Queue, Exchange, Consumer from kombu import Connection, Queue, Exchange, Consumer
...@@ -64,7 +64,7 @@ class BaseConsumer(ConsumerMixin): ...@@ -64,7 +64,7 @@ class BaseConsumer(ConsumerMixin):
def subscribe_to_topics(self, topic_list): def subscribe_to_topics(self, topic_list):
for t in topic_list: for t in topic_list:
queue = Queue( queue = Queue(
name="consumer: {name}.{consumer_name}::RKey:{rkey}".format( name="consumer: {name}.{consumer_name}?rkey={rkey}".format(
name=self.name, name=self.name,
consumer_name=self.consumer_name, consumer_name=self.consumer_name,
rkey=t rkey=t
...@@ -88,7 +88,7 @@ class BaseConsumer(ConsumerMixin): ...@@ -88,7 +88,7 @@ class BaseConsumer(ConsumerMixin):
assert type(body) is dict # assert that kombu deserialized it already assert type(body) is dict # assert that kombu deserialized it already
json_body = json.dumps(body) json_body = json.dumps(body)
self.log.debug("DEFAULT on_message callback, got: {}".format(message.delivery_info.get('routing_key'))) self.log.debug("base on_message() callback, got: {}".format(message.delivery_info.get('routing_key')))
msg = EventBusMessage.load(json_body, message.delivery_info.get('routing_key')) msg = EventBusMessage.load(json_body, message.delivery_info.get('routing_key'))
try: try:
self._on_message(msg) self._on_message(msg)
...@@ -98,16 +98,13 @@ class BaseConsumer(ConsumerMixin): ...@@ -98,16 +98,13 @@ class BaseConsumer(ConsumerMixin):
) )
def _on_message(self, message): def _on_message(self, message):
"Class to be overridden by children calss" "Class to be overridden by children class"
return NotImplementedError() return NotImplementedError()
def on_consume_ready(self, connection, channel, consumers, wakeup=True, **kwargs): def on_consume_ready(self, connection, channel, consumers, wakeup=True, **kwargs):
# control plane info # control plane info
for q in self.queues: for q in self.queues:
self.log.info( self.log.info("Queue: {queue_name} bound to: {rkey} ".format(queue_name=q.name, rkey=q.routing_key))
"Queue: {queue_name} bound to: {rkey} ".format(queue_name=q.name,
rkey=q.routing_key)
)
class BaseController(Process): class BaseController(Process):
......
...@@ -3,10 +3,10 @@ Plugin to connect to the AMQP broker ...@@ -3,10 +3,10 @@ Plugin to connect to the AMQP broker
""" """
import json import json
import logging import logging
from utils.messages import *
from kombu import Producer from kombu import Producer
from .base import BaseController, BaseConsumer from .base import BaseController, BaseConsumer
from ..utils.messages import MsgTestingToolComponentReady
__version__ = (0, 1, 0) __version__ = (0, 1, 0)
......
import json import json
__version__ = (0, 1, 0)
import logging import logging
from requests import Request, Session from requests import Request, Session
from .base import BaseController, BaseConsumer from .base import BaseController, BaseConsumer
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
......
...@@ -6,10 +6,10 @@ import serial ...@@ -6,10 +6,10 @@ import serial
import logging import logging
import threading import threading
from connectors.base import BaseController, BaseConsumer from .base import BaseController, BaseConsumer
from utils.serial_listener import SerialListener from ..utils import messages
from utils import arrow_down, arrow_up, finterop_banner from ..utils import arrow_down
from utils.messages import * from ..utils.serial_listener import SerialListener
__version__ = (0, 1, 0) __version__ = (0, 1, 0)
...@@ -26,11 +26,11 @@ class SerialConsumer(BaseConsumer): ...@@ -26,11 +26,11 @@ class SerialConsumer(BaseConsumer):
def __init__(self, user, password, session, server, exchange, name, consumer_name): def __init__(self, user, password, session, server, exchange, name, consumer_name):
self.dispatcher = { self.dispatcher = {
MsgPacketInjectRaw: self.handle_data, messages.MsgPacketInjectRaw: self.handle_data,
} }
subscriptions = [ subscriptions = [
MsgPacketInjectRaw.routing_key.replace('*', name).replace('ip.tun', '802154.serial'), messages.MsgPacketInjectRaw.routing_key.replace('*', name).replace('ip.tun', '802154.serial'),
] ]
super(SerialConsumer, self).__init__(user, password, session, server, exchange, name, consumer_name, super(SerialConsumer, self).__init__(user, password, session, server, exchange, name, consumer_name,
......
...@@ -7,15 +7,12 @@ import json ...@@ -7,15 +7,12 @@ import json
import logging import logging
import sys import sys
import datetime import datetime
from utils.opentun import OpenTunLinux, OpenTunMACOS
from utils import arrow_down, arrow_up, finterop_banner
from utils.messages import *
from kombu import Producer from kombu import Producer
from connectors.base import BaseController, BaseConsumer
__version__ = (0, 1, 0) from .base import BaseController, BaseConsumer
from ..utils.opentun import OpenTunLinux, OpenTunMACOS
from ..utils import arrow_up
from ..utils import messages
class TunConsumer(BaseConsumer): class TunConsumer(BaseConsumer):
...@@ -29,22 +26,22 @@ class TunConsumer(BaseConsumer): ...@@ -29,22 +26,22 @@ class TunConsumer(BaseConsumer):
def __init__(self, user, password, session, server, exchange, name, consumer_name, force_bootstrap, ipv6_host, def __init__(self, user, password, session, server, exchange, name, consumer_name, force_bootstrap, ipv6_host,
ipv6_prefix): ipv6_prefix):
self.dispatcher = { self.dispatcher = {
MsgAgentTunStart: self.handle_start, messages.MsgAgentTunStart: self.handle_start,
MsgPacketInjectRaw: self.handle_raw_packet_to_inject, messages.MsgPacketInjectRaw: self.handle_raw_packet_to_inject,
} }
self.tun = None self.tun = None
self.packet_count = 0 self.packet_count = 0
subscriptions = [ subscriptions = [
MsgAgentTunStart.routing_key.replace('*', name), # default rkey is "toAgent.*.ip.tun.start" messages.MsgAgentTunStart.routing_key.replace('*', name), # default rkey is "toAgent.*.ip.tun.start"
MsgPacketInjectRaw.routing_key.replace('*', name) messages.MsgPacketInjectRaw.routing_key.replace('*', name)
] ]
super(TunConsumer, self).__init__(user, password, session, server, exchange, name, consumer_name, subscriptions) super(TunConsumer, self).__init__(user, password, session, server, exchange, name, consumer_name, subscriptions)
if force_bootstrap: if force_bootstrap:
self.handle_start( self.handle_start(
MsgAgentTunStart( messages.MsgAgentTunStart(
name=name, name=name,
ipv6_host=ipv6_host, ipv6_host=ipv6_host,
ipv6_prefix=ipv6_prefix, ipv6_prefix=ipv6_prefix,
...@@ -75,7 +72,7 @@ class TunConsumer(BaseConsumer): ...@@ -75,7 +72,7 @@ class TunConsumer(BaseConsumer):
conf_params = self.tun.get_tun_configuration() conf_params = self.tun.get_tun_configuration()
conf_params.update({'name': self.name}) conf_params.update({'name': self.name})
# publish message in event bus # publish message in event bus
msg = MsgAgentTunStarted(**conf_params) msg = messages.MsgAgentTunStarted(**conf_params)
logging.info('Publishing %s' % repr(msg)) logging.info('Publishing %s' % repr(msg))
producer = Producer(self.connection, serializer='json') producer = Producer(self.connection, serializer='json')
...@@ -146,17 +143,25 @@ class TunConsumer(BaseConsumer): ...@@ -146,17 +143,25 @@ class TunConsumer(BaseConsumer):
return return
self.packet_count += 1 self.packet_count += 1
print(arrow_down)
self.log.debug('\n* * * * * * HANDLE INCOMING PACKET (%s) * * * * * * *' % self.packet_count)
self.log.debug("TIME: %s" % datetime.datetime.time(datetime.datetime.now()))
self.log.debug(" - - - ")
self.log.debug(("Interface", message.interface_name))
self.log.debug(("Data", message.data))
self.log.debug('\n* * * * * * * * * * * * * * * * * * * * * * *')
self.log.info("Message received from testing tool. Injecting in Tun. Message count (downlink): %s" self.log.info("Message received from testing tool. Injecting in Tun. Message count (downlink): %s"
% self.packet_count) % self.packet_count)
print(arrow_up)
self.log.info('\n # # # # # # # # # # # # OPEN TUN # # # # # # # # # # # # ' +
'\n data packet EventBus -> TUN interface' +
'\n' + message.to_json() +
'\n # # # # # # # # # # # # # # # # # # # # # # # # # # # # #'
)
# print(arrow_down)
# self.log.debug('\n* * * * * * HANDLE INCOMING PACKET (%s) * * * * * * *' % self.packet_count)
# self.log.debug("TIME: %s" % datetime.datetime.time(datetime.datetime.now()))
# self.log.debug(" - - - ")
# self.log.debug(("Interface", message.interface_name))
# self.log.debug(("Data", message.data))
# self.log.debug('\n* * * * * * * * * * * * * * * * * * * * * * *')
self.tun._eventBusToTun( self.tun._eventBusToTun(
sender="Testing Tool", sender="Testing Tool",
signal="tun inject", signal="tun inject",
......
from __future__ import absolute_import
from . import messages
import logging import logging
import os import os
......
...@@ -12,8 +12,8 @@ import sys ...@@ -12,8 +12,8 @@ import sys
from kombu import Exchange from kombu import Exchange
from utils import arrow_down, arrow_up, finterop_banner from . import arrow_down, arrow_up
from utils.messages import * from . import messages
DEFAULT_IPV6_PREFIX = 'bbbb' DEFAULT_IPV6_PREFIX = 'bbbb'
...@@ -503,7 +503,7 @@ class OpenTunLinux(object): ...@@ -503,7 +503,7 @@ class OpenTunLinux(object):
This function forwards the data to the the EventBus. This function forwards the data to the the EventBus.
""" """
routing_key = MsgPacketSniffedRaw.routing_key.replace('*', self.name) routing_key = messages.MsgPacketSniffedRaw.routing_key.replace('*', self.name)
log.debug("Pushing message to topic: %s" % routing_key) log.debug("Pushing message to topic: %s" % routing_key)
self.packet_count += 1 self.packet_count += 1
...@@ -511,14 +511,14 @@ class OpenTunLinux(object): ...@@ -511,14 +511,14 @@ class OpenTunLinux(object):
% self.packet_count) % self.packet_count)
# dispatch to EventBus # dispatch to EventBus
m = MsgPacketSniffedRaw( m = messages.MsgPacketSniffedRaw(
interface_name=self.ifname, interface_name=self.ifname,
timestamp=time.time(), timestamp=time.time(),
data=data data=data
) )
print(arrow_up) print(arrow_up)
log.info('\n # # # # # # # # # # # # OPEN TUN # # # # # # # # # # # # ' + log.info('\n # # # # # # # # # # # # OPEN TUN # # # # # # # # # # # # ' +
'\n data packet TUN -> EventBus' + '\n data packet TUN interface -> EventBus' +
'\n' + m.to_json() + '\n' + m.to_json() +
'\n # # # # # # # # # # # # # # # # # # # # # # # # # # # # #' '\n # # # # # # # # # # # # # # # # # # # # # # # # # # # # #'
) )
...@@ -718,7 +718,7 @@ class OpenTunMACOS(object): ...@@ -718,7 +718,7 @@ class OpenTunMACOS(object):
This function forwards the data to the the EventBus. This function forwards the data to the the EventBus.
""" """
routing_key = MsgPacketSniffedRaw.routing_key.replace('*', self.name) routing_key = messages.MsgPacketSniffedRaw.routing_key.replace('*', self.name)
log.debug("Pushing message to topic: %s" % routing_key) log.debug("Pushing message to topic: %s" % routing_key)
self.packet_count += 1 self.packet_count += 1
...@@ -726,14 +726,14 @@ class OpenTunMACOS(object): ...@@ -726,14 +726,14 @@ class OpenTunMACOS(object):
% self.packet_count) % self.packet_count)
# dispatch to EventBus # dispatch to EventBus
m = MsgPacketSniffedRaw( m = messages.MsgPacketSniffedRaw(
interface_name=self.ifname, interface_name=self.ifname,
timestamp=time.time(), timestamp=time.time(),
data=data data=data
) )
print(arrow_up) print(arrow_up)
log.info('\n # # # # # # # # # # # # OPEN TUN # # # # # # # # # # # # ' + log.info('\n # # # # # # # # # # # # OPEN TUN # # # # # # # # # # # # ' +
'\n data packet TUN -> EventBus' + '\n data packet TUN interface -> EventBus' +
'\n' + m.to_json() + '\n' + m.to_json() +
'\n # # # # # # # # # # # # # # # # # # # # # # # # # # # # #' '\n # # # # # # # # # # # # # # # # # # # # # # # # # # # # #'
) )
......
...@@ -4,10 +4,12 @@ import json ...@@ -4,10 +4,12 @@ import json
import serial import serial
import logging import logging
import sys import sys
from utils import messages
from kombu import Exchange from kombu import Exchange
from collections import OrderedDict from collections import OrderedDict
from utils import arrow_down, arrow_up, finterop_banner
from . import messages
from . import arrow_down, arrow_up, finterop_banner
STATE_OK = 0 STATE_OK = 0
STATE_ESC = 1 STATE_ESC = 1
......
# Author: # Author:
# Federico Sismondi <federico.sismondi@gmail.com> # Federico Sismondi <federicosismondi@gmail.com>
#
# License: ???
from __future__ import absolute_import from __future__ import absolute_import
import io import io
...@@ -51,7 +50,7 @@ setup( ...@@ -51,7 +50,7 @@ setup(
'kombu', 'kombu',
'pika', 'pika',
'pyserial', 'pyserial',
'requests',
], ],
entry_points={'console_scripts': ['ioppytest-agent=agent.agent_cli:main']}, entry_points={'console_scripts': ['ioppytest-agent=agent.agent_cli:main']},
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment