root / src / Peers / Peer.java
History | View | Annotate | Download (5.75 KB)
1 | 2 | up20160340 | package Peers; |
---|---|---|---|
2 | |||
3 | |||
4 | import java.io.*; |
||
5 | import java.rmi.RemoteException; |
||
6 | import java.rmi.registry.LocateRegistry; |
||
7 | import java.rmi.registry.Registry; |
||
8 | import java.rmi.server.UnicastRemoteObject; |
||
9 | import java.util.concurrent.ConcurrentHashMap; |
||
10 | import java.util.concurrent.ScheduledThreadPoolExecutor; |
||
11 | import java.util.concurrent.TimeUnit; |
||
12 | import java.nio.file.Paths; |
||
13 | |||
14 | import java.nio.file.Paths; |
||
15 | |||
16 | import Channel.Channel; |
||
17 | |||
18 | import Protocols.BackUpProtocol; |
||
19 | import Protocols.DeleteProtocol; |
||
20 | import Protocols.RestoreProtocol; |
||
21 | import RMI.Service; |
||
22 | import Storage.FileSystem; |
||
23 | |||
24 | import static Global.Globals.MAX_SIZE_STORAGE; |
||
25 | |||
26 | public class Peer implements Service, Serializable { |
||
27 | |||
28 | private int id; |
||
29 | |||
30 | private String versionProtocol; |
||
31 | |||
32 | private String remoteObjName; |
||
33 | |||
34 | private Channel mc_channel; |
||
35 | |||
36 | private Channel mdb_channel; |
||
37 | |||
38 | private Channel mdr_channel; |
||
39 | |||
40 | ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(200); |
||
41 | |||
42 | FileSystem storage; |
||
43 | |||
44 | Control control;
|
||
45 | |||
46 | |||
47 | public Peer(String version, String peerId, String remoteName, String mcAddr, int mcPort, String mdbAddr, int mdbPort, String mdrAddr, int mdrPort) { |
||
48 | |||
49 | |||
50 | this.versionProtocol = version;
|
||
51 | this.remoteObjName = remoteName;
|
||
52 | this.id = Integer.parseInt(peerId); |
||
53 | |||
54 | this.storage = new FileSystem(MAX_SIZE_STORAGE, Paths.get(System.getProperty("user.dir")).toString() + "/sdis/", this.id, this); |
||
55 | try {
|
||
56 | mc_channel = new Channel(mcAddr, mcPort, this); |
||
57 | mdb_channel = new Channel(mdbAddr, mdbPort, this); |
||
58 | mdr_channel = new Channel(mdrAddr, mdrPort, this); |
||
59 | |||
60 | if(!loadStatus())
|
||
61 | this.control = new Control(this); |
||
62 | |||
63 | // save data every 2 seconds
|
||
64 | threadPool.scheduleAtFixedRate(() -> { |
||
65 | this.saveState();
|
||
66 | }, 0, 2, TimeUnit.SECONDS); |
||
67 | |||
68 | } catch (IOException e) { |
||
69 | // e.printStackTrace();
|
||
70 | } |
||
71 | |||
72 | |||
73 | /*new Thread(mdb_channel).start();
|
||
74 | new Thread(mdr_channel).start();
|
||
75 | new Thread(mc_channel).start();
|
||
76 | */
|
||
77 | threadPool.execute(mc_channel); |
||
78 | threadPool.execute(mdb_channel); |
||
79 | threadPool.execute(mdr_channel); |
||
80 | |||
81 | } |
||
82 | |||
83 | @Override
|
||
84 | public void backup(String filepath, int replicationDegree) throws IOException { |
||
85 | |||
86 | BackUpProtocol backup = new BackUpProtocol(this, replicationDegree, filepath); |
||
87 | threadPool.execute(backup); |
||
88 | } |
||
89 | |||
90 | @Override
|
||
91 | public void restore(String filepath) throws RemoteException { |
||
92 | RestoreProtocol restore = new RestoreProtocol(this, filepath); |
||
93 | threadPool.execute(restore); |
||
94 | |||
95 | } |
||
96 | |||
97 | @Override
|
||
98 | public void delete(String filepath) throws RemoteException { |
||
99 | DeleteProtocol delete = new DeleteProtocol(this, filepath); |
||
100 | threadPool.execute(delete); |
||
101 | } |
||
102 | |||
103 | @Override
|
||
104 | public void manage(int space) throws RemoteException { |
||
105 | |||
106 | } |
||
107 | |||
108 | @Override
|
||
109 | public void state() throws RemoteException { |
||
110 | |||
111 | } |
||
112 | |||
113 | |||
114 | public static void main(String args[]) throws RemoteException { |
||
115 | if (args.length != 9) { |
||
116 | System.out.println("Usage: java Peers.Peer <version> <peer id> <access_point> <MC_IP_address>" + |
||
117 | " <MC_port> <MDB_IP_address> <MDB_port> <MDR_IP_address> <MDR_port>");
|
||
118 | return;
|
||
119 | } |
||
120 | |||
121 | Peer peer = null;
|
||
122 | |||
123 | try {
|
||
124 | |||
125 | peer = new Peer(args[0], args[1], args[2], args[3], Integer.parseInt(args[4]), args[5], Integer.parseInt(args[6]), args[7], Integer.parseInt(args[8])); |
||
126 | Service serv = (Service) UnicastRemoteObject.exportObject(peer, 0); |
||
127 | Registry registry = LocateRegistry.getRegistry(); |
||
128 | registry.bind(args[2], serv);
|
||
129 | } catch (Exception e) { |
||
130 | System.out.println("Server Exception: " + e.getMessage()); |
||
131 | e.printStackTrace(); |
||
132 | } |
||
133 | |||
134 | try {
|
||
135 | peer.threadPool.awaitTermination(1, TimeUnit.DAYS); |
||
136 | } catch (Exception e) { |
||
137 | // e.printStackTrace();
|
||
138 | } |
||
139 | } |
||
140 | |||
141 | public FileSystem getStorage() {
|
||
142 | return storage;
|
||
143 | } |
||
144 | |||
145 | public String getVersionProtocol() { |
||
146 | return versionProtocol;
|
||
147 | } |
||
148 | |||
149 | public Channel getMc_channel() { |
||
150 | return mc_channel;
|
||
151 | } |
||
152 | |||
153 | public Channel getMdb_channel() { |
||
154 | return mdb_channel;
|
||
155 | } |
||
156 | |||
157 | public Channel getMdr_channel() { |
||
158 | return mdr_channel;
|
||
159 | } |
||
160 | |||
161 | public int getId() { |
||
162 | return id;
|
||
163 | } |
||
164 | |||
165 | public ScheduledThreadPoolExecutor getThreadPool() { |
||
166 | return threadPool;
|
||
167 | } |
||
168 | |||
169 | public Control getControl() { |
||
170 | return control;
|
||
171 | } |
||
172 | |||
173 | |||
174 | private void saveState() { |
||
175 | try {
|
||
176 | |||
177 | FileOutputStream fileCtrl = new FileOutputStream(Paths.get(System.getProperty("user.dir")).toString() + "/objs/" + id + "Ctrl.ser"); |
||
178 | |||
179 | ObjectOutputStream out2 = new ObjectOutputStream(fileCtrl); |
||
180 | |||
181 | out2.writeObject(this.control);
|
||
182 | |||
183 | out2.close(); |
||
184 | fileCtrl.close(); |
||
185 | } catch (IOException e) { |
||
186 | new File(Paths.get(System.getProperty("user.dir")).toString() + "/objs").mkdir(); |
||
187 | try {
|
||
188 | new File(Paths.get(System.getProperty("user.dir")).toString() + "/objs/" + id + "Ctrl.ser").createNewFile(); |
||
189 | } catch (IOException e1) { |
||
190 | // e1.printStackTrace();
|
||
191 | } |
||
192 | |||
193 | //e.printStackTrace();
|
||
194 | } |
||
195 | } |
||
196 | |||
197 | private boolean loadStatus() { |
||
198 | |||
199 | try {
|
||
200 | FileInputStream fileCtrl = new FileInputStream(Paths.get(System.getProperty("user.dir")).toString() + "/objs/" + id + "Ctrl.ser"); |
||
201 | |||
202 | ObjectInputStream obj2 = new ObjectInputStream(fileCtrl); |
||
203 | this.control = (Control) obj2.readObject(); |
||
204 | |||
205 | obj2.close(); |
||
206 | fileCtrl.close(); |
||
207 | return true; |
||
208 | } catch (Exception e) { |
||
209 | System.out.println("no files found"); |
||
210 | } |
||
211 | |||
212 | return false; |
||
213 | } |
||
214 | |||
215 | } |
||
216 |