Project

General

Profile

Revision 2

Updated some classes

View differences:

TestApp.java
12 12
public class TestApp implements Runnable {
13 13

  
14 14
	private InitiatorPeer initiatorPeer;
15
	
16
    private String peer_ap;
17
    private String sub_protocol;
18
    private String opnd_1;
19
    private String opnd_2;
20 15

  
21
    private Map<String, Runnable> protocols;
22
    
16
	private String peer_ap;
17
	private String sub_protocol;
18
	private String opnd_1;
19
	private String opnd_2;
23 20

  
24
    public static void main(String[] args) {
25
        if (args.length < 2 || args.length > 4) {
26
        	System.out.println("Usage: <java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2> >");
21
	private Map<String, Runnable> protocols;
22

  
23

  
24
	public static void main(String[] args) {
25
		if (args.length < 2 || args.length > 4) {
26
			System.out.println("Usage: <java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2> >");
27 27
			System.out.println("<peer_ap> - peer access point");
28 28
			System.out.println("<sub_protocol> - BACKUP, RESTORE, DELETE, RECLAIM");
29 29
			System.out.println("<opnd_1> - path name of the file to backup/restore/delete");
30 30
			System.out.println("<opnd_2> - replication degree");
31 31
			System.exit(1);
32
        }
33
        
34
        String peer_ap = args[0];
35
        String sub_protocol = args[1];
36
        String opnd_1;
37
        String opnd_2;
38
        
39
        if(args.length > 2) 
40
        	opnd_1 = args[2];
41
        else
42
        	opnd_1 = null;
43
        
44
        if(args.length > 3) 
45
        	opnd_2 = args[3];
46
        else
47
        	opnd_2 = null;
32
		}
48 33

  
49
        TestApp app = new TestApp(peer_ap, sub_protocol, opnd_1, opnd_2);
50
        new Thread(app).start();
51
    }
34
		String peer_ap = args[0];
35
		String sub_protocol = args[1];
36
		String opnd_1;
37
		String opnd_2;
52 38

  
53
    public TestApp(String peer_ap, String sub_protocol, String opnd_1, String opnd_2) {
54
    	protocols = new HashMap<>();
55
    	
56
        this.peer_ap = peer_ap;
57
        this.sub_protocol = sub_protocol;
58
        this.opnd_1 = opnd_1;
59
        this.opnd_2 = opnd_2;
39
		if(args.length > 2) 
40
			opnd_1 = args[2];
41
		else
42
			opnd_1 = null;
60 43

  
61
        protocols.put("BACKUP", this::handleBackup);
62
        protocols.put("DELETE", this::handleDelete);
44
		if(args.length > 3) 
45
			opnd_2 = args[3];
46
		else
47
			opnd_2 = null;
63 48

  
64
    }
49
		TestApp app = new TestApp(peer_ap, sub_protocol, opnd_1, opnd_2);
50
		new Thread(app).start();
51
	}
65 52

  
66
    @Override
67
    public void run() {
68
    	try {
69
            Registry registry = LocateRegistry.getRegistry(null);
70
            initiatorPeer = (InitiatorPeer) registry.lookup(peer_ap);
71
        } catch (Exception e) {
72
        	System.out.println("Error when opening RMI stub");
73
            e.printStackTrace();
74
        }
75
        protocols.get(sub_protocol).run();
76
    }
77
    
78
    private void handleBackup() {
79
        File file = new File(this.opnd_1);
80
        System.out.println("--> BACKUP :: Saving chunks at " + file.getAbsolutePath() + "\"");
81
        try {
82
            initiatorPeer.backup(file, Integer.parseInt(this.opnd_2));
83
        } catch (RemoteException e) {
84
        	e.printStackTrace();
85
        }
86
    }
53
	public TestApp(String peer_ap, String sub_protocol, String opnd_1, String opnd_2) {
54
		protocols = new HashMap<>();
87 55

  
88
    private void handleDelete() {
89
    	System.out.println("--> DELETE :: delete file: " + opnd_1 + "\"");
90
        try {
91
            initiatorPeer.delete(this.opnd_1);
92
        } catch (RemoteException e) {
93
        	e.printStackTrace();
94
        }
95
    }
56
		this.peer_ap = peer_ap;
57
		this.sub_protocol = sub_protocol;
58
		this.opnd_1 = opnd_1;
59
		this.opnd_2 = opnd_2;
60

  
61
		protocols.put("BACKUP", () -> {
62
			try {
63
				backup();
64
			} catch (NumberFormatException e) {
65
				e.printStackTrace();
66
			} catch (RemoteException e) {
67
				e.printStackTrace();
68
			}
69
		});
70
		protocols.put("DELETE", () -> {
71
			try {
72
				delete();
73
			} catch (RemoteException e) {
74
				e.printStackTrace();
75
			}
76
		});
77

  
78
	}
79

  
80
	@Override
81
	public void run() {
82
		try {
83
			Registry registry = LocateRegistry.getRegistry(null);
84
			initiatorPeer = (InitiatorPeer) registry.lookup(peer_ap);
85
		} catch (Exception e) {
86
			e.printStackTrace();
87
		}
88
		protocols.get(sub_protocol).run();
89
	}
90

  
91
	private void backup() throws NumberFormatException, RemoteException {
92
		File file = new File(this.opnd_1);
93
		System.out.println("--> BACKUP :: Saving chunks at " + file.getAbsolutePath() + "\"");
94
		initiatorPeer.backup(file, Integer.parseInt(this.opnd_2));
95

  
96
	}
97

  
98
	private void delete() throws RemoteException {
99
		System.out.println("--> DELETE :: delete file: " + opnd_1 + "\"");
100
		initiatorPeer.delete(this.opnd_1);
101

  
102
	}
96 103
}

Also available in: Unified diff