Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/bishakh/psync/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ public void run() {
}
else{
try {
new Thread(fileTransporter.new ListFetcher(controller, new URL("http://"+s+":8080/list"), s)).start();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just change this port while testing. Do not commit this change.

new Thread(fileTransporter.new ListFetcher(controller, new URL("http://"+s+":5345/list"), s)).start();
} catch (MalformedURLException e) {
e.printStackTrace();
}
Expand Down
43 changes: 40 additions & 3 deletions src/main/java/bishakh/psync/Discoverer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bishakh.psync;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this?

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
Expand All @@ -25,6 +26,10 @@ public class Discoverer {
HashMap<String,String>mp = new HashMap<>(); /* Store contact information */

String BROADCAST_IP;
final String DATABASE_NAME;
final String DATABASE_PATH;


int PORT;
String PEER_ID;
Logger logger;
Expand All @@ -40,12 +45,26 @@ public class Discoverer {
public volatile ConcurrentHashMap<String, ArrayList<String>> priorityPeerList;
public volatile ConcurrentHashMap<String, ArrayList<String>> originalPeerList;

public Discoverer(String BROADCAST_IP, String PEER_ID, int PORT, Logger LoggerObj,File ffile) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fileName? what file name. use a better variable name please.

public Discoverer(String BROADCAST_IP, String PEER_ID, int PORT, Logger LoggerObj,String fileName,String databaseDirectory ) throws IOException {
this.BROADCAST_IP = BROADCAST_IP;
this.PORT = PORT;
this.PEER_ID = PEER_ID;
this.logger = LoggerObj;
this.file = ffile; /*file is assigned*/
this.DATABASE_NAME = fileName;
this.DATABASE_PATH = databaseDirectory+DATABASE_NAME;
this.file = new File(DATABASE_PATH);

if(file.exists())
{
System.out.println("File exists");
}
else
{
System.out.println("File not there");
file.createNewFile();
System.out.println("File is created");
}


// Initialize priorities (lower int = higher priority)
// The peers whose ID starts with these keywords will have the priority
Expand All @@ -68,6 +87,7 @@ public Discoverer(String BROADCAST_IP, String PEER_ID, int PORT, Logger LoggerOb
thread[0] = new Thread(broadcastThread);
thread[1] = new Thread(listenThread);
thread[2] = new Thread(peerExpiryThread);

}

public void startBroadcast(){
Expand Down Expand Up @@ -123,7 +143,23 @@ public void stopPeerExpiry() {
}
}

public void startDiscoverer(){
public void startDiscoverer() throws FileNotFoundException {
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
String line =bufferedReader.readLine();
while(line!=null)
{
String[] one=line.split(" ",100); /// here length of PEER_ID is restricted
mp.put(one[0],one[1]);
line=bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
startBroadcast();
startListener();
startPeerExpiry();
Expand Down Expand Up @@ -412,4 +448,5 @@ public void stop() {
}



}
2 changes: 1 addition & 1 deletion src/main/java/bishakh/psync/FileTransporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void downloadFile(String fileID, String fileName, String filePath, String
if(!parent.exists() && !parent.mkdirs()){
throw new IllegalStateException("Couldn't create dir: " + parent);
}
URL fileUrl = new URL("http://"+ peerIP +":8080/getFile/" + fileID);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. port.

URL fileUrl = new URL("http://"+ peerIP +":5345/getFile/" + fileID);
ResumeDownloadThread resumeDownloadThread = new ResumeDownloadThread(fileUrl , fileID, fileName, f, startByte, endByte, fileSize, peerID);
Thread t = new Thread(resumeDownloadThread);
ongoingDownloadThreads.put(t, resumeDownloadThread);
Expand Down
65 changes: 27 additions & 38 deletions src/main/java/bishakh/psync/SyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,73 @@ public class SyncService {
private static String mapFileServerDirectory = "/home/alarm/DMS/";
private static String databaseAndLogDirectory = "/home/alarm/DMS/";
private static String databaseName = "fileDB.txt";
private static String contactFile = "contact.txt";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contactFileName or contactHistoryFileName

more descriptive name = better

private static String contactName ="contact.txt";


public Logger logger;
public WebServer webServer;
public Discoverer discoverer;
public FileManager fileManager;
public FileTransporter fileTransporter;
public Controller controller;
public File nfile;


public SyncService() throws IOException {

public SyncService() {
File nfile =new File(contactFile);
//System.exit(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,nfile);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,contactName,databaseAndLogDirectory);
fileManager = new FileManager(PEER_ID, databaseName, databaseAndLogDirectory, syncDirectory, mapFileServerDirectory, logger);
fileTransporter = new FileTransporter(syncDirectory, logger);
controller = new Controller(discoverer, fileManager, fileTransporter, syncInterval, maxRunningDownloads, logger, 2, true);
webServer = new WebServer(8080, controller, logger);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port

webServer = new WebServer(5345, controller, logger);

}


public SyncService(String inputPeerId, String baseDirectory) {
syncDirectory = baseDirectory + File.separator + "sync" + File.separator;
public SyncService(String inputPeerId, String baseDirectory) throws IOException {
syncDirectory = baseDirectory + "sync" + "/";
mapFileServerDirectory = baseDirectory;
databaseAndLogDirectory = baseDirectory;
PEER_ID = inputPeerId;
File nfile =new File(contactFile);

//System.exit(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,nfile);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,contactName,databaseAndLogDirectory);
fileManager = new FileManager(PEER_ID, databaseName, databaseAndLogDirectory, syncDirectory, mapFileServerDirectory, logger);
fileTransporter = new FileTransporter(syncDirectory, logger);
controller = new Controller(discoverer, fileManager, fileTransporter, syncInterval, maxRunningDownloads, logger, 2, true);
webServer = new WebServer(8080, controller, logger);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port

webServer = new WebServer(5345, controller, logger);
}

public SyncService(String inputPeerId, String baseDirectory, int priorityMethod) {
syncDirectory = baseDirectory + File.separator + "sync" + File.separator;
public SyncService(String inputPeerId, String baseDirectory, int priorityMethod) throws IOException {
syncDirectory = baseDirectory +"sync" + "/";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? why this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose my baseDirectory is provided as "C:/Users/Desktop/WorkingDirectory/" (with a trailing '/').
So, no need to give File.separator after baseDirectory unless syncDirectory would be like "C:/Users/Desktop/WorkingDirectory/ \ sync".
And I just mentioned '/' instead of File.separator, it does not create problem.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay then I am replacing last '/' with File.separator

mapFileServerDirectory = baseDirectory;
databaseAndLogDirectory = baseDirectory;
PEER_ID = inputPeerId;

File nfile =new File(contactFile);
//System.exit(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented codes.

logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,nfile);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,contactName,databaseAndLogDirectory);
fileManager = new FileManager(PEER_ID, databaseName, databaseAndLogDirectory, syncDirectory, mapFileServerDirectory, logger);
fileTransporter = new FileTransporter(syncDirectory, logger);
controller = new Controller(discoverer, fileManager, fileTransporter, syncInterval, maxRunningDownloads, logger, priorityMethod, true);
webServer = new WebServer(8080, controller, logger);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port

webServer = new WebServer(5345, controller, logger);
}

public SyncService(String inputPeerId, String baseDirectory, int priorityMethod, boolean restrictedEpidemicFlag) {
syncDirectory = baseDirectory + File.separator + "sync" + File.separator;
public SyncService(String inputPeerId, String baseDirectory, int priorityMethod, boolean restrictedEpidemicFlag) throws IOException {

//System.exit(0);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

syncDirectory = baseDirectory + "sync" + "/";
mapFileServerDirectory = baseDirectory;
databaseAndLogDirectory = baseDirectory;
PEER_ID = inputPeerId;
File nfile =new File(contactFile);
logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,nfile);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger,contactName,databaseAndLogDirectory);
fileManager = new FileManager(PEER_ID, databaseName, databaseAndLogDirectory, syncDirectory, mapFileServerDirectory, logger);
fileTransporter = new FileTransporter(syncDirectory, logger);
controller = new Controller(discoverer, fileManager, fileTransporter, syncInterval, maxRunningDownloads, logger, priorityMethod, restrictedEpidemicFlag);
webServer = new WebServer(8080, controller, logger);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

port

webServer = new WebServer(5345, controller, logger);

}

Expand All @@ -89,22 +93,7 @@ public SyncService(String inputPeerId, String baseDirectory, int priorityMethod,
public void start() throws FileNotFoundException {


FileReader fileReader = new FileReader(nfile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
try {
String line =bufferedReader.readLine();
while(line!=null)
{
String[] one=line.split(" ",100); /// here length of PEER_ID is restricted
discoverer.mp.put(one[0],one[1]);
line=bufferedReader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
PrintWriter writer = new PrintWriter(nfile);
writer.print("");
writer.close();

discoverer.startDiscoverer();
fileManager.startFileManager();
controller.startController();
Expand All @@ -122,7 +111,7 @@ public void stop() {
webServer.stop();
}

public static void main(final String[] args) throws FileNotFoundException {
public static void main(final String[] args) throws IOException {
System.out.println(args.length);
if(args.length < 2){
SyncService s = new SyncService();
Expand Down