root / src / peer / Peer.java @ 1
History | View | Annotate | Download (4.53 KB)
1 |
package peer; |
---|---|
2 |
|
3 |
import chord.CheckPredecessor; |
4 |
import chord.ChordManager; |
5 |
import chord.ConnectionInfo; |
6 |
import chord.Stabilize; |
7 |
import files.FileHandler; |
8 |
import messages.LookupMessage; |
9 |
import messages.MessageForwarder; |
10 |
import protocols.Backup; |
11 |
import protocols.Delete; |
12 |
import protocols.Reclaim; |
13 |
import protocols.Restore; |
14 |
|
15 |
import java.io.*; |
16 |
import java.math.BigInteger; |
17 |
import java.net.InetAddress; |
18 |
import java.net.UnknownHostException; |
19 |
import java.rmi.registry.LocateRegistry; |
20 |
import java.rmi.registry.Registry; |
21 |
import java.rmi.server.UnicastRemoteObject; |
22 |
import java.util.concurrent.Executors; |
23 |
import java.util.concurrent.ScheduledExecutorService; |
24 |
import java.util.concurrent.TimeUnit; |
25 |
|
26 |
public class Peer implements RMIStub { |
27 |
private static String peerAccessPoint; |
28 |
public static int port; |
29 |
public static ConnectionInfo connectionInfo; |
30 |
public static final CheckPredecessor checkPredecessor = new CheckPredecessor(500); |
31 |
public static Storage storage; |
32 |
private static Peer instance = null; |
33 |
|
34 |
static {
|
35 |
try {
|
36 |
connectionInfo = new ConnectionInfo(new BigInteger(String.valueOf(0)), InetAddress.getLocalHost().getHostAddress(), 0); |
37 |
} catch (UnknownHostException e) { |
38 |
e.printStackTrace(); |
39 |
} |
40 |
} |
41 |
|
42 |
public static ScheduledExecutorService executor; |
43 |
|
44 |
public static void main(String args[]) throws IOException { |
45 |
if (args.length < 2 || args.length > 4) |
46 |
return;
|
47 |
|
48 |
initAtributes(args); |
49 |
|
50 |
Thread th = new Thread(new PeerReceiver(port)); |
51 |
th.start(); |
52 |
|
53 |
executor = Executors.newScheduledThreadPool(100); |
54 |
|
55 |
ChordManager ci = new ChordManager();
|
56 |
executor.submit(ci); |
57 |
|
58 |
storage = new Storage(90000000); |
59 |
if (storage.getSpaceReserved() < storage.getSpaceOcupied())
|
60 |
FileHandler.clearStorageSpace();
|
61 |
|
62 |
if (connectionInfo.getPort() != 0) { |
63 |
MessageForwarder.sendMessage(new LookupMessage(new ConnectionInfo(ChordManager.peerHash, InetAddress.getLocalHost().getHostAddress(), Peer.port), Peer.connectionInfo.getIp(), Peer.connectionInfo.getPort())); |
64 |
} |
65 |
|
66 |
executor.scheduleAtFixedRate(checkPredecessor, 0, 500, TimeUnit.MILLISECONDS); |
67 |
executor.scheduleAtFixedRate(new Stabilize(), 0, 500, TimeUnit.MILLISECONDS); |
68 |
|
69 |
RMIStub stub; |
70 |
instance = new Peer();
|
71 |
|
72 |
stub = (RMIStub) UnicastRemoteObject.exportObject(instance, 0); |
73 |
|
74 |
try {
|
75 |
Registry reg = LocateRegistry.getRegistry(); |
76 |
reg.rebind(peerAccessPoint, stub); |
77 |
System.out.println("peer.Peer connected through getRegistry"); |
78 |
} catch (Exception e) { |
79 |
Registry reg = LocateRegistry.createRegistry(1099); |
80 |
reg.rebind(peerAccessPoint, stub); |
81 |
System.out.println("peer.Peer connected through createRegistry"); |
82 |
} |
83 |
} |
84 |
|
85 |
private static void initAtributes(String[] args) throws IOException { |
86 |
peerAccessPoint = args[0];
|
87 |
try {
|
88 |
port = Integer.parseInt(args[1]); |
89 |
} catch (NumberFormatException e) { |
90 |
System.err.println("port must be an integer value"); |
91 |
System.exit(-1); |
92 |
} |
93 |
|
94 |
if (args.length == 3) { |
95 |
try{
|
96 |
connectionInfo.setPort(Integer.parseInt(args[2])); |
97 |
} catch(NumberFormatException e){ |
98 |
System.err.println("connecter's port must be an integer value"); |
99 |
System.exit(-1); |
100 |
} |
101 |
} else if (args.length == 4) { |
102 |
connectionInfo.setIp(args[2]);
|
103 |
try{
|
104 |
connectionInfo.setPort(Integer.parseInt(args[3])); |
105 |
} catch(NumberFormatException e){ |
106 |
System.err.println("connecter's port must be an integer value"); |
107 |
System.exit(-1); |
108 |
} |
109 |
} |
110 |
|
111 |
} |
112 |
|
113 |
public static String getPeerAccessPoint() { |
114 |
return peerAccessPoint;
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
public void backupProtocol(String file, int replicationDeg) { |
119 |
Backup backup = new Backup(file, replicationDeg);
|
120 |
backup.run(); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public void restoreProtocol(String file) { |
125 |
Restore restore = new Restore(file);
|
126 |
restore.run(); |
127 |
} |
128 |
|
129 |
@Override
|
130 |
public void deleteProtocol(String file) { |
131 |
Delete delete = new Delete(file);
|
132 |
delete.run(); |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public void reclaimProtocol(int reservedSpace) { |
137 |
Reclaim reclaim = new Reclaim(reservedSpace);
|
138 |
reclaim.run(); |
139 |
} |
140 |
} |