Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact Information #20

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
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
11 changes: 7 additions & 4 deletions psyncPC.iml
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.6.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:3.8.1" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.6.2" level="project" />
</component>
</module>
64 changes: 61 additions & 3 deletions src/main/java/bishakh/psync/Discoverer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
* The Discoverer module : Find Peers in communication ranges
*/
public class Discoverer {

HashMap<String,String>contactMap = new HashMap<>(); /* Store contact information */

String BROADCAST_IP;
final String DATABASE_NAME;
final String DATABASE_PATH;


int PORT;
String PEER_ID;
Logger logger;
File file; /* File attribute added */
final Thread[] thread = new Thread[3];
final BroadcastThread broadcastThread;
final ListenThread listenThread;
Expand All @@ -31,11 +42,20 @@ 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) {
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.DATABASE_NAME = fileName;
this.DATABASE_PATH = databaseDirectory+DATABASE_NAME;
this.file = new File(DATABASE_PATH);

if(!file.exists())
{
file.createNewFile();
}


// Initialize priorities (lower int = higher priority)
// The peers whose ID starts with these keywords will have the priority
Expand All @@ -58,6 +78,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 @@ -113,7 +134,24 @@ public void stopPeerExpiry() {
}
}

public void startDiscoverer(){
public void startDiscoverer() throws FileNotFoundException {
/*when discoverer starts, previous information in contactHistory.txt gets stored in HashMap "contactMap" */
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
contactMap.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 @@ -287,6 +325,25 @@ public void run() {
if(willUpdatePeer) {
String peerID = new String(datagramPacket.getData(), 0, datagramPacket.getLength());
updatePeers(datagramPacket.getAddress().getHostAddress(), peerID);

/* While listening to a device, everytime contactHistory.txt file is updated*/

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
Calendar cal = Calendar.getInstance();
String timeStamp = dateFormat.format(cal.getTime());
contactMap.put(peerID, timeStamp);
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
PrintWriter printWriter = new PrintWriter(bufferedWriter);
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
for(Map.Entry m:contactMap.entrySet())
{
printWriter.println(m.getKey()+" "+m.getValue());
}
printWriter.close();

}
} // end of while
}catch (UnknownHostException e){
Expand Down Expand Up @@ -382,4 +439,5 @@ public void stop() {
}


}

}
44 changes: 30 additions & 14 deletions src/main/java/bishakh/psync/SyncService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.text.SimpleDateFormat;
import java.util.Calendar;


public class SyncService {

private static final String BROADCAST_IP = "172.16.5.255";
Expand All @@ -17,69 +18,82 @@ 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 contactName ="contactHistoryFile.txt";


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


public SyncService() throws IOException {


public SyncService() {
logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger);
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
Member

Choose a reason for hiding this comment

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

port


}


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;


logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger);
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
Member

Choose a reason for hiding this comment

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

port

}

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
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
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
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
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;


logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger);
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
Member

Choose a reason for hiding this comment

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

port

}

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 {


syncDirectory = baseDirectory + "sync" + "/";
mapFileServerDirectory = baseDirectory;
databaseAndLogDirectory = baseDirectory;
PEER_ID = inputPeerId;

logger = new Logger(databaseAndLogDirectory, PEER_ID);
discoverer = new Discoverer(BROADCAST_IP, PEER_ID, PORT, logger);
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
Member

Choose a reason for hiding this comment

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

port


}



public void start(){
public void start() throws FileNotFoundException {



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

public static void main(final String[] args) {
public static void main(final String[] args) throws IOException {
System.out.println(args.length);
if(args.length < 2){
SyncService s = new SyncService();
Expand Down Expand Up @@ -147,6 +161,8 @@ public void uncaughtException(Thread t, Throwable e) {
}
}
});


}

}