⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dynamicschedulerimpl.java.svn-base

📁 网络模拟器
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
        }
        if(receiver == null)
        {
            System.err.println("Trying to send a packet TO a non-existant node: " + receiverIPAddr);
            return;
        }

        //Determine the time to schedule
        double execTime = (System.currentTimeMillis() - m_startTime) / 1000.00;
        //Create the necessary Command object
        PacketSender sendEvent = new PacketSender(sender.getIPHandler(), receiverIPAddr, execTime, data);

        m_simulator.schedule(sendEvent);
    }

    /**
     * Schedules a multicast send on the simulator
     */
    public synchronized void scheduleMulticast(InetAddress senderIPAddress, InetAddress multicastGrp, byte[] data) throws RemoteException
   {
        IPAddr senderIPAddr = new IPAddr(senderIPAddress);
        IPAddr multicastGroup = new IPAddr(multicastGrp);

        // First check that the sender node exists in the simulator
        Node sender = (Node) m_nodes.get(senderIPAddr.toString());
        if(sender == null)
        {
            System.err.println("Trying to multicast a packet FROM a non-existant node: " + senderIPAddr);
            return;
        }
        //Could check that the multicastGroup is a multicast address? Todo perhaps...

        //Determine the time to schedule
        double execTime = (System.currentTimeMillis() - m_startTime) / 1000.00;

        //Create the necessary Command object
        PacketSender sendEvent = new PacketSender(sender.getIPHandler(), multicastGroup, execTime, data);

        m_simulator.schedule(sendEvent);
    }

    /**
     * Receives the next message for the IPAddr indicated, will block if
     * untill there is a message available
     *
     * TODO: return more information that might be required to properly set
     *       a datagram.
     */
    public byte[] receive(InetAddress receiverIPaddr) throws RemoteException
    {
        IPAddr recv = new IPAddr(receiverIPaddr);
        Queue incoming = (Queue) m_msgQueues.get(recv.toString());
        IPPacket ipPacket = null;
        synchronized(incoming)
        {
            try
            {
                while(incoming.size() == 0)
                {
                    incoming.wait();
                }
            }
            catch(InterruptedException e)
            {
                e.printStackTrace();
            }
            ipPacket = (IPPacket) incoming.peekFront();
            incoming.popFront();
        }

        byte[] data = (byte[])ipPacket.data;

        return data;

    }

    /**
     * This method will stop the simulator.
     * TODO: Change this so the simulator does not stop untill all the participants have
     *       called stop()
     */
    public void stop() throws RemoteException
    {
        //Determine the time to schedule
        double execTime = (System.currentTimeMillis() - m_startTime) / 1000.00;
        m_simulator.schedule(new StopCommand(execTime));
    }

    /**
     * This method will start the simulator. This is called by the main() method in this class.
     */
    public synchronized void start()
    {
        new Thread(m_simulator).start();
        m_startTime = (double) System.currentTimeMillis();
        m_started = true;
    }

    //-----------------------------------------
    // The Agent interface functions below here
    //-----------------------------------------
    /**
     * Normally, this would attach a higher level agent to this agent interface. However, as
     * the dynamic scheduler propagates all its packets to external clients, trying to
     * attach a higher level agent does not make any sense.
     * So calling this function will result in a Simulator error.
     *
     */
    public void attach(Agent higher_level, int unique_id)
    {
        Simulator.error("A higher level agent tried to attach itself to the dynamic scheduler. This makes no sense. See documentation.");
    }

    /**
     *
     *   Attach a lower-level agent to this agent. This function is used as a
     *   callback. When a higher-level agent attaches itself to a lower level
     *   agent, the lower level agent will attach itself to the higher-level
     *   agent using this function.<br>
     *   The purpose of this is that the higher-level agent will implement this
     *   function in order to store some reference to the lower-level service
     *   which it can use to give packets to it.<br>
     *   Do NOT call this function as a user of the agent, only agents between
     *   themselves need to use it.
     *
     *   In the dynamic scheduler, this function gets called by the node's
     *   IPHandler when the the dynamic scheduler gets attached to the
     *   IPHandler. This is so that packets can be passed up to the dynamic
     *   scheduler
     *
     * @param lower_level the lower level agent to attach to this agent.
     */
    public void attach(Agent lower_level)
    {
        //Does nothing, as I have a reference to all the IPHandlers through
        //the m_nodes hashmap.
    }

    /**
     Indicate to this agent that a packet is waiting for collection. This
     is used by lower-level agents to indicate to higher-level agents.
     Note that higher-level agents may ignore any of the requests!
     @param status one of the constants defined above.
     @param indicator the object that is indicating, normally an Agent, but
     could be an Interface for example.
     */
    public void indicate(int status, Object indicator)
    {
        if(status == Agent.PACKET_AVAILABLE)
        {
            //It will be a node's IPHandler
            if(indicator instanceof IPHandler)
            {
                //Get the handler
                IPHandler ipHandler = (IPHandler) indicator;
                //Get the IP packet
                IPPacket packet = (IPPacket) ipHandler.read(Protocols.UDP);
                //Determine the destination
                IPAddr addr = ipHandler.getAddress();

                //Add the packet, needs to be synchronized.
                Queue incoming = (Queue) m_msgQueues.get(addr.toString());
                synchronized(incoming)
                {
                    incoming.pushBack(packet);
                    //Need to notify, so that if anyone waiting to receive, will know
                    //it has been updated....
                    incoming.notifyAll();

                }
            }
            else
            {
                Simulator.error("Something other than an IPHandler called indicate() on the DynamicScheduler!");
            }
        }

    }

    /**
     Query this agent if a packet of a given length could be sent. You must
     not call the 'send' function of any agent before calling this function.
     There are two possibilities:
     <ul>
     <li>The function returns 'true' - Go ahead and call 'send'
     <li>The function returns 'false' - In this case you are guaranteed to
     get a READY_TO_SEND indication by the lower level agent you are using.
     Do not try to send any packets but wait for a call to your indicate
     function with that flag set. When you do get that call, you <b>must</b>
     call canSend() again because someone else might take their turn before
     you!
     </ul>
     @param destination where to send the packet to
     @param length the length of the data to be sent
     */
    public boolean canSend(IPAddr destination, int length)
    {
        return false;
    }
    //--------------------------------
    // The main() function below here
    //--------------------------------


    /**
     * This function is an example of how to use the DynamicScheduler.
     */
    public static void main(String[] args)
    {

        try
        {
            //Create the DynamicSchedulerImpl
            DynamicSchedulerImpl scheduler = new DynamicSchedulerImpl("mySimulationRun", 500000, 0.008, 0.0, new IPAddr(255, 255, 255, 0));

            //Add nodes to this simulation run
            scheduler.addNode(new IPAddr(192, 168, 0, 1));
            scheduler.addNode(new IPAddr(192, 168, 0, 2));
            scheduler.addNode(new IPAddr(192, 168, 0, 3));
            /*scheduler.addNode(new IPAddr(192, 168, 0, 4));
            scheduler.addNode(new IPAddr(192, 168, 0, 5));
            scheduler.addNode(new IPAddr(192, 168, 0, 6));
            scheduler.addNode(new IPAddr(192, 168, 0, 7));
            scheduler.addNode(new IPAddr(192, 168, 0, 8));
            scheduler.addNode(new IPAddr(192, 168, 0, 9)); */
            scheduler.addLink(new IPAddr(192, 168, 0, 1), new IPAddr(192, 168, 0, 2));
            /*scheduler.addLink(new IPAddr(192, 168, 0, 1), new IPAddr(192, 168, 0, 3));
            scheduler.addLink(new IPAddr(192, 168, 0, 1), new IPAddr(192, 168, 0, 4));
            scheduler.addLink(new IPAddr(192, 168, 0, 2), new IPAddr(192, 168, 0, 4));
            scheduler.addLink(new IPAddr(192, 168, 0, 3), new IPAddr(192, 168, 0, 4));
            scheduler.addLink(new IPAddr(192, 168, 0, 3), new IPAddr(192, 168, 0, 6));
            scheduler.addLink(new IPAddr(192, 168, 0, 4), new IPAddr(192, 168, 0, 5));
            scheduler.addLink(new IPAddr(192, 168, 0, 4), new IPAddr(192, 168, 0, 6));
            scheduler.addLink(new IPAddr(192, 168, 0, 4), new IPAddr(192, 168, 0, 8));
            scheduler.addLink(new IPAddr(192, 168, 0, 5), new IPAddr(192, 168, 0, 8));
            scheduler.addLink(new IPAddr(192, 168, 0, 5), new IPAddr(192, 168, 0, 9));
            scheduler.addLink(new IPAddr(192, 168, 0, 6), new IPAddr(192, 168, 0, 8));
            scheduler.addLink(new IPAddr(192, 168, 0, 6), new IPAddr(192, 168, 0, 7));
            scheduler.addLink(new IPAddr(192, 168, 0, 8), new IPAddr(192, 168, 0, 7));
            scheduler.addLink(new IPAddr(192, 168, 0, 8), new IPAddr(192, 168, 0, 9));
            //scheduler.addLink(new IPAddr(192, 168, 0, 3), new IPAddr(192, 168, 0, 4));  */
             scheduler.addLink(new IPAddr(192, 168, 0, 2), new IPAddr(192, 168, 0, 3));
            //start the simulation run
            scheduler.start();
            //register the simulation run in the RMIRegistry
            Registry reg = LocateRegistry.createRegistry(3778);
            reg.rebind("DynamicScheduler", scheduler);
            System.out.println("The DynamicScheduler has started.");

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

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -