Exemplo n.º 1
0
    def start(self):
        addr = ('127.0.0.1', 2620)
        s = socket.socket()
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(addr)
        s.listen(10)
        while True:
            conn, addr = s.accept()
            print("new conn\n")

            while True:
                data = conn.recv(4)
                d = bytearray(data)
                x, y, size = struct.unpack(">ccH", d)
                body = conn.recv(size - 4)
                m = fpm.Message()
                m.ParseFromString(body)
                if m.add_route:
                    dst = bytes2Ip(m.add_route.key.prefix.bytes) + '/' + str(
                        m.add_route.key.prefix.length)
                    output = ifIdtoPortId(m.add_route.nexthops[0].if_id.index)
                    if output is not None:
                        self.add_flow(dst, str(output))
                        end = time.time()
                        with open('/tmp/result', 'a') as f:
                            f.write("%f\n" % end)
Exemplo n.º 2
0
def main():

    addr = ('127.0.0.1', 2620)
    s = socket.socket()
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(addr)
    s.listen(10)
    flow_count = 1

    sock_size_buf = array.array('i', [0])

    while True:
        conn, addr = s.accept()
        logger.info('new conn')

        while True:
            try:
                data = conn.recv(4)
                d = bytearray(data)
                x, y, size = struct.unpack('>ccH', d)
                body = conn.recv(size - 4)
                m = fpm.Message()
                m.ParseFromString(body)
                logger.info(m)
                if m.HasField('add_route'):
                    dst = bytes2Ip(m.add_route.key.prefix.bytes) + '/' + str(
                        m.add_route.key.prefix.length)
                    output = ifIdtoPortId(m.add_route.nexthops[0].if_id.index)
                    if output is not None:
                        add_flow(dst, str(output))
                        logger.info(str(flow_count))
                        flow_count = flow_count + 1
                elif m.HasField('delete_route'):
                    dst = bytes2Ip(
                        m.delete_route.key.prefix.bytes) + '/' + str(
                            m.delete_route.key.prefix.length)
                    delete_flow(dst)
                else:
                    logger.info('UNKNOWN message type')

                time.sleep(0.000001)
                fcntl.ioctl(conn, termios.FIONREAD, sock_size_buf)
                if sock_size_buf[0] == 0:
                    logger.info('empty sock read')
                    request_update()

            except (SystemExit, KeyboardInterrupt):
                raise
            except Exception:
                logger.error('Faild', exc_info=True)
Exemplo n.º 3
0
os.system(cmd)

addr = ('127.0.0.1', 2620)
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(10)
while True:
    conn, addr = s.accept()
    print("new conn")

    while True:
        data = conn.recv(4)
        d = bytearray(data)
        x, y, size = struct.unpack(">ccH", d)
        body = conn.recv(size - 4)
        m = fpm.Message()
        m.ParseFromString(body)
        print(m)
        if m.add_route:
            dst = bytes2Ip(m.add_route.key.prefix.bytes) + '/' + str(m.add_route.key.prefix.length)
            output = ifIdtoPortId(m.add_route.nexthops[0].if_id.index)
            if output is not None:
                start = time.time()
                add_flow(dst, str(output))
                end = time.time()
                used = end - start
                with open('/tmp/result', 'a') as f:
                    #f.write(repr(end) + '\n')
                    f.write("%f\n" % end)