Пример #1
0
 /**
  * Print out a message to the log.
  *
  * @param source String: the source bean
  * @param method String: the source method
  * @param message String: the message
  */
 protected static void println(String source, String method, String message) {
   if (Utility.init()) {
     logWriter.println(source, method, "", message);
   }
 }
Пример #2
0
 /**
  * Print out an exception to the log.
  *
  * @param source String: the source bean
  * @param method String: the source method
  * @param ex Exception: the exception
  */
 protected static void printException(String source, String method, Exception ex) {
   if (Utility.init()) {
     logWriter.printException(source, method, "", ex);
   }
 }
Пример #3
0
  @Override
  public void run() {
    try {
      socketInput =
          new BufferedReader(
              new InputStreamReader(
                  connectionSocket.getInputStream())); // get the inputstream from other peer
      pieceReader =
          connectionSocket
              .getInputStream(); // return an input stream for reading bytes from this socket.
      socketOutput =
          new DataOutputStream(
              connectionSocket.getOutputStream()); // get the outputstream to others
      // return an output stream for writing bytes to this socket.
    } catch (IOException ex) {
      Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
    }

    // TCP handshake
    if (OtherPeerID != -1) {
      try {
        socketOutput.writeBytes(handshakeMsg);
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }

      try {
        while (socketInput.ready() == false) {}

      } catch (Exception e) {
      }
      try {
        checkHandShakeMsg = socketInput.readLine() + "";
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }
      checkHandShakeHeader = checkHandShakeMsg.substring(0, 18);
      OtherPeerID = Integer.parseInt(checkHandShakeMsg.substring(28, 32));

    } else // ackhandshake
    {

      try {
        while (socketInput.ready() == false) {}

      } catch (Exception e) {
      }

      try {
        checkHandShakeMsg = socketInput.readLine() + "";
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }
      checkHandShakeHeader = checkHandShakeMsg.substring(0, 18);
      OtherPeerID = Integer.parseInt(checkHandShakeMsg.substring(28, 32));

      if (checkHandShakeHeader.equals(handshakeheader)) {

        try { // if it is the right neighbour, write log that peer1 is connected from peer2
          peerProcess.logfile.write(LogWriter.logmsg(1, peerProcess.myPeerID, OtherPeerID));
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {

          socketOutput.writeBytes(handshakeMsg);
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }

    // start transferring
    try {
      while (quited == false
          && (peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).obtainAll() == false
              || peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).obtainAll() == false)) {

        try {
          socketOutput.writeBytes(peerNotExitMsg);
          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " sent not quit message to peer "
                  + OtherPeerID
                  + ".");
        } catch (Exception e) {
        }
        try {
          while (socketInput.ready() == false) {
            //
          }
        } catch (Exception e) {
        }
        try {
          MsgFromOtherPeer = socketInput.readLine() + "";
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        // get the corresponding message length, message type and message payload
        RecvdMsg = MsgFromOtherPeer.split("#");
        msgLen = Integer.parseInt(RecvdMsg[0]);
        msgType = Integer.parseInt(RecvdMsg[1]);
        msgPayload = RecvdMsg[2];
        if (msgType == 8) {

          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received quit message from peer "
                  + OtherPeerID
                  + ".");
          quited = true;
          break;
        } else if (msgType != 9) {
          System.out.println(
              "Received message type " + msgType + ", expecting quit/not quit message.");
        } else {
          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received not quit message from peer "
                  + OtherPeerID
                  + ".");
        }

        // bitField exchange
        try {
          mybitfield =
              peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).obtainBitField();
          haveMsg = mybitfield.length() + "#" + 4 + "#" + mybitfield + "\n";
          socketOutput.writeBytes(haveMsg);
          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " sent bitfield message to peer "
                  + OtherPeerID
                  + ".");
        } catch (Exception e) {
        }
        try {
          while (socketInput.ready() == false) {}

        } catch (Exception e) {
        }
        try {
          MsgFromOtherPeer = socketInput.readLine() + "";
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        RecvdMsg = MsgFromOtherPeer.split("#");
        msgLen = Integer.parseInt(RecvdMsg[0]);
        msgType = Integer.parseInt(RecvdMsg[1]);
        msgPayload = RecvdMsg[2];
        if (msgType == 4) {

          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received bitField message from peer "
                  + OtherPeerID
                  + ".");
          peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setTheBitField(msgPayload);
        } else if (msgType == 8) {

          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received quit message from peer "
                  + OtherPeerID
                  + ".");
          quited = true;
          break;
        } else {
          System.out.println(" received message type " + msgType + ", expecting bitField message.");
        }

        if (peerProcess.choked[OtherPeerID % 1000 - 1] == false) {
          try {

            socketOutput.writeBytes(unchokedMsg);
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }
          mybitfield =
              peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).obtainBitField();
          haveMsg = mybitfield.length() + "#" + 4 + "#" + mybitfield + "\n";
          try {

            socketOutput.writeBytes(haveMsg);
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }

          try {
            while (socketInput.ready() == false) {}

          } catch (Exception e) {
          }
          try {
            MsgFromOtherPeer = socketInput.readLine();
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }
          RecvdMsg = MsgFromOtherPeer.split("#");
          msgLen = Integer.parseInt(RecvdMsg[0]);
          msgType = Integer.parseInt(RecvdMsg[1]);
          msgPayload = RecvdMsg[2];
          if (msgType == 1) {
            try {
              peerProcess.logfile.write(LogWriter.logmsg(3, peerProcess.myPeerID, OtherPeerID));
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
              while (socketInput.ready() == false) {}
            } catch (Exception e) {
            }
            try {
              MsgFromOtherPeer = socketInput.readLine();
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            RecvdMsg = MsgFromOtherPeer.split("#");
            msgLen = Integer.parseInt(RecvdMsg[0]);
            msgType = Integer.parseInt(RecvdMsg[1]);
            msgPayload = RecvdMsg[2];
            if (msgType == 4) {
              peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setTheBitField(msgPayload);
              interestedPieceID =
                  peerProcess
                      .Peers
                      .elementAt(peerProcess.myPeerID % 1000 - 1)
                      .obtainInterestedPiece(OtherPeerID);
              try {
                if (interestedPieceID != -1)
                  peerProcess.logfile.write(
                      LogWriter.logmsg(5, peerProcess.myPeerID, OtherPeerID, interestedPieceID));
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
              if (interestedPieceID == -1) {
                try {
                  socketOutput.writeBytes(1 + "#" + 3 + "#" + "-1\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                  while (socketInput.ready() == false) {}
                } catch (Exception e) {
                }
                try {
                  MsgFromOtherPeer = socketInput.readLine() + "";
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                if (msgType == 2) {
                  try {
                    peerProcess.logfile.write(
                        LogWriter.logmsg(6, peerProcess.myPeerID, OtherPeerID));
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  byte[] piececontent = peerProcess.obtainPiece(Integer.parseInt(msgPayload));
                  piecemsg = msgPayload + "#" + 7 + "#" + piececontent.length + "\n";
                  try {
                    socketOutput.writeBytes(piecemsg);

                    while (socketInput.ready() == false) {}

                    try {
                      MsgFromOtherPeer = socketInput.readLine();
                    } catch (Exception e) {
                    }
                    RecvdMsg = MsgFromOtherPeer.split("#");
                    msgLen = Integer.parseInt(RecvdMsg[0]);
                    msgType = Integer.parseInt(RecvdMsg[1]);
                    msgPayload = RecvdMsg[2];
                    if (msgType == 10) {

                      socketOutput.write(piececontent);

                      while (socketInput.ready() == false) {}

                      try {
                        MsgFromOtherPeer = socketInput.readLine();
                      } catch (Exception e) {
                      }
                      RecvdMsg = MsgFromOtherPeer.split("#");
                      msgLen = Integer.parseInt(RecvdMsg[0]);
                      msgType = Integer.parseInt(RecvdMsg[1]);
                      msgPayload = RecvdMsg[2];
                      if (msgType != 10) {
                        System.out.println(
                            "received message type "
                                + msgType
                                + ", expecting channel clear message.");
                      }
                      continue;
                    } else {
                      System.out.println(
                          "received message type "
                              + msgType
                              + ", expecting channel clear message.");
                    }
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                } else if (msgType == 3) {
                  try {

                    peerProcess.logfile.write(
                        LogWriter.logmsg(7, peerProcess.myPeerID, OtherPeerID));
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                }
              } else {
                try {
                  socketOutput.writeBytes(1 + "#" + 2 + "#" + interestedPieceID + "\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                  while (socketInput.ready() == false) {}
                } catch (Exception e) {
                }
                try {
                  MsgFromOtherPeer = socketInput.readLine() + "";
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                if (msgType == 2) {
                  byte[] piececontent = peerProcess.obtainPiece(Integer.parseInt(msgPayload));
                  piecemsg = msgPayload + "#" + 7 + "#" + piececontent.length + "\n";
                  try {
                    try {
                      peerProcess.logfile.write(
                          LogWriter.logmsg(6, peerProcess.myPeerID, OtherPeerID));
                    } catch (IOException ex) {
                      Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    socketOutput.writeBytes(piecemsg);

                    while (socketInput.ready() == false) {}
                    try {
                      MsgFromOtherPeer = socketInput.readLine();
                    } catch (Exception e) {
                    }
                    RecvdMsg = MsgFromOtherPeer.split("#");
                    msgLen = Integer.parseInt(RecvdMsg[0]);
                    int indexFromOther = msgLen;
                    msgType = Integer.parseInt(RecvdMsg[1]);
                    msgPayload = RecvdMsg[2];
                    int loadsize = Integer.parseInt(msgPayload);
                    byte[] mspiececon = new byte[loadsize];
                    if (msgType == 7) {
                      if (peerProcess.myPeerID < OtherPeerID) {

                        socketOutput.writeBytes(channelClearMsg);

                        while (pieceReader.available() <= 0) {}

                        int totallen = 0;
                        byte[] c = new byte[1];
                        while (totallen < loadsize) {
                          try {
                            pieceReader.read(c);
                          } catch (Exception e) {
                          }
                          mspiececon[totallen] = c[0];
                          totallen++;
                        }
                        socketOutput.write(piececontent);
                        try {

                          peerProcess.writebackPiece(indexFromOther, mspiececon);
                          peerProcess.logfile.write(
                              LogWriter.logmsg(
                                  8,
                                  peerProcess.myPeerID,
                                  OtherPeerID,
                                  interestedPieceID,
                                  peerProcess
                                      .Peers
                                      .elementAt(peerProcess.myPeerID % 1000 - 1)
                                      .hasHowManyPieces()));

                          if (peerProcess
                                  .Peers
                                  .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                                  .obtainAll()
                              == true) {
                            try {
                              peerProcess.logfile.write(LogWriter.logmsg(9, peerProcess.myPeerID));
                            } catch (Exception e) {
                            }
                          }
                        } catch (IOException ex) {
                          Logger.getLogger(SocketThread.class.getName())
                              .log(Level.SEVERE, null, ex);
                        }

                        while (socketInput.ready() == false) {}

                        try {
                          MsgFromOtherPeer = socketInput.readLine();
                        } catch (Exception e) {
                        }
                        RecvdMsg = MsgFromOtherPeer.split("#");
                        msgLen = Integer.parseInt(RecvdMsg[0]);
                        msgType = Integer.parseInt(RecvdMsg[1]);
                        msgPayload = RecvdMsg[2];
                        if (msgType != 10) {
                          System.out.println(
                              "Expecting channel clear message, received message type " + msgType);
                        }
                        continue;
                      } else {

                        while (socketInput.ready() == false) {}

                        try {
                          MsgFromOtherPeer = socketInput.readLine();
                        } catch (Exception e) {
                        }
                        RecvdMsg = MsgFromOtherPeer.split("#");
                        msgLen = Integer.parseInt(RecvdMsg[0]);
                        msgType = Integer.parseInt(RecvdMsg[1]);
                        msgPayload = RecvdMsg[2];
                        if (msgType == 10) {

                          socketOutput.write(piececontent);

                          while (pieceReader.available() <= 0) {}

                          int totallen = 0;
                          byte[] c = new byte[1];
                          while (totallen < loadsize) {
                            try {
                              pieceReader.read(c);
                            } catch (Exception e) {
                            }
                            mspiececon[totallen] = c[0];
                            totallen++;
                          }
                          socketOutput.writeBytes(channelClearMsg);
                          try {

                            peerProcess.writebackPiece(indexFromOther, mspiececon);
                            peerProcess.logfile.write(
                                LogWriter.logmsg(
                                    8,
                                    peerProcess.myPeerID,
                                    OtherPeerID,
                                    interestedPieceID,
                                    peerProcess
                                        .Peers
                                        .elementAt(peerProcess.myPeerID % 1000 - 1)
                                        .hasHowManyPieces()));

                            if (peerProcess
                                    .Peers
                                    .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                                    .obtainAll()
                                == true) {
                              try {
                                peerProcess.logfile.write(
                                    LogWriter.logmsg(9, peerProcess.myPeerID));
                              } catch (Exception e) {
                              }
                            }
                          } catch (IOException ex) {
                            Logger.getLogger(SocketThread.class.getName())
                                .log(Level.SEVERE, null, ex);
                          }
                          continue;
                        } else {
                          System.out.println(
                              "Expecting channel clear message, received message type " + msgType);
                        }
                      }
                    } else {
                      System.out.println(
                          "Expecting piecemessage, received message type " + msgType);
                    }

                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                } else if (msgType == 3) {
                  try {

                    peerProcess.logfile.write(
                        LogWriter.logmsg(7, peerProcess.myPeerID, OtherPeerID));
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  try {

                    MsgFromOtherPeer = socketInput.readLine() + "";
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  RecvdMsg = MsgFromOtherPeer.split("#");
                  msgLen = Integer.parseInt(RecvdMsg[0]);
                  int indexFromOther = msgLen;
                  msgType = Integer.parseInt(RecvdMsg[1]);
                  msgPayload = RecvdMsg[2];
                  int loadsize = Integer.parseInt(msgPayload);
                  byte[] mspiececon = new byte[loadsize];
                  if (msgType == 7) {
                    socketOutput.writeBytes(channelClearMsg);

                    while (pieceReader.available() <= 0) {}
                    int totallen = 0;
                    byte[] c = new byte[1];
                    while (totallen < loadsize) {
                      try {
                        pieceReader.read(c);
                      } catch (Exception e) {
                      }
                      mspiececon[totallen] = c[0];
                      totallen++;
                    }
                    socketOutput.writeBytes(channelClearMsg);
                    try {

                      peerProcess.writebackPiece(indexFromOther, mspiececon);
                      peerProcess.logfile.write(
                          LogWriter.logmsg(
                              8,
                              peerProcess.myPeerID,
                              OtherPeerID,
                              interestedPieceID,
                              peerProcess
                                  .Peers
                                  .elementAt(peerProcess.myPeerID % 1000 - 1)
                                  .hasHowManyPieces()));

                      if (peerProcess
                              .Peers
                              .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                              .obtainAll()
                          == true) {
                        try {
                          peerProcess.logfile.write(LogWriter.logmsg(9, peerProcess.myPeerID));
                        } catch (Exception e) {
                        }
                      }
                    } catch (IOException ex) {
                      Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    continue;
                  } else {
                    System.out.println(
                        "Expecting piece data, received message type" + msgType + ".");
                  }
                }
              }
            }
          } else if (msgType == 0) {

            try {
              peerProcess.logfile.write(LogWriter.logmsg(4, peerProcess.myPeerID, OtherPeerID));
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
              while (socketInput.ready() == false) {}

            } catch (Exception e) {
            }
            try {
              MsgFromOtherPeer = socketInput.readLine();
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            RecvdMsg = MsgFromOtherPeer.split("#");
            msgLen = Integer.parseInt(RecvdMsg[0]);
            msgType = Integer.parseInt(RecvdMsg[1]);
            msgPayload = RecvdMsg[2];
            if (msgType == 2) {
              byte[] piececontent = peerProcess.obtainPiece(Integer.parseInt(msgPayload));
              piecemsg = msgPayload + "#" + 7 + "#" + piececontent.length + "\n";
              try {
                socketOutput.writeBytes(piecemsg);

                while (socketInput.ready() == false) {}

                try {
                  MsgFromOtherPeer = socketInput.readLine();
                } catch (Exception e) {
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                if (msgType == 10) {

                  socketOutput.write(piececontent);

                  while (socketInput.ready() == false) {}

                  try {
                    MsgFromOtherPeer = socketInput.readLine();
                  } catch (Exception e) {
                  }
                  RecvdMsg = MsgFromOtherPeer.split("#");
                  msgLen = Integer.parseInt(RecvdMsg[0]);
                  msgType = Integer.parseInt(RecvdMsg[1]);
                  msgPayload = RecvdMsg[2];
                  if (msgType != 10) {
                    System.out.println(
                        "Expecting channel clear message, received message type " + msgType);
                  }
                  continue;
                } else {
                  System.out.println(
                      "Expecting channel clear message, received message type " + msgType);
                }
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
              try {
                peerProcess.logfile.write(LogWriter.logmsg(6, peerProcess.myPeerID, OtherPeerID));
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
            } else if (msgType == 3) {
              try {

                peerProcess.logfile.write(LogWriter.logmsg(7, peerProcess.myPeerID, OtherPeerID));
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
            }
          } else {
            continue;
          }
        } else {
          try {
            socketOutput.writeBytes(chokedMsg);
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }

          try {
            while (socketInput.ready() == false) {}

          } catch (Exception e) {
          }
          try {
            MsgFromOtherPeer = socketInput.readLine() + "";
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }
          RecvdMsg = MsgFromOtherPeer.split("#");
          msgLen = Integer.parseInt(RecvdMsg[0]);
          msgType = Integer.parseInt(RecvdMsg[1]);
          msgPayload = RecvdMsg[2];
          if (msgType == 0) {

            try {
              peerProcess.logfile.write(LogWriter.logmsg(4, peerProcess.myPeerID, OtherPeerID));
            } catch (Exception e) {
            }

            continue;
          } else if (msgType == 1) {

            try {
              peerProcess.logfile.write(LogWriter.logmsg(3, peerProcess.myPeerID, OtherPeerID));
            } catch (Exception e) {
            }
            try {
              while (socketInput.ready() == false) {}

            } catch (Exception e) {
            }
            try {
              MsgFromOtherPeer = socketInput.readLine() + "";
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            RecvdMsg = MsgFromOtherPeer.split("#");
            msgLen = Integer.parseInt(RecvdMsg[0]);
            msgType = Integer.parseInt(RecvdMsg[1]);
            msgPayload = RecvdMsg[2];
            if (msgType == 4) {

              peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setTheBitField(msgPayload);
              interestedPieceID =
                  peerProcess
                      .Peers
                      .elementAt(peerProcess.myPeerID % 1000 - 1)
                      .obtainInterestedPiece(OtherPeerID);
              try {

                if (interestedPieceID != -1)
                  peerProcess.logfile.write(
                      LogWriter.logmsg(5, peerProcess.myPeerID, OtherPeerID, interestedPieceID));
              } catch (Exception e) {
              }
              if (interestedPieceID == -1) {
                try {
                  socketOutput.writeBytes(1 + "#" + 3 + "#" + "-1\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
              } else {
                try {
                  socketOutput.writeBytes(1 + "#" + 2 + "#" + interestedPieceID + "\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                  while (socketInput.ready() == false) {}

                } catch (Exception e) {
                }
                try {
                  MsgFromOtherPeer = socketInput.readLine() + "";
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                int indexFromOther = msgLen;
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                int loadsize = Integer.parseInt(msgPayload);
                byte[] mspiececon = new byte[loadsize];
                if (msgType == 7) {
                  socketOutput.writeBytes(channelClearMsg);

                  while (pieceReader.available() <= 0) {}
                  int totallen = 0;
                  byte[] c = new byte[1];
                  while (totallen < loadsize) {
                    try {
                      pieceReader.read(c);
                    } catch (Exception e) {
                    }
                    mspiececon[totallen] = c[0];
                    totallen++;
                  }
                  socketOutput.writeBytes(channelClearMsg);
                  try {

                    peerProcess.writebackPiece(indexFromOther, mspiececon);
                    peerProcess.logfile.write(
                        LogWriter.logmsg(
                            8,
                            peerProcess.myPeerID,
                            OtherPeerID,
                            interestedPieceID,
                            peerProcess
                                .Peers
                                .elementAt(peerProcess.myPeerID % 1000 - 1)
                                .hasHowManyPieces()));

                    if (peerProcess
                            .Peers
                            .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                            .obtainAll()
                        == true) {
                      try {
                        peerProcess.logfile.write(LogWriter.logmsg(9, peerProcess.myPeerID));
                      } catch (Exception e) {
                      }
                    }
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  continue;
                } else {
                  System.out.println("received msg type " + msgType + ", expecting piece data.");
                }
              }

            } else {
              System.out.println("received msg type " + msgType + ", expecting have msg.");
            }
          } else {
            System.out.println("received msg type " + msgType + ", expecting choke/unchoke msg.");
          }
        }
      }
    } catch (Exception ite) {
    }

    if (quited == false) {

      try {
        socketOutput.writeBytes(peerExitMsg);
        System.out.println(
            "Peer " + peerProcess.myPeerID + " sent quit message to peer " + OtherPeerID + ".");
        try {
          Thread.sleep(100);
        } catch (Exception e) {
        }
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    System.out.println(
        "The socket between peer"
            + peerProcess.myPeerID
            + "and peer "
            + OtherPeerID
            + " will disconnect now.\n");
    peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setHasAllPieces();
    peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).setHasAllPieces();
  }