Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Modify Hub code to Switch

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Modify Hub code to Switch

    Hi Everyone,

    I am new to the Java world, but I am willing to learn, because it's an exciting programming language. I have a code for a Hub and I wanted to modify it to a Switch. Please help me. See the code below:

    --- Update ---

    package module.Hub;

    import framework.Port;
    import module.ModuleUI;
    import java.lang.String;
    import framework.Packet;

    public class HubMod extends module.ModuleAbstractClass {

    static final int MAXPORTS = 10;
    int noOfPorts = 0;
    double absoluteTime = 0;
    private Port ports[];
    private int serialNo = 0;
    private final String name = "Hub";
    private boolean newPacket = false;
    private ModuleUI modUI = null;

    public HubMod() {
    buffer.EditMode.module = this;
    ports = new Port[MAXPORTS];
    modUI = new HubUI(this);
    }

    public void setSno(int n) {
    serialNo = n;
    }

    /**
    * Adds a new port (connection)
    *
    * @return The error code if any. e.g. ERROR_ALREADY_MAX_ports is returned if no more ports of type wireType can be handeled by this module.
    */
    public int addPort(Port port, int wireType) {
    if (noOfPorts < MAXPORTS) {
    ports[noOfPorts++] = port;
    port.setActive(true);
    modUI.getModWin().msg.append("\nADDED port " + noOfPorts);
    return 1;
    } else
    return ERROR_ALREADY_MAX_PORTS;
    }

    /**
    * If the implementing class doesn't want to extend or create an object of ModuleUI them return null.
    *
    * @return
    */
    public ModuleUI getModuleUI() {
    return modUI;
    }

    public void setModuleUI(ModuleUI m) {
    modUI = m;
    }

    /**
    * Returns the name of the implementing module. This name is displayed by the UI.
    *
    * @return
    */
    public String getName() {
    return name;
    }

    /**
    * USES R-R SCHEDULING TO SERVICE THE PORTS
    */
    public boolean step(double TimeStep) {
    absoluteTime++;
    int noOfDataPorts = 0;
    for (int x = 0; x < noOfPorts; x++) {
    if (ports[x].hasData()) {
    noOfDataPorts++;
    }
    }
    if (noOfDataPorts == 1) {
    for (int i = 0; i < noOfPorts; i++) {
    if (ports[i].hasData()) {
    Packet packet = ports[i].getPacket(this);
    modUI.getModWin().msg.append("\nFrom:" + i + " To:");
    for (int j = 0; j < noOfPorts; j++) {
    if (j != i) {
    modUI.getModWin().msg.append(" " + j);
    Packet tempPacket = new Packet(packet);
    tempPacket.dataUI = null;
    ports[j].putPacket(tempPacket, this);
    ports[j].setActive(true);
    newPacket = true;
    }

    }
    return true;
    }
    }

    } else if (noOfDataPorts > 1) {
    modUI.getModWin().msg.append("\nMultiple Packets, ports: ");
    for (int i = 0; i < noOfPorts; i++) {
    if (ports[i].hasData()) {
    modUI.getModWin().msg.append(" " + i);
    Packet packet = ports[i].getPacket(this);
    for (int j = 0; j < noOfPorts; j++) {
    Packet tempPacket = new Packet(packet);
    tempPacket.dataUI = null;
    tempPacket.gotCorrupted();
    ports[j].putPacket(tempPacket, this);
    ports[j].setActive(true);
    newPacket = true;

    }
    return false;
    }
    }
    }
    // boolean Success or failure
    return true;
    }

    public boolean reset() { // passed when the simulation is reset
    for (int i = 0; i < noOfPorts; i++)
    if (ports[i].hasData())
    ports[i].getPacket(this);
    absoluteTime = 0;
    return true;
    }

    public Port[] getPorts() {// Gives the list of already initialized ports
    return ports;
    }

    public boolean isNewPacket() {// Tells whether new packet is released by the module. If released, it sends true and resets the flag,else it
    // returns false.
    if (newPacket == true) {
    newPacket = false;
    return true;
    } else
    return false;
    }

    public int getNoOfPorts() {
    return noOfPorts;
    }

    public int getSno() {

    return serialNo;
    }

    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,737
    My Mood
    Mellow
    Thanks
    233
    Thanked 708 Times in 694 Posts

    Default Re: Modify Hub code to Switch

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please learn to post your code correctly per the above link, and then edit your post so that your code is readable.

Similar Threads

  1. In need of help with switch-statement code.
    By RobbieKeller in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2013, 03:27 PM
  2. [SOLVED] So you want to know how to modify a file...
    By Cronus in forum Java Theory & Questions
    Replies: 4
    Last Post: June 26th, 2013, 09:17 AM
  3. Replies: 9
    Last Post: February 24th, 2013, 05:51 PM
  4. Having Trouble Modify the code!!
    By jehov86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 09:42 AM
  5. Replies: 1
    Last Post: April 17th, 2012, 06:21 AM