Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
mhd0528 committed Nov 15, 2018
1 parent 8e5e9da commit 81b9da3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Common.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ UnchokingInterval 100
OptimisticUnchokingInterval 5
FileName CG.zip
FileSize 1098227
PieceSize 100000
PieceSize 32768
1 change: 1 addition & 0 deletions src/controller/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void setFile(int id, boolean haveFile) {
file.delete(); //Delete and start writing to .temp file to keep intention clean
file = new File(filePath + ".temp");
//file.delete();

}

}
Expand Down
6 changes: 6 additions & 0 deletions src/custom/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package custom;

import model.PeerInfo;

public class Util {

public Util() {
Expand Down Expand Up @@ -32,4 +34,8 @@ public static boolean allTrue (boolean[] values) {
//byte2boolean

//boolean2byte

public static void writeLog(PeerInfo peerinfo, String logContent){

}
}
4 changes: 3 additions & 1 deletion src/model/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class Connection extends Thread{
//check handshake
public Connection(Socket s, List<Connection> connectionList) {
try {
//create log file
peerInfo.setFile();

socket = s;
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
Expand Down Expand Up @@ -51,7 +54,6 @@ public Connection(Socket s, PeerInfo info, List<Connection> connectionList) {
@Override
public void run() {
try {

if (peerInfo != null) {
MessageHandler.getInstance().sendHandShakeMessage(this);
MessageHandler.getInstance().sendBitfieldMessage(this);
Expand Down
57 changes: 56 additions & 1 deletion src/model/PeerInfo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package model;
import java.io.*;
import java.text.*;
import java.util.*;

import controller.ArgReader;

public class PeerInfo {
private int peerID;
private String host;
private int port;
private Boolean hasFile;
private File file;
private String filePath;
BufferedWriter writer;

public PeerInfo(int peerID, String host, int port, Boolean hasFile) {
this.peerID = peerID;
this.host = host;
Expand All @@ -24,5 +33,51 @@ public int getPort() {
public Boolean getHasFile() {
return hasFile;
}

public File getFile(){
return file;
}
public void setFile() throws IOException{
this.filePath = "log_peer_" + this.peerID + ".log"; //file address
this.file = new File(filePath);
writer = new BufferedWriter(new FileWriter(this.filePath));
}
public void writeLog(String logInfo, PeerInfo opPeerInfo) throws IOException{
int opPeerID = opPeerInfo.getId();
if(logInfo == "TCPconnection"){
//BufferedWriter writer = new BufferedWriter(new FileWriter(this.filePath));
//read timer
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
writer.write("[" + dateFormat.format(date) + "]");
writer.write(": Peer [" + peerID + "] makes a connection to Peer [" + opPeerID + "]. ");
}
else if(logInfo == "changeOfPreferredNeighbors"){

}
else if(logInfo == "changeOfOptimisticallyUnchokedNeighbor"){

}
else if(logInfo == "unchoking"){

}
else if(logInfo == "choking"){

}
else if(logInfo == "receivingHaveMessage"){

}
else if(logInfo == "receivingInterestedMessage"){

}
else if(logInfo == "receivingNotInterestedMessage"){

}
else if(logInfo == "downloadingAPiece"){

}
else if(logInfo == "completionOfDownload"){

}
}
}

0 comments on commit 81b9da3

Please sign in to comment.