-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeer.java
59 lines (49 loc) · 2.49 KB
/
Peer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import ConfigurationParsers.PeerConfig;
import ConfigurationParsers.CommonFileParser;
import ConfigurationParsers.ParametersParser;
import ConfigurationParsers.FileFormatExxception;
import Communicators.CommunicationHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;
import java.util.Map;
import java.io.IOException;
public class Peer
{
private final String commonConfigFileName = System.getProperty("user.dir") + "/Common.cfg";
private final String peerConfigFileName = System.getProperty("user.dir") + "/PeerInfo.cfg";
private Map<String, CommunicationHandler> fileToTransceiverMap;
private Map<String, Map<Integer, PeerConfig>> fileToPeerConfigMap;
private Map<String, CommonFileParser> fileToCommonConfigMap = null;
private final int myPeerID;
private static final Logger logger = Logger.getLogger("A");
public Peer(int myPeerID) throws FileFormatExxception, IOException, InterruptedException
{
this.myPeerID = myPeerID;
this.fileToCommonConfigMap = new HashMap<String, CommonFileParser>();
this.fileToPeerConfigMap = new HashMap<String, Map<Integer,PeerConfig>>();
this.fileToTransceiverMap = new HashMap<String, CommunicationHandler>();
this.startup();
}
private void startup() throws FileFormatExxception, IOException, InterruptedException
{
ParametersParser commonConfigReader = new ParametersParser(commonConfigFileName);
CommonFileParser myCommonConfig = commonConfigReader.getCommonConfig();
this.fileToCommonConfigMap.put(myCommonConfig.getFileName(), myCommonConfig);
ParametersParser peerConfigReader = new ParametersParser(peerConfigFileName);
Map<Integer, PeerConfig> peerConfigMap = peerConfigReader.getPeerConfigMap();
this.fileToPeerConfigMap.put(myCommonConfig.getFileName(), peerConfigMap);
CommunicationHandler aTransceiver = new CommunicationHandler(myCommonConfig, peerConfigMap, myPeerID);
this.fileToTransceiverMap.put(myCommonConfig.getFileName(), aTransceiver);
aTransceiver.start();
logger.info("Transceiver started for PeerID = " + this.myPeerID);
}
public static void main(String[] args) throws NumberFormatException, FileFormatExxception, IOException, InterruptedException
{
if(args.length < 1)
{
System.out.println("Usage: java Peer <PeerID>");
}
Peer p = new Peer(Integer.parseInt(args[0]));
}
}