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

📄 ratecontrolledrouter.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            super.write("");            super.write("receive incoming, " + pkt + ", delay, " + nextTime);            super.write("break this packet into, " + numPackets);        }        // break a large packet into smaller ones that fit into MTU        // by making null or empty packets except for the last one        for (int i = 0; i < numPackets - 1; i++)        {            NetPacket np = new NetPacket(null, pkt.getID(), MTU, tag,                                         pkt.getSrcID(), pkt.getDestID(),                                         pkt.getNetServiceType(),i+1,numPackets);            np.setLast( super.get_id() );            if (super.reportWriter_ != null) {                super.write("enqueing, " + np);            }            // put the packet into the scheduler            super.sim_schedule(sched.getSchedID(),0,GridSimTags.SCHEDULER_ENQUE, np);        }        // put the actual packet into the last one and resize it accordingly        pkt.setLast( super.get_id() );        pkt.setSize(pkt.getSize() - MTU * (numPackets - 1));        if (super.reportWriter_ != null) {            super.write("enqueing, " + pkt);        }        // put the packet into the scheduler        super.sim_schedule(sched.getSchedID(),0,GridSimTags.SCHEDULER_ENQUE,pkt);    }    /**     * Gets the link's name for a given id     * @param destID    a destination id     * @return the link's man     * @pre destID > 0     * @post $none     */    private synchronized String getLinkName(int destID)    {        String destName = GridSim.getEntityName(destID);        String linkName = null;        //directly connected        if (hostTable_.containsValue(destName)) {            linkName = (String)linkTable_.get(destName);        }        else        {            // need to forward to another router            Object[] data = (Object[]) forwardTable_.get(destName);            String router = (String) data[0];            linkName = (String) linkTable_.get(router);        }        return linkName;    }    /**     * Returns the Scheduler associated with a packet.     *     * @param np NetPacket for which the associated scheduler is to be returned     * @return the packet's scheduler or <tt>null</tt> if the packet is empty     * @pre np != null     * @post $none     */    public PacketScheduler getScheduler(Packet np)    {        if (np == null) {            return null;        }        String destName = GridSim.getEntityName( np.getDestID() );        return getScheduler(destName);    }    /**     * Returns the Scheduler that the router would use to reach a particular     * destination. This can be used to set weigths, priorities etc. as the     * case may be on the Scheduler     *     * @param dest id of the destination for which the Scheduler is required.     * @return the destination's packet scheduler     * @pre dest > 0     * @post $none     */    public PacketScheduler getScheduler(int dest)    {        if (dest < 0) {            return null;        }        String destName = GridSim.getEntityName(dest);        return getScheduler(destName);    }    /**     * Returns the Scheduler that the router would use to reach a particular     * destination. This can be used to set weigths, priorities etc. as the     * case may be on the Scheduler     *     * @param dest Name of the destination for which the Scheduler is required.     * @return destination's packet scheduler or <tt>null</tt> if destination     *         name is invalid.     * @pre dest != null     * @post $none     */    public PacketScheduler getScheduler(String dest)    {        if (dest == null || dest.length() == 0) {            return null;        }        PacketScheduler sched = null;        try        {            if ( hostTable_.containsValue(dest) )            {                String linkName = (String) linkTable_.get(dest);                sched = (PacketScheduler) schedTable_.get(linkName);            }            else            {                // need to forward to another router                Object[] data = (Object[]) forwardTable_.get(dest);                // in case the forwarding table is incomplete                if (data == null) {                    return null;                }                String router = (String) data[0];                String linkName = (String) linkTable_.get(router);                sched = (PacketScheduler) schedTable_.get(linkName);            }        }        catch (Exception e)        {            sched = null;            System.out.println(super.get_name() + ".getScheduler(): exception");        }        return sched;    }    /**     * Dequeue a packet from the scheduler and sends it to the next     * destination via a link.     * @param sched  the packet scheduler     * @pre sched != null     * @post $none     */    private synchronized void dequeue(Sim_event ev)    {        Packet np = (Packet)ev.get_data();        // process ping() packet        if (np instanceof InfoPacket) {            ((InfoPacket) np).addExitTime( GridSim.clock() );        }        if (super.reportWriter_ != null) {            super.write("dequeuing, " + np);        }        // must distinguish between normal and junk packet        int tag = GridSimTags.PKT_FORWARD;        if (np.getTag() == GridSimTags.JUNK_PKT) {            tag = GridSimTags.JUNK_PKT;        }        // sends the packet via the link        String linkName = getLinkName( np.getDestID() );        super.sim_schedule(GridSim.getEntityId(linkName),                           GridSimTags.SCHEDULE_NOW, tag, np);    }    /**     * Prints this router's routing table in a nice-formatted layout     * @pre $none     * @post $none     */    public synchronized void printRoutingTable()    {        synchronized (System.out)        {            System.out.println();            System.out.println("--- Routing Table for " +                               super.get_name() + " ---");            for (Enumeration e = hostTable_.keys(); e.hasMoreElements(); )            {                String link = (String) e.nextElement();                System.out.println(hostTable_.get(link) + "\t\t" + link);            }            for (Enumeration e = forwardTable_.keys(); e.hasMoreElements(); )            {                String host = (String)e.nextElement();                Object[] data = (Object[])forwardTable_.get(host);                String nextHop = (String)data[0];                System.out.println(host + "\t\t" + nextHop);            }            System.out.println("-------------------------------------");            System.out.println();        }    }    //----------- ADVERTISING FUNCTIONS --------------//    /**     * All hosts connected to this router are advertised to adjacent routers     * @pre $none     * @post $none     */    protected synchronized void advertiseHosts()    {        Collection hosts = hostTable_.values(); // who to advertise        Enumeration routers = routerTable_.elements();        while ( routers.hasMoreElements() )        {            FloodAdPack ad = new FloodAdPack(super.get_name(), hosts);            String router = (String) routers.nextElement();            if (super.reportWriter_ != null) {                super.write("advertise to router, " + router);            }            sim_schedule(Sim_system.get_entity_id(router),                         GridSimTags.SCHEDULE_NOW, GridSimTags.ROUTER_AD, ad);        }        super.sim_pause(5);   // wait for 5 secs to gather the results    }    /**     * When an ad is recieved, the forwarding table is updated. After that we     * need to propogate this ad along all links except the incoming one.     * {@link #forwardAd(FloodAdPack)} is used for that     * @param ev  a Sim_event object     * @pre ev != null     * @post $none     */    private synchronized void receiveAd(Sim_event ev)    {        if (super.reportWriter_ != null) {            super.write("receive router ad from, " +                GridSim.getEntityName(ev.get_src()) );        }        // what to do when an ad is received        FloodAdPack ad = (FloodAdPack) ev.get_data();        // prevent count-to-infinity        if (ad.getHopCount() > 15) {            return;        }        String sender = ad.getSender();        Iterator it = ad.getHosts().iterator();        while ( it.hasNext() )        {            String host = (String) it.next();            if ( host.equals(super.get_name()) ) {                continue;            }            if (hostTable_.containsValue(host)) { // direct connection                continue;            }            if (forwardTable_.containsKey(host))            {                Object[] data = (Object[])forwardTable_.get(host);                int hop = ((Integer)data[1]).intValue();                if ((hop) > ad.getHopCount())                {                    Object[] toPut = { sender, new Integer(ad.getHopCount()) };                    forwardTable_.put(host, toPut);                }            }            else            {                Object[] toPut = { sender, new Integer(ad.getHopCount()) };                forwardTable_.put(host, toPut);            }        }        forwardAd(ad);    }    /**     * Received ads should be forwarded along all links except the incoming     * one. Also need to change id to onself     *     * @param ad    a FloodAdPack object     * @pre ad != null     * @post $none     */    private synchronized void forwardAd(FloodAdPack ad)    {        String sender = ad.getSender();        ad.incrementHopCount();        FloodAdPack newad = new FloodAdPack(super.get_name(), ad.getHosts());        newad.setHopCount(ad.getHopCount());        Enumeration routers = routerTable_.elements();        while ( routers.hasMoreElements() )        {            String router = (String)routers.nextElement();            if (!router.equals(sender))            {                sim_schedule(Sim_system.get_entity_id(router),                      GridSimTags.SCHEDULE_NOW, GridSimTags.ROUTER_AD, newad);            }        }    }    /**     * Informs the registered entities regarding to the end of a simulation.     * @pre $none     * @post $none     */    protected void processEndSimulation()    {        Enumeration scheds = schedTable_.elements();        while (scheds.hasMoreElements())        {            PacketScheduler sched = (PacketScheduler) scheds.nextElement();            sim_schedule(sched.getSchedID(), 0, GridSimTags.END_OF_SIMULATION);        }    }} // end class

⌨️ 快捷键说明

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