-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
184 lines (154 loc) · 5.64 KB
/
Client.java
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.RemoteServer;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
public class Client extends UnicastRemoteObject implements Client_itf {
private static final int RMI_REGISTRY_PORT = 50051;
private static final String RMI_REGISTRY_HOSTNAME = "localhost";
private static HashMap<Integer, SharedObject> sharedObjects;
private static Set<Client_itf> clientsParticipants;
//private static HashMap<Integer, AtomicInteger> versions; // Ajout d'une map de correspondance entre les shared
// objects et leurs versions. Util??
private static Client instanceClient;
private static Server_itf server;
private static String name;
static Moniteur monitor;
protected Client() throws RemoteException {
super();
}
public static String getIdSite() {
return name;
}
public static void write(Integer id, Object o) throws RemoteException {
// Ecrire au serveur et attendre acknowldgement
// Majorité ou touts les clients
//if (server.isWriter(instanceClient))
server.write(id, o);
}
/*
* reads the shared object of id id
*
* public static Object read(Integer id) {
* SharedObject so = sharedObjects.get(id);
*
* so.read();
*
* try {
* instanceClient.reportValue(id, null); // note: cannot call the method
* reportValue (logically) because
* // there is no callback
* } catch (Exception e) {
* e.getMessage();
* }
* return sharedObjects.get(id);
* }
*/
@Override
public void initSO(int idObj, Object valeur) throws RemoteException {
SharedObject so = new SharedObject(idObj, valeur);
so.setClients(clientsParticipants);
sharedObjects.put(idObj, so);
//versions.put(idObj, new AtomicInteger(0));
}
/*
* Update : the method reportValue now does the same thing as lookFor but
* updates the shared object at the same time
* Problem : doesn't call the method report value of shared object
*/
@Override
public void reportValue(int idObj, ReadCallback rcb) throws RemoteException {
SharedObject so = sharedObjects.get(idObj);
System.out.println( Integer.parseInt(so.getVersion()) + ", " + so.getObj());
so.reportValue(rcb);
//rcb.call(Integer.parseInt(so.getVersion()), so.getObj());
}
@Override
public void update(int idObj, int version, Object valeur, WriteCallback wcb) throws RemoteException {
SharedObject so = sharedObjects.get(idObj);
so.update(version, valeur, wcb);
}
@Override
public String getSite() throws RemoteException {
return name;
}
@Override
public Object getObj(String name) throws RemoteException {
int id = server.lookup(name);
Object o = sharedObjects.get(id).getObj();
return o;
}
@Override
public Object getObj(int id) throws RemoteException {
Object o = sharedObjects.get(id).getObj();
return o;
}
/* Get la version d'un objet chez le client à partir de son nom. Util?? */
@Override
public int getVersion(String name) throws RemoteException {
int idObj = server.lookup(name);
return Integer.parseInt(sharedObjects.get(idObj).getVersion());
}
@Override
public void setMonitor(Moniteur m) throws RemoteException {
monitor = m;
}
@Override
public int getVersion(Integer idObj) throws RemoteException {
return Integer.parseInt(sharedObjects.get(idObj).getVersion());
}
public static void init(String myName) {
try {
server = (Server_itf) Naming.lookup("rmi://"
+ RMI_REGISTRY_HOSTNAME + ":"
+ RMI_REGISTRY_PORT
+ "/server");
if (server == null)
System.out.println("Server null");
// Initialiser le nom du site
Client.name = myName;
sharedObjects = new HashMap<>();
instanceClient = new Client();
//versions = new HashMap<>();
clientsParticipants = server.addClient(instanceClient);
if ( clientsParticipants == null) System.exit(0);
//monitor = server.getMonitor();
} catch (NotBoundException | MalformedURLException | RemoteException e) {
e.printStackTrace();
}
}
public static SharedObject publish(String string, String obj, boolean b) {
int id = 0;
try {
id = server.publish(string, obj, b);
} catch (RemoteException e) {
throw new RuntimeException(e);
}
System.out.println("client::publish : " + id);
return sharedObjects.get(id);
}
public static SharedObject lookup(String objname) {
try {
final int objectReference = server.lookup(objname);
if (objectReference == -1)
return null;
SharedObject obj = sharedObjects.values()
.stream()
.filter(e -> e.getId() == objectReference)
.findFirst().orElse(null);
if (obj == null) {
// add the shared object to the list
obj = new SharedObject(objectReference);
sharedObjects.put(objectReference, obj);
}
return obj;
} catch (RemoteException e) {
e.printStackTrace();
return null;
}
}
}