Skip to content

Commit

Permalink
add LogType
Browse files Browse the repository at this point in the history
  • Loading branch information
wytizxy committed Nov 16, 2018
1 parent 663c7c5 commit bdc6351
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
24 changes: 13 additions & 11 deletions src/controller/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import custom.Config.*;

import custom.Config;
import model.PeerInfo;

public class Log {
Expand All @@ -29,7 +31,7 @@ public Log() {
public void setOpPeer(PeerInfo opPeer) {
this.opPeer = opPeer;
}
public void writeLog(String logInfo) throws IOException{
public void writeLog(LogType logType) throws IOException{
if (opPeer == null) {
System.out.println("opPeer is null, can't not write the log");
return;
Expand All @@ -38,7 +40,7 @@ public void writeLog(String logInfo) throws IOException{
System.out.println("start write log.");
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
if(logInfo.equals("TCPconnection")){
if(logType == LogType.TCPConnection){
//read timer
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
Expand All @@ -47,27 +49,27 @@ public void writeLog(String logInfo) throws IOException{
writer.newLine();
writer.close();
}
else if(logInfo.equals("changeOfPreferredNeighbors")){
else if(logType == LogType.ChangeOfPreferredNeighbor){
writer.write("[" + dateFormat.format(date) + "]");
//writer.write(": Peer [" + peerID + "] has the preferred neighbors [" + MyTimer + "].");
}
else if(logInfo.equals("changeOfOptimisticallyUnchokedNeighbor")){
else if(logType == LogType.ChangeOfOptUnchokedNeighbor){

}
else if(logInfo.equals("unchoking")){
else if(logType == LogType.Unchoking){
writer.write("[" + dateFormat.format(date) + "]");
writer.write(": Peer [" + myInfo.getId() + "] is unchoked by [" + opPeer.getId() + "].");
writer.newLine();
writer.close();

}
else if(logInfo.equals("choking")){
else if(logType == LogType.Choking){
writer.write("[" + dateFormat.format(date) + "]");
writer.write(": Peer [" + myInfo.getId() + "] is choked by [" + opPeer.getId() + "].");
writer.newLine();
writer.close();
}
else if(logInfo.equals("receivingHaveMessage")){
else if(logType == LogType.ReceivingHaveMessage){
//read timer
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
Expand All @@ -76,7 +78,7 @@ else if(logInfo.equals("receivingHaveMessage")){
writer.newLine();
writer.close();
}
else if(logInfo.equals("receivingInterestedMessage")){
else if(logType == LogType.ReceivingInterestedMessage){
//read timer
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
Expand All @@ -85,7 +87,7 @@ else if(logInfo.equals("receivingInterestedMessage")){
writer.newLine();
writer.close();
}
else if(logInfo.equals("receivingNotInterestedMessage")){
else if(logType == LogType.ReceivingNotInterestedMessage){
//read timer
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
Expand All @@ -94,7 +96,7 @@ else if(logInfo.equals("receivingNotInterestedMessage")){
writer.newLine();
writer.close();
}
else if(logInfo.equals("downloadingAPiece")){
else if(logType == LogType.DownloadingAPiece){
//read timer
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
Expand All @@ -103,7 +105,7 @@ else if(logInfo.equals("downloadingAPiece")){
//writer.newLine();
writer.close();
}
else if(logInfo.equals("completionOfDownload")){
else if(logType == LogType.CompletionOfDownload){
//read timer
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
Expand Down
5 changes: 3 additions & 2 deletions src/controller/MessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import custom.Config.MessageType;
import java.math.*;
import java.util.List;
import custom.Config.*;

public class MessageHandler {
private Connection connect;
Expand All @@ -31,7 +32,7 @@ public void handleHandshakeMessage(Message message) throws Exception{
connect.setReceivedHandShake();
//set log
connect.getLogger().setOpPeer(peerInfo);
connect.getLogger().writeLog("TCPconnection");
connect.getLogger().writeLog(LogType.TCPConnection);
System.out.println("receive handshake from " + connect.getPeerInfo().getId());
//send handshake
sendHandShakeMessage();
Expand Down Expand Up @@ -199,7 +200,7 @@ public int handlePieceMessage(Message message, List<Connection> connectionList)
BitfieldManager.getInstance().updateBitfield(peerInfo, pieceNum); //update Bitfield
if (BitfieldManager.getInstance().isAllReceived(peerInfo)) {
FileManager.getInstance().renameTemp();
connect.getLogger().writeLog("completionOfDownload");
connect.getLogger().writeLog(LogType.CompletionOfDownload);
}
//broadcast have message
for (Connection connection : connectionList) {
Expand Down
13 changes: 12 additions & 1 deletion src/custom/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@ public static enum MessageType {
PIECE, //7
HANDSHAKE //8
};

public static enum LogType {
TCPConnection, //0
ChangeOfPreferredNeighbor, //1
ChangeOfOptUnchokedNeighbor, //2
Unchoking, //3
Choking, //4
ReceivingHaveMessage, //5
ReceivingInterestedMessage, //6
ReceivingNotInterestedMessage, //7
DownloadingAPiece, //8
CompletionOfDownload, //9
}
}
12 changes: 6 additions & 6 deletions src/model/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Connection(Socket s, PeerInfo info, peerProcess controller) {
if (info != null) {
peerInfo = info;
log.setOpPeer(peerInfo);
log.writeLog("TCPconnection");
log.writeLog(LogType.TCPConnection);
}
super.start();
} catch(Exception e) {
Expand Down Expand Up @@ -61,31 +61,31 @@ public void run() {
case CHOKE: {
peerChokeMe = true;
System.out.println(peerInfo.getId() + " choke me");
log.writeLog("choking");
log.writeLog(LogType.Choking);
}
break;
case UNCHOKE: {
peerChokeMe = false;
messageHandler.handleUnchokedMessage(message);
System.out.println(peerInfo.getId() + " unchoke me");
log.writeLog("unchoking");
log.writeLog(LogType.Unchoking);
}
break;
case INTERESTED: {
System.out.println(peerInfo.getId() + " interest me");
peerInterestMe = true;
log.writeLog("receivingInterestedMessage");
log.writeLog(LogType.ReceivingInterestedMessage);
}
break;
case NOT_INTERESTED: {
System.out.println(peerInfo.getId() + " notInterest me");
peerInterestMe = false;
log.writeLog("receivingNotInterestedMessage");
log.writeLog(LogType.ReceivingNotInterestedMessage);
}
break;
case HAVE: {
messageHandler.handleHaveMessage(message);
log.writeLog("receivingHaveMessage");
log.writeLog(LogType.ReceivingHaveMessage);
}
break;
case BITFIELD: {
Expand Down

0 comments on commit bdc6351

Please sign in to comment.