Pergunta

Eu estou fazendo o meu trabalho em arquitetura de rede 1, onde eu tenho que implementar um vetor de distância de roteamento em cada nó.

Em cada nó, que tem uma rosca que escuta para DatagramPackets de entrada que contêm informações de encaminhamento de nódulos vizinha apenas num porta específica. Quando um datagrama chega, os processos de rosca que datagrama, e se há atualizações em suas tabelas de roteamento interno, em seguida, ele envia a sua informação de roteamento para todos os seus vizinhos.

Eu estou tentando fazê-lo em Java.

O problema que estou enfrentando é que quando um datagrama chega, eu preciso para processá-lo. Se durante esse tempo qualquer outro datagrama chega, ele é descartado, como o fio está processando informações. Isso significa que tenho uma perda de informação.

qualquer um pode me ajudar com isso?

Eu estou usando o modo normal de leitura de um soquete em java.

DatagramSocket socket = new DatagramSocket(4445, InetAddress.getByName("127.0.0.1"));
while (true) {
    try {
        byte[] buf = new byte[2000];

        // receive request
        DatagramPacket recvRequest = new DatagramPacket(buf, buf.length);

        socket.receive(recvRequest);

        //Some process of data in datagram

    } catch (IOException e) {
        e.printStackTrace();
    }
}
Foi útil?

Solução

DatagramSocket socket = new DatagramSocket (4445, InetAddress.getByName ( "127.0.0.1")); while (true) { experimentar { // nota final .. byte final [a] buf = new byte [2000];

    // receive request
    DatagramPacket recvRequest = new DatagramPacket(buf, buf.length);

    socket.receive(recvRequest);

    //Some process of data in datagram
    (new Thread(new Runnable() {
        public void run () {
            // do stuff with data in buf
            ...
        }
    })).start();

} catch (IOException e) {
    e.printStackTrace();
}

}

Outras dicas

Você pode processar o datagrama recebido em um fio, de modo que a sua discussão com o ouvinte soquete pode continuar a receber novas datagramas.

Este projeto final que eu submetidos. Ele pode estar tendo alguma documentação inadequada e algum mau uso de Java. Como este projeto é executado no sistema local, em vez de usar o endereço IP diferente e mesmo número de porta, eu estou fazendo isso outra maneira.

NetworkBoot.java fornece os detalhes iniciais vizinho para cada roteador.

Graças -Sunny Jain

enter code here

/ * * Nome do arquivo: Router.java * Nome da classe pública: Router * * /

// ~ --- importações JDK --------------------------------------- ---------------------

import java.io.IOException;

importação java.net.DatagramPacket;

importação java.net.DatagramSocket;

importação java.net.InetAddress;

importação java.util.HashMap;

importação java.util.Iterator;

importação java.util.Set;

importação java.util.concurrent.LinkedBlockingQueue;

javax.swing.SwingUtilities importação;

/ ** * * Projeto NA1 2 Primavera de 2009 semestre * @Author Jain ensolarado * * * /

classe pública Router estende Thread {

/**
 * HashMap containing list of neighbors and cost to reach them.
 */
private HashMap<Integer, Integer> hmapDirectNeighbours = new HashMap<Integer, Integer>(61);
/**
 * HashMap containing list of destination as key and routing info to them as value.
 * Routing info contains RouteDetail object.
 * @see RouteDetail
 */
private HashMap<Integer, RouteDetail> hmapRoutes = new HashMap<Integer, RouteDetail>();
/**
 * DatagramSocket
 */
private DatagramSocket dSoc;
/**
 * DatagramPacket
 */
private DatagramPacket dpackReceive,  dpackSend;
/**
 * Inetaddress of system on which runs this algorithm.
 */
private InetAddress localAddress;
/**
 * port to listen at for incoming route info from neighbors.
 */
int port;
private LinkedBlockingQueue<DatagramPacket> lbq = new LinkedBlockingQueue<DatagramPacket>();

/**
 * Made constructor private to force initialization by specifying port
 * compulsory.
 */
private Router() {
}

/**
 * Constuctor taking port number as parameter and creates a datagramSocket
 * to listen for incoming DatagramPacket on that socket.
 * @param port
 */
public Router(int port) {
    try {
        this.port = port;
        localAddress = InetAddress.getByName("127.0.0.1");
        dSoc = new DatagramSocket(port, localAddress);
    } catch (Exception ex) {
        System.out.println("Error while creating socket : " + ex.getMessage());
    }
    this.start();

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            while (true) {
                try {
                    received_Route_Info(lbq.take());
                } catch (InterruptedException ex) {
                    System.out.println("Error while reading elements from datagram queue");
                }}}});
}

public void setRouterBootInfo(String strNeighboursInfo) {
    String[] strNeighbouringNodes = strNeighboursInfo.split(";");

   for (int i = 0; i < strNeighbouringNodes.length; i++) {

        String[] strNodeIpAndPort = strNeighbouringNodes[i].split(":");

        hmapDirectNeighbours.put(Integer.valueOf(strNodeIpAndPort[0]), Integer.valueOf(strNodeIpAndPort[1]));
        hmapRoutes.put(Integer.valueOf(strNodeIpAndPort[0]), new RouteDetail(null, Integer.valueOf(strNodeIpAndPort[1])));
    }
    propagateChanges();
// entry in Route table....No need for infinity as we creat entry when a node is reachable.
}

@Override
public void run() {
    while (true) {
        try {
            byte[] buf = new byte[250];
            // receive request
            dpackReceive = new DatagramPacket(buf, buf.length);
            dSoc.receive(dpackReceive);
            lbq.put(dpackReceive);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
            dSoc.close();
        } catch (IOException e) {
            e.printStackTrace();
            dSoc.close();
        }
    }


}

/**
 * This method is called for each DatagramPacket received containing new
 * routing information.
 *
 * This method checks whether this packet came from neighboring node
 * (routers) only. If true it applies Distance vector algorithm on data
 * present in datagram packet and due to this information if their is any
 * change in local routing information that it displays current local
 * updated routing information and also sends this updated information to
 * other neighbours only.
 *
 * @param dataPckt
 * @see  #validate_Is_Packet_From_Neighbor(java.net.DatagramPacket)
 * @see #apply_Routing_Algorithm(java.net.DatagramPacket, java.util.HashMap)
 * @see #print_route_info()
 * @see #send_Updates_To_Neighbors(routesInfo)
 */
private void received_Route_Info(DatagramPacket dataPckt) {
    if (dataPckt.getPort() == 4000) {
        setRouterBootInfo(getStringFromBytes(dataPckt));
    } else if (validate_Is_Packet_From_Neighbor(dataPckt)) {
        if (apply_Routing_Algorithm(dataPckt, create_HashMap_Routes(getStringFromBytes(dataPckt)))) {

            // if their is change in routing information.
            propagateChanges();
        }
    }
}

/**
 * Validates whether the Datagram packet received is from the neighbors only.
 * @param datagrampckt DatagramPacket comtaining routing information.
 * @return true if datagrampckt is from neighbors only otherwise false.
 */
private boolean validate_Is_Packet_From_Neighbor(DatagramPacket datagrampckt) {
    return hmapDirectNeighbours.containsKey(Integer.valueOf(datagrampckt.getPort()));
}

/**
 * Returns byte representaion of data contained in DatagramPacket pkt.
 * @param pkt DatagramPacket
 * @return byte representation of data contained in pkt
 */
private String getStringFromBytes(DatagramPacket pkt) {
    String strData = new String(pkt.getData());
    return strData.substring(0, strData.lastIndexOf(';'));
}

/**
 * Applies Distance Vector algorithm using newly received routing information
 * and information presently with this node (Router).
 * @param datagrampckt DatagramPacket containing routing information.
 * @param newRoutes HashMap of routes new information received with
 * destination as key and cost to that destination as value.
 */
private boolean apply_Routing_Algorithm(DatagramPacket dataPckt, HashMap<Integer, Integer> newRoutes) {
    boolean updated = false;
    Integer pktSourse = Integer.valueOf(dataPckt.getPort());

    // Get a set of the routes
    Set<Integer> set = newRoutes.keySet();

    // Get an iterator
    Iterator<Integer> iterator = set.iterator();

    // Display elements.
    while (iterator.hasNext()) {
        Integer key = iterator.next();
        Integer nextHopCost = hmapRoutes.get(pktSourse).getPathCost();
        int optionalCost = newRoutes.get(key) + (nextHopCost == null ? 0 : nextHopCost);
        if (hmapRoutes.containsKey(key)) {
            RouteDetail routeDetail = hmapRoutes.get(key);

            if (routeDetail.getPathCost().compareTo(optionalCost) > 0) {
                routeDetail.setNextHop(pktSourse);
                routeDetail.setPathCost(optionalCost);
                hmapRoutes.put(key, routeDetail);
                updated = true;

            // try to verify above statement
            }
        } else {
            if (!key.equals(port)) {
                RouteDetail newRouteDetail = new RouteDetail(pktSourse, optionalCost);
                hmapRoutes.put(key, newRouteDetail);
                updated = true;
            }
        }
    }

    return updated;
}

/**
 * When internal routing information is chaged, send this information to
 * other neighbors.
 * @param routesInfo byte representaion of routing information.
 */
private void send_Updates_To_Neighbors(byte[] routesInfo) {

    // Get a set of the routes
    Set<Integer> set = hmapDirectNeighbours.keySet();

    // Get an iterator
    Iterator<Integer> iterator = set.iterator();

    // Display elements.
    while (iterator.hasNext()) {
        dpackSend = new DatagramPacket(routesInfo, routesInfo.length, localAddress, iterator.next().intValue());

        try {
            dSoc.send(dpackSend);
        } catch (IOException ex) {
            System.out.println("Error while sending route updates : " + ex.getMessage());
        }
    }
}

/**
 * Parses routeInfo to creat an HashMap based on this informationin the
 * format as HashMap of <<Integer:Destination>,<Integer: Cost to this destination>>
 * @param routeInfo contains routing information as String in the syntax
 * of {<Destination>:<Cost to destination>;}
 * @return Hashmap<<Integer:Destination>,<Integer: Cost to this destination>>
 */
private HashMap<Integer, Integer> create_HashMap_Routes(String routeInfo) {
    HashMap<Integer, Integer> routes = new HashMap<Integer, Integer>();
    String[] straRoute = routeInfo.split(";");

    for (int i = 0; i < straRoute.length; i++) {
        String[] straDestAndCost = straRoute[i].split(":");

        routes.put(Integer.parseInt(straDestAndCost[0]), Integer.parseInt(straDestAndCost[1]));
    }

    return routes;
}

/**
 * Converts current routing information stored as HashMap to String
 * presentation in format as {<Destination>:<Cost to destination>;}
 *
 * @return String representaion of routing information.
 * @see #hmapRoutes.
 */
private String create_String_Of_Routes() {
    StringBuilder strB = new StringBuilder();

    // Get a set of the routes
    Set<Integer> set = hmapRoutes.keySet();

    // Get an iterator
    Iterator<Integer> iterator = set.iterator();

    // Display elements.
    while (iterator.hasNext()) {
        Integer destination = iterator.next();

        strB.append(destination);
        strB.append(":");
        strB.append(hmapRoutes.get(destination).getPathCost());
        strB.append(";");
    }

    return strB.toString();
}

/**
 * Prints the current routing information stored in <code>hmapRoutes</code>
 * to default output stream of this program.
 * @see #hmapRoutes.
 */
public void print_route_info() {
    RouteDetail route;
    StringBuilder builder;

    // PRINT THE CURRENT ROUTING INFO AT THIS NODE
    System.out.println("");
    System.out.println("    TABLE AT NODE WITH PORT  : " + port);
    System.out.println("--------------------------------------------------------------------------------");
    System.out.println("\t\tTo  \t|\t Via\t|\tCost\t\t");
    System.out.println("--------------------------------------------------------------------------------");

    // Get a set of the routes
    Set<Integer> set = hmapRoutes.keySet();

    // Get an iterator
    Iterator<Integer> iterator = set.iterator();

    // Display elements.
    while (iterator.hasNext()) {
        Integer key = iterator.next();

        route = hmapRoutes.get(key);
        builder = new StringBuilder();
        builder.append("\t\t" + key.intValue());
        builder.append("\t|\t" + (route.getNextHop() == null ? "   -" : route.getNextHop()));
        builder.append("\t|\t" + route.getPathCost() + "\t\t");
        System.out.println(builder.toString());
    }
}

/**
 * This class provides details for each destination.
 * It provides detail of cost that will be incurred to reach that
 * destination and next router on that path.
 */

classe privada RouteDetail {

    Integer nextHop;
    Integer pathCost;

    public RouteDetail(Integer nextHop, Integer pathCost) {
        this.nextHop = nextHop;
        this.pathCost = pathCost;
    }

    public Integer getNextHop() {
        return nextHop;
    }

    public void setNextHop(Integer nextHop) {
        this.nextHop = nextHop;
    }

    public Integer getPathCost() {
        return pathCost;
    }

    public void setPathCost(Integer pathCost) {
        this.pathCost = pathCost;
    }
}

private void propagateChanges() {
    print_route_info();
    send_Updates_To_Neighbors(create_String_Of_Routes().getBytes());
}

public static void main(String[] args) {
    new Router(Integer.parseInt(args[0]));
}

}

/ * * Nome do arquivo: NetworkBoot.java * Nome da classe pública: NetworkBoot * * /

import java.io.IOException;

importação java.net.DatagramPacket;

importação java.net.DatagramSocket;

importação java.net.InetAddress;

/ ** * * Projeto NA1 2 Primavera de 2009 semestre * @Author Jain ensolarado * * * /

classe pública NetworkBoot {

public static void main(String[] args) {
    try {
        DatagramSocket dSoc = new DatagramSocket(4000, InetAddress.getByName("127.0.0.1"));
        String[] sendD = {"4006:3;4007:5;4009:2;", "4005:3;4007:3;4008:6;", "4005:5;4006:3;", "4009:2;4006:6;", "4008:2;4005:2;"};
        for (int i = 0, port = 4005; i < 5; i++) {
            dSoc.send(new DatagramPacket(sendD[i].getBytes(), sendD[i].length(), InetAddress.getByName("127.0.0.1"), port++));
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

}

Eu não tenho feito isso em Java, mas, você pode (ou deve) passar mais de um buffer de datagrama simultânea à tomada (ou com vários segmentos cada invocando o synchrnonous método receber ou, de preferência com uma thread invocando uma asynchrnonous receber método mais de uma vez).

A vantagem de passar vários buffers de datagramas simultâneas para a tomada é óbvia: isto é, a tomada ainda terá um tampão (em que a receber o próximo datagrama) mesmo quando ela já preenchido um tampão (com um datagrama anterior) e passada que tampão de volta para você.

Você pode perguntar, "em que seqüência é que os buffers ser passados ??de volta para mim?" ea resposta é: "Não deveria importar." Se a seqüência em que você processa datagramas é importante, então os datagramas si deve conter um número sequencial (porque datagramas pode ficar fora de seqüência como são encaminhadas através da rede, com ou sem você passou múltipla simultânea para o socket local com a consequente possibilidade de "simultâneo" recebe a ser entregue de volta para você fora de seqüência).

Vale a pena lembrar é UDP transporte com perdas, minimizando a perda de pacotes é uma boa idéia que você nunca deve assumir que você vai ter todos os pacotes, (ou que os pacotes chegarão na ordem que você enviou)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top