/** This method is called from within the constructor to initialize the form. */
  public void initComponents() {

    /** **************** The components ********************************* */
    firstPanel = new JPanel();
    firstPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 2));
    // If put to False: we see the container's background
    firstPanel.setOpaque(false);
    // rows, columns, horizontalGap, verticalGap
    firstPanel.setLayout(new GridLayout(4, 2, 3, 3));
    this.setLayout(new GridLayout(2, 1, 3, 3));
    this.add(firstPanel);

    proxyStackNameLabel = new JLabel("Proxy stack name:");
    proxyStackNameLabel.setToolTipText("The name of the stack to set");
    // Alignment of the text
    proxyStackNameLabel.setHorizontalAlignment(AbstractButton.CENTER);
    // Color of the text
    proxyStackNameLabel.setForeground(Color.black);
    // Size of the text
    proxyStackNameLabel.setFont(new Font("Dialog", 1, 12));
    // If put to true: we see the label's background
    proxyStackNameLabel.setOpaque(true);
    proxyStackNameLabel.setBackground(ProxyLauncher.labelBackGroundColor);
    proxyStackNameLabel.setBorder(ProxyLauncher.labelBorder);
    proxyStackNameTextField = new JTextField(20);
    proxyStackNameTextField.setHorizontalAlignment(AbstractButton.CENTER);
    proxyStackNameTextField.setFont(new Font("Dialog", 0, 14));
    proxyStackNameTextField.setBackground(ProxyLauncher.textBackGroundColor);
    proxyStackNameTextField.setForeground(Color.black);
    proxyStackNameTextField.setBorder(BorderFactory.createLoweredBevelBorder());
    firstPanel.add(proxyStackNameLabel);
    firstPanel.add(proxyStackNameTextField);

    proxyIPAddressLabel = new JLabel("Proxy IP address:");
    proxyIPAddressLabel.setToolTipText("The address of the proxy to set");
    // Alignment of the text
    proxyIPAddressLabel.setHorizontalAlignment(AbstractButton.CENTER);
    // Color of the text
    proxyIPAddressLabel.setForeground(Color.black);
    // Size of the text
    proxyIPAddressLabel.setFont(new Font("Dialog", 1, 12));
    // If put to true: we see the label's background
    proxyIPAddressLabel.setOpaque(true);
    proxyIPAddressLabel.setBackground(ProxyLauncher.labelBackGroundColor);
    proxyIPAddressLabel.setBorder(ProxyLauncher.labelBorder);
    proxyIPAddressTextField = new JTextField(20);
    proxyIPAddressTextField.setHorizontalAlignment(AbstractButton.CENTER);
    proxyIPAddressTextField.setFont(new Font("Dialog", 0, 14));
    proxyIPAddressTextField.setBackground(ProxyLauncher.textBackGroundColor);
    proxyIPAddressTextField.setForeground(Color.black);
    proxyIPAddressTextField.setBorder(BorderFactory.createLoweredBevelBorder());
    firstPanel.add(proxyIPAddressLabel);
    firstPanel.add(proxyIPAddressTextField);

    outboundProxyLabel = new JLabel("Next hop (IP:port/protocol):");
    outboundProxyLabel.setToolTipText(
        "Location where the message will be sent "
            + "if all the resolutions (DNS, router,...) fail. If not set: 404 will be replied");
    // Alignment of the text
    outboundProxyLabel.setHorizontalAlignment(AbstractButton.CENTER);
    // Color of the text
    outboundProxyLabel.setForeground(Color.black);
    // Size of the text
    outboundProxyLabel.setFont(new Font("Dialog", 1, 12));
    // If put to true: we see the label's background
    outboundProxyLabel.setOpaque(true);
    outboundProxyLabel.setBackground(ProxyLauncher.labelBackGroundColor);
    outboundProxyLabel.setBorder(ProxyLauncher.labelBorder);
    outboundProxyTextField = new JTextField(20);
    outboundProxyTextField.setHorizontalAlignment(AbstractButton.CENTER);
    outboundProxyTextField.setFont(new Font("Dialog", 0, 14));
    outboundProxyTextField.setBackground(ProxyLauncher.textBackGroundColor);
    outboundProxyTextField.setForeground(Color.black);
    outboundProxyTextField.setBorder(BorderFactory.createLoweredBevelBorder());
    firstPanel.add(outboundProxyLabel);
    firstPanel.add(outboundProxyTextField);

    routerClassLabel = new JLabel("The Router class name:");
    routerClassLabel.setToolTipText(
        "The class name (full java package name) of the router" + " used to forward the messages");
    // Alignment of the text
    routerClassLabel.setHorizontalAlignment(AbstractButton.CENTER);
    // Color of the text
    routerClassLabel.setForeground(Color.black);
    // Size of the text
    routerClassLabel.setFont(new Font("Dialog", 1, 12));
    // If put to true: we see the label's background
    routerClassLabel.setOpaque(true);
    routerClassLabel.setBackground(ProxyLauncher.labelBackGroundColor);
    routerClassLabel.setBorder(ProxyLauncher.labelBorder);
    routerClassTextField = new JTextField(20);
    routerClassTextField.setHorizontalAlignment(AbstractButton.CENTER);
    routerClassTextField.setFont(new Font("Dialog", 0, 12));
    routerClassTextField.setBackground(ProxyLauncher.textBackGroundColor);
    routerClassTextField.setForeground(Color.black);
    routerClassTextField.setBorder(BorderFactory.createLoweredBevelBorder());
    firstPanel.add(routerClassLabel);
    firstPanel.add(routerClassTextField);

    JPanel panel = new JPanel();
    // top, left, bottom, right
    panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 2));
    // If put to False: we see the container's background
    panel.setOpaque(false);
    // rows, columns, horizontalGap, verticalGap
    panel.setLayout(new BorderLayout());
    this.add(panel);

    JLabel lpLabel = new JLabel("Listening points list:");
    lpLabel.setVisible(true);
    lpLabel.setToolTipText("The listening points of the proxy");
    lpLabel.setHorizontalAlignment(AbstractButton.CENTER);
    lpLabel.setForeground(Color.black);
    lpLabel.setFont(new Font("Dialog", 1, 12));
    lpLabel.setOpaque(true);
    lpLabel.setBackground(ProxyLauncher.labelBackGroundColor);
    lpLabel.setBorder(ProxyLauncher.labelBorder);
    panel.add(lpLabel, BorderLayout.NORTH);

    // this.add(listeningPointsList);
    JScrollPane scrollPane =
        new JScrollPane(
            listeningPointsList,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    panel.add(scrollPane, BorderLayout.CENTER);

    thirdPanel = new JPanel();
    thirdPanel.setOpaque(false);
    // top, left, bottom, right
    thirdPanel.setBorder(BorderFactory.createEmptyBorder(3, 0, 5, 0));
    thirdPanel.setLayout(new GridLayout(1, 2, 3, 3));

    JButton addLPButton = new JButton(" Add ");
    addLPButton.setToolTipText("Add a listening point");
    addLPButton.setFocusPainted(false);
    addLPButton.setFont(new Font("Dialog", 1, 16));
    addLPButton.setBackground(ProxyLauncher.buttonBackGroundColor);
    addLPButton.setBorder(ProxyLauncher.buttonBorder);
    addLPButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            addLPButtonActionPerformed(evt);
          }
        });
    thirdPanel.add(addLPButton);

    JButton removeLPButton = new JButton(" Remove ");
    removeLPButton.setToolTipText("Remove a listening point");
    removeLPButton.setFocusPainted(false);
    removeLPButton.setFont(new Font("Dialog", 1, 16));
    removeLPButton.setBackground(ProxyLauncher.buttonBackGroundColor);
    removeLPButton.setBorder(ProxyLauncher.buttonBorder);
    removeLPButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            removeLPButtonActionPerformed(evt);
          }
        });
    thirdPanel.add(removeLPButton);

    panel.add(thirdPanel, BorderLayout.SOUTH);
  }
 public void unblockProperties() {
   outboundProxyAddressTextField.setEditable(true);
   outboundProxyAddressTextField.setBackground(Color.white);
   outboundProxyPortTextField.setEditable(true);
   outboundProxyPortTextField.setBackground(Color.white);
   registrarAddressTextField.setEditable(true);
   registrarAddressTextField.setBackground(Color.white);
   registrarPortTextField.setEditable(true);
   registrarPortTextField.setBackground(Color.white);
   imAddressTextField.setEditable(true);
   imAddressTextField.setBackground(Color.white);
   imPortTextField.setEditable(true);
   imPortTextField.setBackground(Color.white);
   imProtocolTextField.setEditable(true);
   imProtocolTextField.setBackground(Color.white);
   outputFileTextField.setEditable(true);
   outputFileTextField.setBackground(Color.white);
   buddiesFileTextField.setEditable(true);
   buddiesFileTextField.setBackground(Color.white);
   authenticationFileTextField.setEditable(true);
   authenticationFileTextField.setBackground(Color.white);
   defaultRouterTextField.setEditable(true);
   defaultRouterTextField.setBackground(Color.white);
 }
Example #3
0
  /** initialize GUI components */
  private JPanel initComponents() {
    JPanel panel = new JPanel();
    JScrollPane jScrollPane1 = new JScrollPane();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();

    jScrollPane1.setViewportView(jEditorPaneAbout);

    jButtonOk.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButtonOkActionPerformed(e);
          }
        });

    jButtonOk.setText("OK");
    jLabel1.setText("Application:");
    jLabel2.setText("Version:");

    jTextFieldApplication.setBackground(new Color(255, 255, 255));
    jTextFieldApplication.setEditable(false);
    jTextFieldApplication.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

    jTextFieldVersion.setBackground(new Color(255, 255, 255));
    jTextFieldVersion.setEditable(false);
    jTextFieldVersion.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));

    jEditorPaneAbout.setEditable(false);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(panel);
    panel.setLayout(layout);
    layout.setHorizontalGroup(
        layout
            .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(
                layout
                    .createSequentialGroup()
                    .add(
                        layout
                            .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(
                                layout
                                    .createSequentialGroup()
                                    .addContainerGap()
                                    .add(
                                        layout
                                            .createParallelGroup(
                                                org.jdesktop.layout.GroupLayout.LEADING)
                                            .add(
                                                jScrollPane1,
                                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                                289,
                                                Short.MAX_VALUE)
                                            .add(
                                                layout
                                                    .createSequentialGroup()
                                                    .add(
                                                        layout
                                                            .createParallelGroup(
                                                                org.jdesktop.layout.GroupLayout
                                                                    .LEADING)
                                                            .add(
                                                                jTextFieldApplication,
                                                                org.jdesktop.layout.GroupLayout
                                                                    .PREFERRED_SIZE,
                                                                145,
                                                                org.jdesktop.layout.GroupLayout
                                                                    .PREFERRED_SIZE)
                                                            .add(jLabel1))
                                                    .add(4, 4, 4)
                                                    .add(
                                                        layout
                                                            .createParallelGroup(
                                                                org.jdesktop.layout.GroupLayout
                                                                    .LEADING)
                                                            .add(jLabel2)
                                                            .add(
                                                                jTextFieldVersion,
                                                                org.jdesktop.layout.GroupLayout
                                                                    .DEFAULT_SIZE,
                                                                140,
                                                                Short.MAX_VALUE)))))
                            .add(
                                layout
                                    .createSequentialGroup()
                                    .add(106, 106, 106)
                                    .add(
                                        jButtonOk,
                                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                        101,
                                        Short.MAX_VALUE)
                                    .add(94, 94, 94)))
                    .addContainerGap()));
    layout.setVerticalGroup(
        layout
            .createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(
                layout
                    .createSequentialGroup()
                    .addContainerGap()
                    .add(
                        layout
                            .createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(jLabel2))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(
                        layout
                            .createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(
                                jTextFieldApplication,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(
                                jTextFieldVersion,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
                                org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                                org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(
                        jScrollPane1,
                        org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
                        300,
                        Short.MAX_VALUE)
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(jButtonOk)
                    .addContainerGap()));
    return panel;
  }
  // FUNCTION TO SIMULATE A STEP IN THE SIMULATION ON THE FRAME
  public void step() {

    /**
     * EACH TIME THE NEXT BUTTON IS PRESSED, ALL THE STATES THAT OCURRED UPTO THE CURRENT STATE ARE
     * EVALUATED. THERE IS A while STATEMENT THAT PERFORMS THIS FUNCTION AND CONTAINS A switch
     * STATEMENT WITHIN IT TO EVALUATE EACH STEP AS IT OCCURS.
     */

    ////////////////////// INITIALIZATION ///////////////////////////////////////

    // UPDATE THE STATE OF THE CACHE AND MEMORY
    for (int i = 0; i < 16; i++) {
      cachePanel.stringBlocks[i] = "";
      cachePanel.boolBlocks[i] = false;
      cachePanel.tag[i] = "";
      cachePanel.boolTags[i] = false;

      statusCacheEmpty[i] = true;
      statusCacheLRU[i] = 0;
    }
    for (int i = 0; i < 8; i++) {
      cachePanel.boolWords[i] = false;
      memoryPanel.boolWords[i] = false;
    }
    for (int i = 0; i < 32; i++) memoryPanel.boolBlocks[i] = false;

    // UPDATE THE BITS IN MAIN MEMORY ADDRESS
    tTag.setText("");
    tWord.setText("");
    tTag.setBackground(new Color(205, 205, 205));
    tWord.setBackground(new Color(205, 205, 205));

    // UPDATE THE CACHE HITS AND MISSES FIELDS
    cacheHits = 0;
    cacheMisses = 0;
    tCacheHits.setText("  0");
    tCacheMisses.setText("  0");

    // UPDATE THE VALUES USED FOR BRINGING MEMORY BLOCKS IN CACHE
    statusLRU = 0;
    memInCache = -1;
    lruCacheBlock = -1;

    // RESET THE VALUE OF addSel
    evaluateIndex = 0;

    // DISABLE ADDRESS GENERATION BUTTONS
    autoGen.setEnabled(false);
    selfGen.setEnabled(false);

    ////////////////////// END INITIALIZATION /////////////////////////////////////

    // IF Next WAS CLICKED, INCREMENT moveStatus
    if (nextClicked) moveStatus++;
    else {
      // DECREMENT moveStatus AND ENABLE NEXT SINCE IT MIGHT BE DISABLED
      moveStatus--;
      next.setEnabled(true);
    }

    // IF NO MORE back MOVES CAN BE MADE, DISABLE back BUTTON
    if (moveStatus == 0) {
      back.setEnabled(false);
      tProgress.setText(
          "You cannot go back any further."
              + "\nPlease click on \"Next\" or \"Restart\" to continue.");
      tProgress.setCaretPosition(0);

      // CLEAR THE SELECTED ADDRESS REFERENCE STRING
      addRefStrList.clearSelection();
    } else
      // ENABLE back BUTTON ONCE THE FIRST MOVE IS MADE
      back.setEnabled(true);

    // INITIALIZE THE VARIABLE THAT KEEPS TRACK OF THE STATE WE ARE CURRENTLY EVALUATING.
    int tempState = 1;

    // CONTINUE TO EVALUATE EACH STATE TILL WE REACH THE CURRENT STATE
    while (tempState <= moveStatus) {

      switch (tempState % 6) {
        case 1: // IF A NEW CYCLE IS BEGINNING, OBTAIN NEXT ADDRESS REFERENCE

          // OBTAIN THE ADDRESS REFERENCE STRING
          addRefStrList.setSelectedIndex(evaluateIndex);

          // ENSURE THAT THE LIST SCROLLS AND SELECTED INDEX IS VISIBLE
          // DUE TO REPAINTING CONSTRAINTS, ONLY DO THIS IN THE CURRENT STATE
          if (tempState == moveStatus) addRefStrList.ensureIndexIsVisible(evaluateIndex);

          // EVALUATE THE TAG, BLOCK AND WORD
          hexAddress = (String) addRefStrList.getSelectedValue();
          int intAddress = Integer.parseInt(hexAddress, 16);
          binAddress = Integer.toBinaryString(intAddress);

          // USING CLASS INTEGER'S parseInt FUNCTION RETURNS A BINARY STRING WITHOUT LEADING 0'S
          // ENSURE THAT binAddress is 8 bits
          if (binAddress.length() < 8) {
            int zeroes = 8 - binAddress.length();
            for (int i = 0; i < zeroes; i++) binAddress = '0' + binAddress;
          }

          tag = binAddress.substring(0, 5);
          word = binAddress.substring(5);

          // CALCULATE THE ACTUAL CACHE AND MEMORY BLOCKS AND WORDS IN QUESTION
          intWordDec = Integer.parseInt(word, 2);
          intBlockDecMem = Integer.parseInt(tag, 2);

          // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
          if (tempState == moveStatus) {
            tProgress.setText(
                "The memory address we want is obtained from the Address Reference String."
                    + "\nIt is (in hexadecimal): "
                    + hexAddress
                    + ".");
            tProgress.setCaretPosition(0);
          }

          break;

        case 2: // EVALUATE THE BITS IN MAIN MEMORY ADDRESS AND HIGHLIGHT THEM

          // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
          if (tempState == moveStatus) {
            tProgress.setText(
                "The hexadecimal address "
                    + hexAddress
                    + " evaluates to its binary equivalent "
                    + binAddress
                    + "."
                    + "\nHence the bits in the Main Memory Address are divided into the following fields\n"
                    + tag
                    + " --> Tag,  "
                    + word
                    + " --> Word."
                    + "\nThe tag bits identify the memory block, "
                    + "and the word bits identify the word within the block.");
            tProgress.setCaretPosition(0);

            // HIGHLIGHT THE BITS IN MAIN MEMORY ADDRESS IN GREEN
            tTag.setBackground(Color.green);
            tWord.setBackground(Color.green);
          }

          tTag.setText("      " + tag);
          tWord.setText("  " + word);

          break;

        case 3: // FIND THE CACHE BLOCK IN QUESTION AND HIGHLIGHT IT

          // UNDO HIGHLIGHTS OF PREVIOUS STEP
          tTag.setBackground(new Color(205, 205, 205));
          tWord.setBackground(new Color(205, 205, 205));

          // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
          if (tempState == moveStatus) {
            tProgress.setText(
                "Every time a memory block is placed in cache, its tag field is stored with it as well."
                    + "\nSo, to find the required memory block in cache, its tag, "
                    + tag
                    + " is compared to all the valid tag fields in cache.");
            tProgress.setCaretPosition(0);
          }

          // GET THE BLOCK IN CACHE WHERE MEMORY BLOCK EXISTS, IF AT ALL
          memInCache = getCacheBlock(tag);

          // IF MEMORY BLOCK IS NOT IN CACHE...
          if (memInCache == -1) {
            if (tempState == moveStatus) {
              tProgress.append(
                  "\n\nSince the memory block is not in cache, there is a cache miss."
                      + "\nSo the block needs to be brought in from memory.");
              tProgress.setCaretPosition(0);
            }

            // GET FIRST EMPTY CACHE BLOCK, IF AVAILABLE
            emptyCacheBlock = getFirstEmptyCacheBlock();

            // IF EMPTY CACHE BLOCK IS AVAILABLE, THIS IS WHERE THE MEMORY WILL BE BROUGHT SO
            // DISPLAY IT
            if (!(emptyCacheBlock == -1)) {

              // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
              if (tempState == moveStatus) {
                tProgress.append(
                    "\nSince the cache has empty space, the first available block will be filled."
                        + "\nSee the highlighted cache block.");
                tProgress.setCaretPosition(0);
              }

              // HIGHLIGHT THE CACHE BLOCK IN YELLOW
              cachePanel.boolBlocks[emptyCacheBlock] = true;
              cachePanel.boolTags[emptyCacheBlock] = true;

              // STORE THE CHANGED CACHE BLOCK INDEX IN COMMON VARIABLE
              intBlockDec = emptyCacheBlock;
            }

            // ELSE DISPLAY THE LRU CACHE BLOCK WHICH WILL BE REPLACED
            else {

              // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
              if (tempState == moveStatus) {
                tProgress.append(
                    "\nSince the cache is full, the least recently used cache block will be replaced."
                        + "\nSee the highlighted cache block.");
                tProgress.setCaretPosition(0);
              }

              lruCacheBlock = getLRUCacheBlock();

              // HIGHLIGHT THE CACHE BLOCK IN YELLOW
              cachePanel.boolBlocks[lruCacheBlock] = true;
              cachePanel.boolTags[lruCacheBlock] = true;

              // STORE THE CHANGED CACHE BLOCK INDEX IN COMMON VARIABLE
              intBlockDec = lruCacheBlock;
            }

            // UPDATE COUNT OF CACHE MISSES
            cacheMisses++;
            tCacheMisses.setText("  " + cacheMisses);
          } else {

            if (tempState == moveStatus) {
              tProgress.append(
                  "\n\nSince the required memory block is in cache block "
                      + memInCache
                      + " there is a cache hit.");
              tProgress.setCaretPosition(0);
            }

            // HIGHLIGHT THE CACHE BLOCK IN YELLOW
            // TO CAUSE HIGHLIGHTING ON THE CACHE, WE NEED TO MODIFY IT'S STATE, i.e. IT'S DATA
            // MEMBERS
            cachePanel.boolBlocks[memInCache] = true;
            cachePanel.boolWords[intWordDec] = true;
            cachePanel.boolTags[memInCache] = true;

            // STORE THE CHANGED CACHE BLOCK INDEX IN COMMON VARIABLE
            intBlockDec = memInCache;

            // UPDATE COUNT OF CACHE HITS
            cacheHits++;
            tCacheHits.setText("  " + cacheHits);
          }

          break;

        case 4: // EVALUATE THE MEMORY BLOCK IN QUESTION AND HIGHLIGHT IT

          // UNDO THE HIGHLIGHTS OF THE PREVIOUS STEP
          cachePanel.boolBlocks[intBlockDec] = false;
          cachePanel.boolWords[intWordDec] = false;
          cachePanel.boolTags[intBlockDec] = false;

          // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
          if (tempState == moveStatus) {
            tProgress.setText(
                "Highlighted is the memory block in question. Since the tag bits are "
                    + tag
                    + ", the memory block, in decimal, is "
                    + intBlockDecMem
                    + ".");
            tProgress.setCaretPosition(0);
          }

          // SET THE MEMORY STATE SO AS TO HIGHLIGHT THE REQUIRED MEMORY BLOCK
          memoryPanel.boolBlocks[intBlockDecMem] = true;

          // SET THE MEMORY STATE SO AS TO HIGHLIGHT THE REQUIRED WORD
          memoryPanel.boolWords[intWordDec] = true;

          break;

        case 5: // HIGHLIGHT THE CACHE BLOCK WITH THE MEMORY BLOCK NOW IN IT

          // UNDO HIGHLIGHTS OF PREVIOUS STEP
          memoryPanel.boolBlocks[intBlockDecMem] = false;
          memoryPanel.boolWords[intWordDec] = false;

          /*
           * NOW, THERE ARE 3 WAYS TO GO FROM HERE
           * 1. IF THERE IS AN EMPTY CACHE BLOCK, SIMPLY BRING THE MEMORY BLOCK INTO CACHE
           * 2. IF THE REQUIRED MEMORY BLOCK IS ALREADY IN CACHE, DO NOTHING
           * 3. IF THE CACHE IS FULL, FIND THE LRU BLOCK AND REPLACE IT WITH THE REQUIRED MEMORY BLOCK
           */

          // IF THE MEMORY BLOCK WAS NOT IN CACHE AND AN EMPTY CACHE BLOCK IS AVAILABLE
          // BRING THE MEMORY BLOCK AND TAG INTO CACHE AND HIGHLIGHT CACHE BLOCK
          if ((memInCache == -1) && !(emptyCacheBlock == -1)) {

            // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
            if (tempState == moveStatus) {
              tProgress.setText(
                  "As we saw earlier, the required memory block was not in cache."
                      + "\nSince there was empty space in cache, we brought the memory block into it."
                      + "\nWe also stored the tag, "
                      + tag
                      + ", of the memory block with the cache block."
                      + "\n\nRemember that the memory block could be brought into any empty cache block."
                      + "\nIn our example, we are using the first available empty block.");
              tProgress.setCaretPosition(0);
            }

            // UPDATE THE COUNTER FOR THE LRU CACHE BLOCK
            statusLRU++;
            statusCacheLRU[emptyCacheBlock] = statusLRU;
            statusCacheEmpty[emptyCacheBlock] = false;

            // UPDATE THE CACHE ARRAYS KEEPING TRACK OF MEMORY BLOCKS AND TAGS
            cachePanel.stringBlocks[emptyCacheBlock] = "" + intBlockDecMem;
            cachePanel.tag[emptyCacheBlock] = tag;

            // HIGHLIGHT THE CACHE BLOCK IN YELLOW
            cachePanel.boolBlocks[emptyCacheBlock] = true;
            cachePanel.boolWords[intWordDec] = true;
            cachePanel.boolTags[emptyCacheBlock] = true;

            // STORE THE CHANGED CACHE BLOCK INDEX IN COMMON VARIABLE
            intBlockDec = emptyCacheBlock;

          } // END IF

          // IF MEMORY BLOCK IS ALREADY IN CACHE THEN JUST HIGHLIGHT THE CACHE BLOCK
          else if ((memInCache >= 0) && (memInCache < 16)) {

            // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
            if (tempState == moveStatus) {
              tProgress.setText(
                  "As we saw earlier, the required memory block is already in cache.");
              tProgress.setCaretPosition(0);
            }

            // UPDATE THE COUNTER FOR THE LRU CACHE BLOCK
            statusLRU++;
            statusCacheLRU[memInCache] = statusLRU;

            // HIGHLIGHT THE CACHE BLOCK IN YELLOW
            cachePanel.boolBlocks[memInCache] = true;
            cachePanel.boolWords[intWordDec] = true;
            cachePanel.boolTags[memInCache] = true;

            // STORE THE CHANGED CACHE BLOCK INDEX IN COMMON VARIABLE
            intBlockDec = memInCache;

          } // END ELSE IF

          // IF THE MEMORY BLOCK IS NOT IN CACHE AND THE CACHE IS FULL
          // FIND THE LRU CACHE BLOCK AND REPLACE IT WITH THE MEMORY BLOCK, THEN HIGHLIGHT THE CACHE
          // BLOCK
          else {

            // FIND THE LRU CACHE BLOCK
            lruCacheBlock = getLRUCacheBlock();

            // IF THIS IS THE CURRENT STATE, UPDATE THE PROGRESS FIELD
            if (tempState == moveStatus) {
              tProgress.setText(
                  "As we saw earlier, the cache is full."
                      + "\nSo we picked the least recently used cache block, "
                      + lruCacheBlock
                      + ", and replaced it with the required memory block.");
              tProgress.setCaretPosition(0);
            }

            // UPDATE THE COUNTER FOR THE LRU CACHE BLOCK
            statusLRU++;
            statusCacheLRU[lruCacheBlock] = statusLRU;
            statusCacheEmpty[lruCacheBlock] = false; // redundant stmt

            // UPDATE THE CACHE ARRAYS KEEPING TRACK OF MEMORY BLOCKS AND TAGS
            cachePanel.stringBlocks[lruCacheBlock] = "" + intBlockDecMem;
            cachePanel.tag[lruCacheBlock] = tag;

            // HIGHLIGHT THE CACHE BLOCK IN YELLOW
            cachePanel.boolBlocks[lruCacheBlock] = true;
            cachePanel.boolWords[intWordDec] = true;
            cachePanel.boolTags[lruCacheBlock] = true;

            // STORE THE CHANGED CACHE BLOCK INDEX IN COMMON VARIABLE
            intBlockDec = lruCacheBlock;
          } // END ELSE

          break;

        case 0: // LAST STEP IN CYCLE - CLEANUP STEP!

          // UNDO HIGHLIGHTS OF PREVIOUS STEP
          cachePanel.boolBlocks[intBlockDec] = false;
          cachePanel.boolWords[intWordDec] = false;
          cachePanel.boolTags[intBlockDec] = false;

          tTag.setText("");
          tWord.setText("");

          tTag.setBackground(new Color(205, 205, 205));
          tWord.setBackground(new Color(205, 205, 205));

          // CLEAR THE SELECTED ADDRESS REFERENCE STRING
          addRefStrList.clearSelection();

          // INCREMENT THE INDEX SO AS TO POINT TO THE NEXT ADDRESS REFERENCE STRING
          evaluateIndex++;

          // IF THE LAST ADDRESS REFERENCE STRING HAS BEEN REACHED, DO THE APPROPRIATE
          if (evaluateIndex == listData.size()) {

            if (tempState == moveStatus) {
              tProgress.setText(
                  "This completes the runthrough."
                      + "\nPlease click on \"Restart\", generate the Address Reference String "
                      + "OR click \"Quit\" to finish.");
              tProgress.setCaretPosition(0);
            }
            next.setEnabled(false);

            // ENABLE ADDRESS GENERATION BUTTONS
            autoGen.setEnabled(true);
            selfGen.setEnabled(true);

            reStarted = false;
          }

          // ELSE AN ACCESS CYCLE HAS BEEN COMPLETED SO SHOW THE APPROPRIATE MESSAGE IN THE PROGRESS
          // FIELD
          else {
            if (tempState == moveStatus) {
              tProgress.setText("This completes an access cycle.");
              tProgress.setCaretPosition(0);
            }

            // CLEAR THE SELECTION IN THE ADDRESS REFERENCE STRING
            addRefStrList.clearSelection();
          }

          break;

        default:
          JOptionPane.showMessageDialog(null, "Uh Oh, there's a problem in switch-case!");
      } // END switch

      tempState++;
    } // END while

    // CALL THE REPAINT METHOD
    repaint();
  } // END FUNCTION step
  /*
   * FUNCTION TO RESTART THE APPLICATION
   * BRINGS THE FRAME TO ITS ORIGINAL STARTING CONFIGURATION
   */
  public void reStart() {

    // UNDO THE HIGHLIGHTS, IF ANY, OF THE PREVIOUS STEPS

    // UPDATE THE STATE OF THE CACHE AND MEMORY
    for (int i = 0; i < 16; i++) {
      cachePanel.stringBlocks[i] = "";
      cachePanel.boolBlocks[i] = false;
      cachePanel.tag[i] = "";
      cachePanel.boolTags[i] = false;

      statusCacheEmpty[i] = true;
      statusCacheLRU[i] = 0;
    }
    for (int i = 0; i < 8; i++) {
      cachePanel.boolWords[i] = false;
      memoryPanel.boolWords[i] = false;
    }
    for (int i = 0; i < 32; i++) memoryPanel.boolBlocks[i] = false;

    // UPDATE THE CACHE HITS AND MISSES FIELDS
    cacheHits = 0;
    cacheMisses = 0;
    tCacheHits.setText("  0");
    tCacheMisses.setText("  0");

    // UPDATE THE VALUES USED FOR BRINGING MEMORY BLOCKS IN CACHE
    statusLRU = 0;
    memInCache = -1;
    lruCacheBlock = -1;

    // REFRESH THE ADDRESS REFERENCE STRING TO NULL
    listData.removeAllElements();
    addRefStrList.setListData(listData);

    // UPDATE THE BITS IN MAIN MEMORY ADDRESS
    tTag.setText("");
    tWord.setText("");
    tTag.setBackground(new Color(205, 205, 205));
    tWord.setBackground(new Color(205, 205, 205));

    // UPDATE THE PROGRESS FIELD
    tProgress.setText(
        "Let's start over. \nPlease generate the Address Reference String."
            + "\nThen click on \"Next\" to continue.");

    // DISABLE THE NEXT AND BACK BUTTONS
    next.setEnabled(false);
    back.setEnabled(false);

    // ENABLE ADDRESS GENERATION BUTTONS
    autoGen.setEnabled(true);
    selfGen.setEnabled(true);

    // RESET THE VALUES OF moveStatus AND addSel AND reStarted
    moveStatus = 0;
    evaluateIndex = 0;
    reStarted = true;

    // CALL THE REPAINT METHOD
    repaint();
  } // END FUNCTION reStart
Example #6
0
  /** Initializes the graphical components */
  public void init() {
    username = getParameter("username");
    if (username == null) {
      username =
          JOptionPane.showInputDialog(
              this, "Please enter a username", "Login", JOptionPane.QUESTION_MESSAGE);
    }
    try {
      PORT = Integer.valueOf(getParameter("port")).intValue();
    } catch (NumberFormatException e) {
      PORT = 42412;
    }

    URL url = getDocumentBase();
    site = url.getHost();
    locationURL = "http://" + site + ":" + url.getPort() + "/" + url.getPath();

    setSize(615, 362);
    c = getContentPane();

    c.setBackground(new Color(224, 224, 224));

    if (site == null || locationURL == null) {
      c.add(new JLabel("ERROR: did not recieve needed data from page"));
    }

    myAction = new MyAction();
    myKeyListener = new MyKeyListener();
    myMouseListener = new MyMouseListener();
    myHyperlinkListener = new MyHyperlinkListener();

    c.setLayout(null);

    cboChannels = new JComboBox();
    cboChannels.setBounds(5, 5, 150, 24);

    butChannel = new JButton("Join");
    butChannel.setToolTipText("Join channel");
    butChannel.addActionListener(myAction);
    butChannel.setBounds(160, 5, 60, 24);

    butCreate = new JButton("Create");
    butCreate.setToolTipText("Create new channel");
    butCreate.addActionListener(myAction);
    butCreate.setBounds(230, 5, 100, 24);
    butCreate.setEnabled(false);

    butInvite = new JButton("Invite");
    butInvite.setToolTipText("Invite Friend");
    butInvite.addActionListener(myAction);
    butInvite.setBounds(340, 5, 80, 24);

    mainChat = new ChatPane(this);
    textScroller =
        new JScrollPane(
            mainChat,
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    textScroller.setBounds(5, 34, 500, 270);

    userList = new JList();
    userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    userList.setCellRenderer(new MyCellRenderer());
    userList.setBackground(new Color(249, 249, 250));
    JScrollPane userScroller = new JScrollPane(userList);
    userScroller.setBounds(510, 34, 100, 297);

    messageText = new JTextField();
    messageText.setBounds(5, 309, 500, 22);
    messageText.setColumns(10);
    messageText.setBackground(new Color(249, 249, 250));

    JMenuItem item;
    popup = new JPopupMenu("test");
    popup.add("whisper").addActionListener(myAction);
    popup.add("private message").addActionListener(myAction);
    popup.add("ignore").addActionListener(myAction);
    popup.add("clear ignore list").addActionListener(myAction);

    conNo = new ImageIcon(getURL("images/connect_no.gif"));
    conYes = new ImageIcon(getURL("images/connect_established.gif"));
    secNo = new ImageIcon(getURL("images/decrypted.gif"));
    secYes = new ImageIcon(getURL("images/encrypted.gif"));

    conIcon = new JLabel(conNo);
    conIcon.setBorder(new EtchedBorder());
    secIcon = new JLabel(secNo);
    secIcon.setBorder(new EtchedBorder());

    conIcon.setBounds(563, 334, 22, 22);
    secIcon.setBounds(588, 334, 22, 22);

    bottomText =
        new JLabel(
            "<html><body><font color=#445577><b>"
                + "LlamaChat "
                + VERSION
                + "</b></font> &nbsp;&copy; "
                + "<a href=\""
                + linkURL
                + "\">Joseph Monti</a> 2002-2003"
                + "</body></html>");
    bottomText.setBounds(5, 336, 500, 20);

    c.add(cboChannels);
    c.add(butChannel);
    c.add(butCreate);
    c.add(butInvite);
    c.add(textScroller);
    c.add(userScroller);
    c.add(messageText);
    c.add(conIcon);
    c.add(secIcon);
    c.add(bottomText);

    userList.addMouseListener(myMouseListener);
    messageText.addKeyListener(myKeyListener);
    bottomText.addMouseListener(myMouseListener);

    users = new ArrayList();
    ignores = new ArrayList(5);
    afks = new ArrayList(5);
    admins = new ArrayList(5);
    history = new CommandHistory(10);
    admin = false;
    channels = new Hashtable();
    privates = new PrivateMsg(this);
    showUserStatus = false;

    myColors[0] = new Color(200, 0, 0);
    myColors[1] = new Color(0, 150, 0);
    myColors[2] = new Color(0, 0, 200);

    rect = new Rectangle(0, 0, 1, 1);

    String opening =
        "<font color=#333333>"
            + "==================================<br>"
            + "Welcome to LlamaChat "
            + VERSION
            + "<br>"
            + "If you need assistance, type \\help<br>"
            + "Enjoy your stay!<br>"
            + "Maestria Aplicada en Redes<br>"
            + "==================================<br></font>";
    HTMLDocument doc = (HTMLDocument) mainChat.getDocument();
    HTMLEditorKit kit = (HTMLEditorKit) mainChat.getEditorKit();
    try {
      kit.insertHTML(doc, doc.getLength(), opening, 0, 0, HTML.Tag.FONT);
    } catch (Throwable t) {
      t.printStackTrace(System.out);
    }

    // validate the name
    if (!username.matches("[\\w_-]+?")) {
      error(
          "username contains invalid characters, changing to "
              + "'invalid' for now. "
              + "Type \\rename to chose a new name");
      username = "******";
    }
    if (username.length() > 10) {
      username = username.substring(0, 10);
      error("username too long, changed to " + username);
    }

    connect();
  }