Java GridSim:如何通过路由器在对等节点之间发送消息?

Java GridSim:如何通过路由器在对等节点之间发送消息?,java,grid,Java,Grid,我是Gridsim的新用户,目前希望将其用于我的工作。我希望通过路由器连接用户(比如2)。我遵循了example01(将pkt直接从发送者发送到接收者),并包括一个路由器,与两个用户连接。但我不知道如何通过路由器启动两个实体之间的通信。它返回空值。另一方面,当我使用link.attach时,两个用户都绕过路由器直接通信 Example03工作正常,但从用户向资源发送消息,但不向对等用户发送消息。 我希望你能帮我解决这个问题 我们将非常感谢在这方面提供的任何帮助 谢谢。我找到了解决办法。代码如下:

我是
Gridsim
的新用户,目前希望将其用于我的工作。我希望通过路由器连接用户(比如2)。我遵循了example01(将pkt直接从发送者发送到接收者),并包括一个路由器,与两个用户连接。但我不知道如何通过路由器启动两个实体之间的通信。它返回空值。另一方面,当我使用
link.attach
时,两个用户都绕过路由器直接通信

Example03工作正常,但从用户向资源发送消息,但不向对等用户发送消息。 我希望你能帮我解决这个问题

我们将非常感谢在这方面提供的任何帮助


谢谢。

我找到了解决办法。代码如下:

程序名称:FlowNetEx01.java

package network.flow.example01;

import gridsim.*;
import gridsim.net.*;
import gridsim.net.flow.*;
import java.util.*;

public class FlowNetEx01
{
    public static void main(String[] args)
    {
        System.out.println("Starting network example ...");

        try
        {
            int num_user = 2;   // number of grid users
            Calendar calendar = Calendar.getInstance();
            boolean trace_flag = false;  // mean trace GridSim events
            System.out.println("Initializing GridSim package");
            GridSim.initNetworkType(GridSimTags.NET_FLOW_LEVEL);
            GridSim.init(num_user, calendar, trace_flag);

            // In this example, the topology is:
            // user(s) --10Mb/s-- r1 --1.5Mb/s-- r2 --10Mb/s-- GridResource(s)

            Router r1 = new FlowRouter("router1", trace_flag);   // router 1
            Router r2 = new FlowRouter("router2", trace_flag);   // router 2

            String sender1 = "user1";
            String receipient1 = "test1";

            // these entities are the senders
            FlowNetUser user1 = new FlowNetUser(sender1, receipient1, 5.0);
            // these entities are the receipients
            FlowTest test1 = new FlowTest(receipient1, sender1);

            // The schedulers are redundent and will be stripped out soon
            FIFOScheduler userSched1 = new FIFOScheduler("NetUserSched_0");
            r1.attachHost(user1, userSched1);

            FIFOScheduler testSched1 = new FIFOScheduler("FlowTestSched_0");
            r2.attachHost(test1, testSched1);

            double baud_rate = 1572864; // bits/sec (baud) [1.5Mb/s]
            double propDelay = 200;   // propagation delay in millisecond
            int mtu = Integer.MAX_VALUE;;    // max. transmission unit in byte

            Link link = new FlowLink("r1_r2_link", baud_rate, propDelay, mtu);
            FIFOScheduler r1Sched = new FIFOScheduler("r1_Sched");
            FIFOScheduler r2Sched = new FIFOScheduler("r2_Sched");
            r1.attachRouter(r2, link, r1Sched, r2Sched);
            GridSim.startGridSimulation();
            System.out.println("\nFinish ...");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.err.print(e.toString());
            System.out.println("Unwanted errors happen");
        }
    }
} 
程序名称:FlowNetUser.java

package network.flow.example01;

import gridsim.*;
import gridsim.net.*;
import gridsim.net.flow.*;
import eduni.simjava.*;
import java.util.*;

public class FlowNetUser extends GridSim
{
    private int myID_;          // my entity ID
    private String name_;       // my entity name
    private String destName_;   // destination name
    private int destID_;        // destination id
    private double wait_;       // Delay until I begin sending

    /** Custom tag that denotes sending a message */
    public static final int SEND_MSG = 1;    
    public static final int ACK_MSG = 2;


    /**
     * Creates a new NetUser object
     * @param name      this entity name
     * @param destName  the destination entity's name
     * @param link      the physical link that connects this entity to destName
     * @throws Exception    This happens when name is null or haven't 
     *                      initialized GridSim.
     */
    public FlowNetUser(String name, String destName, Link link, double wait) throws Exception
    {
        super(name, link);

        // get this entity name from Sim_entity
        this.name_ = super.get_name();

        // get this entity ID from Sim_entity
        this.myID_ = super.get_id();

        // get the destination entity name
        this.destName_ = destName;

        // get the waiting time before sending
        this.wait_ = wait;
    }

    public FlowNetUser(String name, String destName, double wait) throws Exception
    {
        // 10,485,760 baud = 10Mb/s
        super(name, new FlowLink(name+"_link",10485760,450,Integer.MAX_VALUE));

        // get this entity name from Sim_entity
        this.name_ = super.get_name();

        // get this entity ID from Sim_entity
        this.myID_ = super.get_id();

        // get the destination entity name
        destName_ = destName;

        // get the waiting time before sending
        this.wait_ = wait;
    }

    /**
     * The core method that handles communications among GridSim entities.
     */
    public void body()
    {
        int packetSize = 524288000;   // packet size in bytes [5MB]
        //int packetSize = 52428800;   // packet size in bytes [50MB]
        //int packetSize = 524288000;   // packet size in bytes [500MB]
        //int packetSize = 5242880000;   // packet size in bytes [5000MB]
        int size = 3;           // number of packets sent
        int i = 0;

        // get the destination entity ID
        this.destID_ = GridSim.getEntityId(destName_);

        //super.sim_pause(this.wait_);
        this.gridSimHold(this.wait_);


        // sends messages over the other side of the link
        for (i = 0; i < size; i++)
        {

            String msg = "Message_" + i;
            IO_data data = new IO_data(msg, packetSize, destID_);
            System.out.println(name_ + ".body(): Sending " + msg +
                ", at time = " + GridSim.clock() );

            // sends through Output buffer of this entity
            super.send(super.output, GridSimTags.SCHEDULE_NOW,
                       GridSimTags.FLOW_SUBMIT, data);

            //super.sim_pause();
            super.sim_pause(10.0);
            //this.gridSimHold((Math.random()*10)+1.0);

        }

        ////////////////////////////////////////////////////////
        // get the ack back
        Object obj = null;
        for (i = 0; i < size; i++)
        {
            // waiting for incoming event in the Input buffer
            obj = super.receiveEventObject();
            System.out.println(name_ + ".body(): Receives Ack for " + obj);
        }         


        // Wait for other FlowNetUser instances to finish
        this.gridSimHold(1000.0);


        super.send(destID_, GridSimTags.SCHEDULE_NOW,
                   GridSimTags.END_OF_SIMULATION);


        ////////////////////////////////////////////////////////
        // shut down I/O ports
        shutdownUserEntity();
        terminateIOEntities();

        System.out.println(this.name_ + ":%%%% Exiting body() at time " +
                           GridSim.clock() );
    }

} // end class

我找到了解决办法。代码如下:

程序名称:FlowNetEx01.java

package network.flow.example01;

import gridsim.*;
import gridsim.net.*;
import gridsim.net.flow.*;
import java.util.*;

public class FlowNetEx01
{
    public static void main(String[] args)
    {
        System.out.println("Starting network example ...");

        try
        {
            int num_user = 2;   // number of grid users
            Calendar calendar = Calendar.getInstance();
            boolean trace_flag = false;  // mean trace GridSim events
            System.out.println("Initializing GridSim package");
            GridSim.initNetworkType(GridSimTags.NET_FLOW_LEVEL);
            GridSim.init(num_user, calendar, trace_flag);

            // In this example, the topology is:
            // user(s) --10Mb/s-- r1 --1.5Mb/s-- r2 --10Mb/s-- GridResource(s)

            Router r1 = new FlowRouter("router1", trace_flag);   // router 1
            Router r2 = new FlowRouter("router2", trace_flag);   // router 2

            String sender1 = "user1";
            String receipient1 = "test1";

            // these entities are the senders
            FlowNetUser user1 = new FlowNetUser(sender1, receipient1, 5.0);
            // these entities are the receipients
            FlowTest test1 = new FlowTest(receipient1, sender1);

            // The schedulers are redundent and will be stripped out soon
            FIFOScheduler userSched1 = new FIFOScheduler("NetUserSched_0");
            r1.attachHost(user1, userSched1);

            FIFOScheduler testSched1 = new FIFOScheduler("FlowTestSched_0");
            r2.attachHost(test1, testSched1);

            double baud_rate = 1572864; // bits/sec (baud) [1.5Mb/s]
            double propDelay = 200;   // propagation delay in millisecond
            int mtu = Integer.MAX_VALUE;;    // max. transmission unit in byte

            Link link = new FlowLink("r1_r2_link", baud_rate, propDelay, mtu);
            FIFOScheduler r1Sched = new FIFOScheduler("r1_Sched");
            FIFOScheduler r2Sched = new FIFOScheduler("r2_Sched");
            r1.attachRouter(r2, link, r1Sched, r2Sched);
            GridSim.startGridSimulation();
            System.out.println("\nFinish ...");
        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.err.print(e.toString());
            System.out.println("Unwanted errors happen");
        }
    }
} 
程序名称:FlowNetUser.java

package network.flow.example01;

import gridsim.*;
import gridsim.net.*;
import gridsim.net.flow.*;
import eduni.simjava.*;
import java.util.*;

public class FlowNetUser extends GridSim
{
    private int myID_;          // my entity ID
    private String name_;       // my entity name
    private String destName_;   // destination name
    private int destID_;        // destination id
    private double wait_;       // Delay until I begin sending

    /** Custom tag that denotes sending a message */
    public static final int SEND_MSG = 1;    
    public static final int ACK_MSG = 2;


    /**
     * Creates a new NetUser object
     * @param name      this entity name
     * @param destName  the destination entity's name
     * @param link      the physical link that connects this entity to destName
     * @throws Exception    This happens when name is null or haven't 
     *                      initialized GridSim.
     */
    public FlowNetUser(String name, String destName, Link link, double wait) throws Exception
    {
        super(name, link);

        // get this entity name from Sim_entity
        this.name_ = super.get_name();

        // get this entity ID from Sim_entity
        this.myID_ = super.get_id();

        // get the destination entity name
        this.destName_ = destName;

        // get the waiting time before sending
        this.wait_ = wait;
    }

    public FlowNetUser(String name, String destName, double wait) throws Exception
    {
        // 10,485,760 baud = 10Mb/s
        super(name, new FlowLink(name+"_link",10485760,450,Integer.MAX_VALUE));

        // get this entity name from Sim_entity
        this.name_ = super.get_name();

        // get this entity ID from Sim_entity
        this.myID_ = super.get_id();

        // get the destination entity name
        destName_ = destName;

        // get the waiting time before sending
        this.wait_ = wait;
    }

    /**
     * The core method that handles communications among GridSim entities.
     */
    public void body()
    {
        int packetSize = 524288000;   // packet size in bytes [5MB]
        //int packetSize = 52428800;   // packet size in bytes [50MB]
        //int packetSize = 524288000;   // packet size in bytes [500MB]
        //int packetSize = 5242880000;   // packet size in bytes [5000MB]
        int size = 3;           // number of packets sent
        int i = 0;

        // get the destination entity ID
        this.destID_ = GridSim.getEntityId(destName_);

        //super.sim_pause(this.wait_);
        this.gridSimHold(this.wait_);


        // sends messages over the other side of the link
        for (i = 0; i < size; i++)
        {

            String msg = "Message_" + i;
            IO_data data = new IO_data(msg, packetSize, destID_);
            System.out.println(name_ + ".body(): Sending " + msg +
                ", at time = " + GridSim.clock() );

            // sends through Output buffer of this entity
            super.send(super.output, GridSimTags.SCHEDULE_NOW,
                       GridSimTags.FLOW_SUBMIT, data);

            //super.sim_pause();
            super.sim_pause(10.0);
            //this.gridSimHold((Math.random()*10)+1.0);

        }

        ////////////////////////////////////////////////////////
        // get the ack back
        Object obj = null;
        for (i = 0; i < size; i++)
        {
            // waiting for incoming event in the Input buffer
            obj = super.receiveEventObject();
            System.out.println(name_ + ".body(): Receives Ack for " + obj);
        }         


        // Wait for other FlowNetUser instances to finish
        this.gridSimHold(1000.0);


        super.send(destID_, GridSimTags.SCHEDULE_NOW,
                   GridSimTags.END_OF_SIMULATION);


        ////////////////////////////////////////////////////////
        // shut down I/O ports
        shutdownUserEntity();
        terminateIOEntities();

        System.out.println(this.name_ + ":%%%% Exiting body() at time " +
                           GridSim.clock() );
    }

} // end class