Project

General

Profile

Statistics
| Revision:

root / project / src / main / java / peer / Peer.java @ 1

History | View | Annotate | Download (11.2 KB)

1 1 up20120064
package main.java.peer;
2
3
4
import main.java.database.Database;
5
import main.java.file.FileID;
6
import main.java.listeners.Listener;
7
import main.java.protocols.Backup;
8
import main.java.protocols.Delete;
9
import main.java.protocols.Reclaim;
10
import main.java.protocols.Restore;
11
import main.java.service.RMI;
12
13
import java.io.IOException;
14
import java.net.InetAddress;
15
import java.net.MulticastSocket;
16
17
import java.io.*;
18
import java.net.*;
19
import java.rmi.RemoteException;
20
import java.rmi.registry.LocateRegistry;
21
import java.rmi.registry.Registry;
22
import java.rmi.server.UnicastRemoteObject;
23
import java.util.concurrent.ConcurrentHashMap;
24
25
import static main.java.utils.Utilities.getLocalAddress;
26
import static main.java.utils.Constants.*;
27
28
public class Peer implements RMI {
29
30
31
32
33
    private static Listener MCChannel;  //MC CHANNEL
34
    private static Listener MDBChannel; //BACKUP CHANNEL
35
    private static Listener MDRChannel; //RESTORE CHANNEL
36
37
    private static String rmiRemoteObject;
38
39
    private static int ID;
40
    private static float protocolVersion;
41
42
    private static InetAddress MCAddress;
43
    private static InetAddress MDBAddress;
44
    private static InetAddress MDRAddress;
45
46
    private static int MCPort;
47
    private static int MDBPort;
48
    private static int MDRPort;
49
50
    private static InetAddress ip;
51
52
    private static volatile Database db;
53
    private static Disk disk;
54
55
    public static boolean restoring;
56
57
    /*
58
        javac -cp /Users/zemiguel/IdeaProjects/SDIS-P1/src/ peer/Peer.java
59
        DENTRO DO /src/: rmiregistry &
60
        usage:
61
        protocol version,the server id, service access point, MC, MDB, MDR
62
        rmi init example:
63
        java -Djava.net.preferIPv4Stack=true -Djava.rmi.server.codebase=file:/Users/zemiguel/IdeaProjects/SDIS-P1/src/
64
        main/java/service/ main/java/peer/Peer 1.0 0 192.168.0.1 224.0.0.0:8000 224.0.0.0:8001 224.0.0.0:8002
65
        1.0, 0, 192.168.0.1, 224.0.0.0:8000, 224.0.0.0:8001, 224.0.0.0:8002
66
        normal peer example:
67
        java peer.Peer 1.0 1 224.0.0.0:8000 224.0.0.0:8001 224.0.0.0:8002
68
    */
69
    public static void main(String[] args) throws IOException, ClassNotFoundException {
70
71
        if(!parseArgs(args)) {
72
            System.out.println("Bad arguments");
73
            System.out.println("USAGE (RMI): java peer.Peer <protocol_version> <service_access_point> <MCADDR>:<MCPORT> " +
74
                    "<MDBADDR>:<MDBPORT> <MDRADDR>:<MDRPORT>");
75
            System.out.println("USAGE (NON-RMI): java peer.Peer <protocol_version> <MCADDR>:<MCPORT> " +
76
                    "<MDBADDR>:<MDBPORT> <MDRADDR>:<MDRPORT>");
77
            return;
78
        }
79
80
81
        ip = getLocalAddress();
82
83
        MCChannel = new Listener(MCAddress, MCPort);
84
        MDBChannel = new Listener(MDBAddress, MDBPort);
85
        MDRChannel = new Listener(MDRAddress, MDRPort);
86
87
        loadDisk();
88
        loadDatabase();
89
90
        db.printDatabase();
91
        disk.printDisk();
92
        restoring = false;
93
94
95
        System.out.println("Start Listening on MC Channel...");
96
        new Thread(MCChannel).start();
97
        System.out.println("Start Listening on MDB Channel...");
98
        new Thread(MDBChannel).start();
99
        System.out.println("Start Listening on MDR Channel...");
100
        new Thread(MDRChannel).start();
101
102
        return;
103
104
105
106
107
108
    }
109
110
    public static void saveDBToDisk() {
111
112
        File dir = new File("peer"+getID()+"/database/");
113
114
        if(!dir.exists()){
115
            System.out.println("creating directory: " + dir.getName());
116
117
            try{
118
                dir.mkdirs();
119
            }
120
            catch(SecurityException se){
121
                se.printStackTrace();
122
            }
123
124
        }
125
126
        FileOutputStream fos = null;
127
        try {
128
            fos = new FileOutputStream("peer"+getID()+"/database/dbs.data");
129
        } catch (FileNotFoundException e) {
130
            e.printStackTrace();
131
            System.out.println("Database does not exist!");
132
            createDB();
133
            System.out.println("New DB created and saved to disk...");
134
        }
135
        ObjectOutputStream oos = null;
136
        try {
137
            oos = new ObjectOutputStream(fos);
138
        } catch (IOException e) {
139
            e.printStackTrace();
140
        }
141
        try {
142
            assert oos != null;
143
            oos.writeObject(db);
144
        } catch (IOException e) {
145
            e.printStackTrace();
146
        }
147
        try {
148
            oos.close();
149
        } catch (IOException e) {
150
            e.printStackTrace();
151
        }
152
153
    }
154
155
    private static void createDB() {
156
        db = new Database();
157
        saveDBToDisk();
158
    }
159
160
    private static void loadDatabase() {
161
        System.out.println("Loading database...");
162
        try {
163
            FileInputStream fileInputStream = new FileInputStream("peer"+getID()+"/database/dbs.data");
164
165
            ObjectInputStream objectInputStream = new ObjectInputStream(
166
                    fileInputStream);
167
            db = (Database) objectInputStream.readObject();
168
            objectInputStream.close();
169
        } catch (FileNotFoundException e) {
170
            System.out.println("Database not found");
171
172
            createDB();
173
        } catch (IOException | ClassNotFoundException e) {
174
            e.printStackTrace();
175
        }
176
    }
177
178
179
    private static boolean parseArgs(String[] args) {
180
181
        //args == 6 -> INITIALIZE RMI
182
        if(args.length == 6) {
183
184
            protocolVersion = Float.parseFloat(args[0]);
185
            ID = Integer.parseInt(args[1]);
186
            rmiRemoteObject = args[2];
187
            try {
188
189
                MCAddress = InetAddress.getByName(args[3].split(":")[0]);
190
                MDBAddress = InetAddress.getByName(args[4].split(":")[0]);
191
                MDRAddress = InetAddress.getByName(args[5].split(":")[0]);
192
193
                MCPort = Integer.parseInt(args[3].split(":")[1]);
194
                MDBPort = Integer.parseInt(args[4].split(":")[1]);
195
                MDRPort = Integer.parseInt(args[5].split(":")[1]);
196
197
198
199
            }
200
            catch (UnknownHostException e){
201
                System.out.println("Address not found");
202
                return false;
203
            }
204
205
            System.out.println("\t LAUNCHING RMI");
206
            launchRMI();
207
208
        } else if (args.length == 5) {//normal peer
209
210
            protocolVersion = Float.parseFloat(args[0]);
211
            ID = Integer.parseInt(args[1]);
212
213
            try {
214
215
                MCAddress = InetAddress.getByName(args[2].split(":")[0]);
216
                MDBAddress = InetAddress.getByName(args[3].split(":")[0]);
217
                MDRAddress = InetAddress.getByName(args[4].split(":")[0]);
218
219
                MCPort = Integer.parseInt(args[2].split(":")[1]);
220
                MDBPort = Integer.parseInt(args[3].split(":")[1]);
221
                MDRPort = Integer.parseInt(args[4].split(":")[1]);
222
223
224
225
            }
226
            catch (UnknownHostException e){
227
                System.out.println("Address not found");
228
                return false;
229
            }
230
231
232
        }
233
        else return false;
234
235
236
        return true;
237
    }
238
239
    private static void launchRMI() {
240
241
        //System.setProperty("java.rmi.server.hostname", "localhost");
242
243
        //System.setProperty("rmi.server.codebase", "file:/Users/zemiguel/IdeaProjects/SDIS-P1/src/main/java/service/bin/");
244
        try {
245
246
247
            RMI peer = new Peer();
248
249
            RMI stub = (RMI) UnicastRemoteObject.exportObject(peer, 0);
250
251
            Registry registry = LocateRegistry.getRegistry();
252
            registry.rebind("obj", stub);
253
254
        } catch (Exception e) {
255
            System.err.println("Server exception: " + e.toString());
256
            e.printStackTrace();
257
        }
258
259
260
261
    }
262
263
264
265
    public static Database getDb() {
266
        return db;
267
268
    }
269
270
    public static Listener getMCListener() {
271
        return MCChannel;
272
    }
273
274
    public static Listener getMDBListener() {
275
        return MDBChannel;
276
    }
277
278
    public static Listener getMDRListener() {
279
        return MDRChannel;
280
    }
281
282
    public static void saveDisk(){
283
        FileOutputStream fos = null;
284
        File dir = new File("peer"+getID()+"/disk/");
285
286
        if(!dir.exists()){
287
            System.out.println("creating directory: " + dir.getName());
288
289
            try{
290
                dir.mkdirs();
291
            }
292
            catch(SecurityException se){
293
                se.printStackTrace();
294
            }
295
296
        }
297
298
        try {
299
            fos = new FileOutputStream("peer"+getID()+"/disk/disk.data");
300
        } catch (FileNotFoundException e) {
301
            e.printStackTrace();
302
            System.out.println("Disk does not exist!");
303
            createDisk();
304
            System.out.println("New Disk created and saved to disk...");
305
        }
306
        ObjectOutputStream oos = null;
307
        try {
308
            oos = new ObjectOutputStream(fos);
309
        } catch (IOException e) {
310
            e.printStackTrace();
311
        }
312
        try {
313
            oos.writeObject(disk);
314
        } catch (IOException e) {
315
            e.printStackTrace();
316
        }
317
        try {
318
            oos.close();
319
        } catch (IOException e) {
320
            e.printStackTrace();
321
        }
322
    }
323
324
    private static void loadDisk() throws ClassNotFoundException, IOException {
325
326
        System.out.println("Loading disk...");
327
328
        try {
329
            FileInputStream fileInputStream = new FileInputStream("peer"+getID()+"/disk/disk.data");
330
331
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
332
            disk = (Disk) objectInputStream.readObject();
333
            objectInputStream.close();
334
        } catch (FileNotFoundException e) {
335
            System.out.println("Disk not found");
336
            createDisk();
337
        } catch (IOException | ClassNotFoundException e) {
338
            e.printStackTrace();
339
        }
340
    }
341
342
    public static void createDisk(){
343
        disk = new Disk();
344
        saveDisk();
345
    }
346
347
    @Override
348
    public void backup(File file, int replicationDegree) throws RemoteException {
349
350
        Thread t = new Thread(new Backup(file, replicationDegree));
351
        t.start();
352
353
        try {
354
            t.join();
355
        } catch (InterruptedException e) {
356
            e.printStackTrace();
357
        }
358
359
360
    }
361
362
    @Override
363
    public void delete(String filePath) throws RemoteException {
364
        Thread t = new Thread(new Delete(new FileID(filePath, 0)));
365
366
        t.start();
367
368
        try {
369
            t.join();
370
        } catch (InterruptedException e){
371
            e.printStackTrace();
372
        }
373
374
    }
375
376
    @Override
377
    public void restore(File file) throws RemoteException {
378
379
        Thread t = new Thread(new Restore(file));
380
        t.start();
381
382
        try {
383
            t.join();
384
        } catch (InterruptedException e) {
385
            e.printStackTrace();
386
        }
387
388
389
    }
390
391
    @Override
392
    public String state() throws RemoteException {
393
394
        db.printDatabase();
395
        disk.printDisk();
396
        return null;
397
    }
398
399
    @Override
400
    public void reclaim(int amount) throws RemoteException {
401
402
        Thread t = new Thread(new Reclaim(amount));
403
        t.start();
404
405
        try {
406
            t.join();
407
        } catch (InterruptedException e) {
408
            e.printStackTrace();
409
        }
410
411
412
413
    }
414
415
416
417
418
    public static int getID() {
419
        return ID;
420
    }
421
422
    public static InetAddress getAddress() {
423
        return ip;
424
    }
425
426
    public static float getProtocolVersion() {
427
        return protocolVersion;
428
    }
429
430
    public static Disk getDisk(){ return disk; }
431
432
433
}