-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
119 lines (91 loc) · 3.9 KB
/
Client.java
File metadata and controls
119 lines (91 loc) · 3.9 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package example.hello;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.io.Serializable;
public class Client implements ClientInterface, Serializable {
private static final long serialVersionUID = 7526472295622776147L;
private String name;
private HashMap<Integer, HashSet<Cell>> objects = new HashMap<Integer, HashSet<Cell>>();
public void receive(Integer key, Cell o) {
HashSet<Cell> cellList = objects.get(key);
if(cellList == null) cellList = new HashSet<Cell>();
cellList.add(o);
objects.put(key, cellList);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HashSet<Cell> getMessages(Integer key) {
return objects.get(key);
}
public static void main(String[] args) {
String name = args[0];
if(args.length == 1) {
try {
ClientInterface cli = new Client();
cli.setName(name);
ClientInterface clientStub = (ClientInterface)UnicastRemoteObject.exportObject(cli, 0);
Registry registry = LocateRegistry.getRegistry();
registry.bind(name, clientStub);
registry = LocateRegistry.getRegistry("");
ServerInterface serverStub = (ServerInterface) registry.lookup("server");
Scanner keyboard = new Scanner(System.in);
int opt = 2;
while(opt != 0) {
System.out.println("[1] Publish");
System.out.println("[2] Subscribe");
System.out.println("[3] Show cells");
System.out.println("[0] Exit");
System.out.print("Option: ");
opt = keyboard.nextInt();
Integer key;
if(opt != 0){
System.out.print("Enter key: ");
key = keyboard.nextInt();
}
switch (opt) {
case 1:
String message;
keyboard.nextLine();
System.out.print("Enter your message: ");
message = keyboard.nextLine();
Cell o = new Cell();
o.set(message);
serverStub.publish(key, o);
break;
case 2:
serverStub.subscribe(key, cli.getName());
break;
case 3:
HashSet<Cell> messagesCells = cli.getMessages(key);
if(messagesCells != null) {
for(Cell c : messagesCells) {
System.out.println(c.get());
}
}
break;
case 0:
System.out.println("Bye!!");
break;
default:
System.out.println("Invalid option.");
break;
}
}
keyboard.close();
} catch (Exception e) {
System.err.println("Client exception: " + e.toString());
e.printStackTrace();
}
} else {
System.out.println("Use the following command structure: 'java -classpath classes -Djava.rmi.server.codebase=file:classes/ example.hello.Client [name]'");
}
}
}