Пример #1
0
    def __init__(self, dest):

        if not os.path.exists(dest):
            print("Creating directory '%s'" %dest)
            os.makedirs(dest)
        self.dest = dest

        self.protos = load()

        self.formats = dict((v[1],k) for k, v in formats.items())
Пример #2
0
 def __init__(self, **kwargs):
     super(AtpSender, self).__init__()
     self.port = PORT
     self.host = HOST
     self.symmetrical = False
     self.transmitter = 'arm'
     for arg in kwargs:
         if arg == 'host':
             self.host = kwargs[arg]
         elif arg == 'port':
             self.port = int(kwargs[arg])
         elif arg == 'symmetrical':
             self.symmetrical = kwargs[arg]
         elif arg == 'transmitter':
             self.transmitter = kwargs[arg]
         else:
             print("Warning: unexpected '%s' argument" %arg, file=sys.stderr)
     self.protos = protos.load()
     self.error = QtGui.QErrorMessage()
     self.initUI()
Пример #3
0
 def __init__(self, **kwargs):
     super(AtpSender, self).__init__()
     self.port = PORT
     self.host = HOST
     self.symmetrical = False
     self.transmitter = 'arm'
     for arg in kwargs:
         if arg == 'host':
             self.host = kwargs[arg]
         elif arg == 'port':
             self.port = int(kwargs[arg])
         elif arg == 'symmetrical':
             self.symmetrical = kwargs[arg]
         elif arg == 'transmitter':
             self.transmitter = kwargs[arg]
         else:
             print("Warning: unexpected '%s' argument" % arg,
                   file=sys.stderr)
     self.protos = protos.load()
     self.error = QtGui.QErrorMessage()
     self.initUI()
Пример #4
0
    def __init__(self, stream, callback, **kwargs):

        self._logger = getLogger("comm.channel")

        transmitter = "arm"
        follow = False
        proto = None
        self._symmetrical = False

        for arg in kwargs:
            if arg == "transmitter":
                if kwargs[arg] != None:
                    transmitter = kwargs[arg]
            elif arg == "follow":
                if kwargs[arg] != None:
                    follow = kwargs[arg]
            elif arg == "proto":
                if kwargs[arg] != None:
                    proto = kwargs[arg].capitalize()
            elif arg == "symmetrical":
                if kwargs[arg] != None:
                    self._symmetrical = kwargs[arg]
            else:
                self._logger.warning("unexpected '%s' argument" %arg)

        self._stream = stream

        import protos
        self._protos = protos.load()
        self._proto = None
        self._proto_name = None

        if proto:
            try:
                self._proto = self._protos[proto]
            except KeyError:
                pass

            if not self._proto:
                raise Exception("Unknow protocol '%s', " \
                        "available protocols: %s" \
                        %(proto, ','.join(self._protos.keys())))

            for packet_name in self._proto['packets']:
                packet = self._proto['packets'][packet_name]
                if packet['transmitter'] == transmitter or packet['transmitter'] == 'both' or transmitter == 'both':
                    self.__setattr__(packet_name, self._create_method(packet_name, packet))
            self._proto_name = proto.lower()

        def recv(id, args):
            if id == -1:
                callback(None, args)
                return
            if self._proto == None:
                if id == 255:
                    if len(args) != 1:
                        self._logger.warning("invalid arguments count for id %d" %id)
                        return
                    board = args[0]
                    for proto_name in self._protos:
                        proto = self._protos[proto_name]
                        if proto["id"] == board:
                            self._proto = proto
                            self._proto_name = proto_name.lower()
                            self.logger.info("Loading proto '%s'" %proto_name)
                            recv(id, args)
                    if self._proto == None:
                        self._logger.warning("unknow board %d" %board)
                else:
                    self._logger.info("no protocol loaded, can't decode id %d" %id)
            else:
                know_packet = False
                for packet_name in self._proto['packets']:
                    packet = self._proto['packets'][packet_name]
                    if packet['id'] == id:
                        know_packet = True
                        break
                if not know_packet:
                    self._logger.warning("unknow packet id %d" %id)
                    return
                if packet['transmitter'] == transmitter and packet['transmitter'] != 'both' and transmitter != 'both':
                    self._logger.info("ignoring %s message" %packet['transmitter'])
                    return
                if len(packet['args']) != len(args) \
                        and len(packet['args']) + 2 != len(args):
                    self._logger.warning("packet with id %d expected " \
                            "%d arguments, %d was given"
                            %(id, len(packet['args']), len(args)))
                    return
                arguments = dict(zip(packet['args'], args))
                if len(args) == len(packet['args']) + 2:
                    arguments['timestamp'] = args[-2]
                    arguments['microseconds'] = args[-1]
                if self._symmetrical:
                    symmetrical(self._proto_name, packet_name, arguments)
                callback(packet_name, arguments)

        thread = decode(stream, recv, follow)
Пример #5
0
class ATPHandler(http.server.SimpleHTTPRequestHandler):

    protos = protos.load()

    def do_POST(self):
        self.do(True)

    def do_GET(self):
        self.do()

    def do(self, post=False):
        o = urllib.parse.urlparse(self.path)
        params = urllib.parse.parse_qs(o.query)
        if 'proto' in params:
            proto = params['proto'][0]
            if proto in self.protos:
                if 'packet' in params and post:
                    packet = params['packet'][0]
                    form = cgi.FieldStorage(fp=self.rfile,
                                            headers=self.headers,
                                            environ={
                                                'REQUEST_METHOD':
                                                'POST',
                                                'CONTENT_TYPE':
                                                self.headers['Content-Type'],
                                            })
                    if packet in self.protos[proto]['packets']:
                        p = self.protos[proto]['packets'][packet]
                        args = []
                        for arg in p['args']:
                            value = form.getfirst(packet + '_' + arg)
                            if value:
                                args.append(value)
                            else:
                                break
                        if len(p['args']) == len(args):
                            self.flashmsg = self.send_packet(
                                proto, packet, args)
                        else:
                            pass
                        self.form_proto(proto, self.protos[proto])
                    else:
                        self.send_error(404, "Unknow protocole name")
                        self.end_headers()
                else:
                    self.form_proto(proto, self.protos[proto])
            else:
                self.send_error(404, "Unknow protocole name")
                self.end_headers()
        else:
            self.home()

    def send_packet(self, proto, packet, args):
        chan = self.channels[proto]
        try:
            chan.send(None, self.protos[proto]['packets'][packet], *args)
            return None
        except Exception as e:
            return str(e)

    def form_proto(self, proto_name, proto):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

        template = Template(template_page)

        body = Template(template_proto).substitute(PROTO=proto_name)

        for packet in proto['packets']:
            body += self.form_packet(proto_name, packet,
                                     proto['packets'][packet])
        params = {}
        params['TITLE'] = 'ATP Sender'
        params['BANNER'] = self.banner()
        params['BODY'] = body

        self.wfile.write(bytes(template.substitute(params), 'utf-8'))

    def form_packet(self, proto_name, packet_name, packet):
        params = {}
        params['PROTO'] = proto_name
        params['PACKET'] = packet_name
        params['ANCRE'] = 'P' + packet_name
        params['ARGS'] = self.form_args(packet_name, packet)
        return Template(template_packet).substitute(params)

    def form_args(self, packet_name, packet):
        args = ''
        for arg in packet['args']:
            params = {}
            params['PACKET'] = packet_name
            params['ARG'] = arg
            args += Template(template_arg).substitute(params)
        return args

    def banner(self, pending_proto=None):
        banner = ''
        template = Template(template_banner)
        for proto in self.protos:
            if proto == pending_proto:
                active = ' class="active"'
            else:
                active = ''
            banner += template.substitute(ACTIVE=active, PROTO=proto)
        return banner

    def home(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

        template = Template(template_page)

        args = {}

        args['TITLE'] = 'ATP Sender'
        args['BANNER'] = self.banner()
        args['BODY'] = """
            <div class="hero-unit">
                <h1>ATP Sender</h1>
                <p>Please choose a protocole to start sending messages.</p>
            </div>"""

        self.wfile.write(bytes(template.substitute(args), 'utf-8'))