-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImplPolyEbay.java
335 lines (299 loc) · 13.9 KB
/
ImplPolyEbay.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* ImplPolyEbay.java
*
* Created on 9 vril 2007
*
* @author BRUN Joel & DEBONNEL Yann
*/
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Timer;
import java.util.Vector;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ImplPolyEbay extends UnicastRemoteObject implements InterfacePolyEbay, Runnable {
private Vector<Article> articles;
private Vector<String> clients;
private Hashtable<String, String> clientsArticle; // ip article
private Hashtable<String, String> clientsIp; // ip client
private Hashtable<String, InterfaceClient> remoteClients; // ip remote
private InterfacePolyPaypal remotePolyPaypal;
private boolean IsPolyPayPal = false;
private Timer timer;
private Thread t_listener;
private String month[] = {"janvier","fevrier","mars","avril","mai",
"juin","juillet","aout","septembre","octobre","novembre","decembre"};
private boolean isThreadActive;
public ImplPolyEbay() throws RemoteException{
super();
articles = new Vector<Article>();
clientsArticle = new Hashtable<String, String>();
clientsIp = new Hashtable<String, String>();
remoteClients = new Hashtable<String, InterfaceClient>();
clients = new Vector<String>();
isThreadActive = false;
}
public synchronized ClientConnect connectClient(String nom, String ipClient) throws RemoteException {
ClientConnect temp = null;
//if(clientsIp.containsKey(ipClient)==true || remotePolyPaypal.connect(nom)==-1)
if(remotePolyPaypal.connect(nom)==-1)
temp = new ClientConnect(false);
else{
InterfaceClient rC;
try {
rC = (InterfaceClient) Naming.lookup("//" + ipClient + "/" + nom);
remoteClients.put(ipClient, rC);
System.out.println("Connection du client "+nom+ " @ " + ipClient);
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
}
temp = new ClientConnect(true);
if(clientsIp.containsValue(nom)==true){
Vector<String> ip = new Vector<String>();
for( Enumeration e = clientsIp.keys(); e.hasMoreElements()==true;){
String unIp = (String) e.nextElement();
if(clientsIp.get(unIp).compareTo(nom)==0){
ip.add(unIp);
}
}
for(int i=0; i<articles.size() && clientsArticle.isEmpty()==false; i++){
boolean isUse=false;
Article UnArt = articles.elementAt(i);
for(int j=0; j<ip.size() && isUse==false; j++){
if(UnArt.getNom().compareTo(clientsArticle.get(ip.elementAt(j)))==0)
isUse=true;
}
if(isUse==false) temp.addArticle(UnArt);
}
} else{
for(int i=0; i<articles.size(); i++){
temp.addArticle(articles.elementAt(i));
}
}
clientsIp.put(ipClient, nom);
if(!clients.contains(nom)) clients.add(nom);
}
return temp;
}
public synchronized boolean disconnectClient(String nom, String ipClient) throws RemoteException {
if(clientsIp.get(ipClient).compareTo(nom)==0){
clientsArticle.remove(ipClient);
clientsIp.remove(ipClient);
remoteClients.remove(ipClient);
return true;
} else return false;
}
public synchronized boolean addClientArticle(String ipClient, String article) throws RemoteException {
if(clientsIp.containsKey(ipClient) && !clientsArticle.containsKey(ipClient)){
clientsArticle.put(ipClient, article);
return true;
} else return false;
}
public synchronized void miserArticle(String ipClient, String article, float montant) throws RemoteException {
boolean isArt = false;
Article unArt ;
if(clientsArticle.get(ipClient).compareTo(article)==0){System.out.println(1);
for(Iterator ite = articles.iterator(); ite.hasNext() && !isArt ; ){System.out.println(2);
unArt = (Article) ite.next();System.out.println(2);
if(unArt.getNom().compareTo(article)==0){System.out.println(3);
isArt = true;System.out.println(4);
if(unArt.addMise(clientsIp.get(ipClient),montant)==true){System.out.println(5);
System.out.println("mise effectuer avec succes: "+article+" "+ montant);System.out.println(6);
for(Enumeration e = remoteClients.keys(); e.hasMoreElements(); ){System.out.println(7);
String unIp = (String) e.nextElement();System.out.println(8);
if(clientsArticle.get(unIp).compareTo(article)==0){System.out.println(9);
remoteClients.get(unIp).UpdateClient(unArt);System.out.println(0);
}
}
} else
System.out.println("mise erreur : "+article+" "+ montant);
}
}
printInfo();
}
}
public boolean addArticle(String nom, double prix, String time_fin){
boolean isAdded=false;
for(Iterator ite = articles.iterator(); ite.hasNext() && isAdded==true;){
Article unArt = (Article) ite.next();
if( unArt.getNom().compareTo(nom)==0)
isAdded=true;
}
if(isAdded==false){
Article unArt = new Article(nom, prix, time_fin);
articles.add(unArt);
if(!isThreadActive){ isThreadActive=true; t_listener=new Thread(this); t_listener.start();}
}
/*////////////////////// test
System.out.println("--------------------------");
System.out.println("articles ajoutes : ");
for(Iterator ite = articles.iterator(); ite.hasNext();){
Article unArt = (Article) ite.next();
System.out.println(unArt.getNom());
}
System.out.println("--------------------------");
//////////////////// fin test*/
//printInfo();
return !isAdded;
}
public void printInfo(){
String connected = "";
if(!clients.isEmpty())
connected = clients.toString();
int nb = clients.size();
GregorianCalendar today = new GregorianCalendar();
String stToday = today.get(Calendar.DAY_OF_MONTH)+" "+
month[today.get(Calendar.MONTH)]+" "+
today.get(Calendar.YEAR)+", "+today.get(Calendar.HOUR_OF_DAY)+":"+
today.get(Calendar.MINUTE)+":"+today.get(Calendar.SECOND);
System.out.println(" -------------------------------------------------------------");
System.out.println("| Serveur PolyEbay, "+stToday+" |");
System.out.println(" -------------------------------------------------------------");
if(IsPolyPayPal) System.out.println("|connexions: PolyPayPal, "+connected+" |");
else System.out.println("|connexions: "+connected+" |");
System.out.println("| nombre de clients connectes: "+nb+" |");
System.out.println("| nom(s): "+connected+" |");
for(int i=0; i<articles.size();i++){
Article unArt = articles.elementAt(i);
System.out.println(" -------------------------------------------------------------");
System.out.println("| Article "+(i+1)+": "+unArt.getNom()+" prix "+unArt.getPrix()
+", fermeture a "+unArt.getDateFin()+" |");
unArt.print();
}
System.out.println("| |");
System.out.println(" -------------------------------------------------------------");
}
public void arreterServeur(){
try {
Naming.unbind("//localhost:4500/POLYEBAY");
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
}
}
public void demarrerServeur(String IP_PAYPAL) {
try {
remotePolyPaypal = (InterfacePolyPaypal)Naming.lookup("//" + IP_PAYPAL + ":5000/POLYPAYPAL");
System.out.println("Connection au serveur PAYPAL etablie.");
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (NotBoundException ex) {
ex.printStackTrace();
}
try{
java.rmi.registry.LocateRegistry.createRegistry(4500);
System.out.println("Registre cree sur le port 4500");
} catch(Exception e) {
e.printStackTrace();
}
try {
java.rmi.registry.Registry reg = java.rmi.registry.LocateRegistry.getRegistry(4500);
System.out.println("Registre utilise sur le port 4500");
} catch (Exception e) {
System.out.println("Error: " + e);
e.printStackTrace();
}
// Créer et installer le gestionnaire de sécurité.
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
Naming.rebind("rmi://" + "localhost:4500/POLYEBAY", this);
System.out.println("Serveur PolyEBAY fonctionnel @ localhost : 4500");
} catch (RemoteException e1) {
e1.printStackTrace();
System.out.println("Systeme de partage de fichier n'est pas disponible en ce moment");
System.out.println("Veuillez reessayer la connection ulterieurement");
System.exit(0);
} catch (MalformedURLException e) {
e.printStackTrace();
System.out.println("Systeme de partage de fichier n'est pas disponible en ce moment");
System.out.println("Veuillez reessayer la connection ulterieurement");
System.exit(0);
}
}
public static void main(String args[]) throws RemoteException{
ImplPolyEbay polyEbay;
polyEbay = new ImplPolyEbay();
polyEbay.demarrerServeur(args[0]);
polyEbay.addArticle("livre java 1", 09.00, "01:32:00");
polyEbay.addArticle("livre c++ 1", 12.99, "01:35:00");
polyEbay.addArticle("livre philo 1", 04.99, "01:30:00");
polyEbay.addArticle("livre qwer", 5.00, "01:33:00");
polyEbay.addArticle("livre resta", 4.30, "01:35:00");
InputStreamReader in = new InputStreamReader(System.in);
String In;
char[] input = new char[20];
while(true){
System.out.println("\n-----------------------------------------------");
System.out.println("Pour arreter le systeme appuyer sur : q ou Q\n");
try {
int size = in.read(input, 0, 20);
In = String.valueOf(input);
In= In.substring(0,1);
if(In.equalsIgnoreCase("q") == true){
//polyEbay.arreterServeur();
System.exit(0);
}
} catch (IOException ex) {
ex.printStackTrace();
}
//try {
polyEbay.printInfo();
//} catch (InterruptedException ex) {
// ex.printStackTrace();
//}
}
}
public void run() {
while(isThreadActive){
for(int i=0; i<articles.size();i++){
Article art = articles.elementAt(i);
art.setTimeRemaining();
if(art.IsTimeOut()){
if(art.getLeader().compareTo("")!=0){
try {
remotePolyPaypal.updateCredit(art.getLeader(), art.getbestMise());
for(Enumeration e1 = clientsArticle.keys(); e1.hasMoreElements();){
String ip = (String) e1.nextElement();
if(clientsArticle.get(ip).compareTo(art.getNom())==0){
remoteClients.get(ip).UpdateClient(art);
}
} }catch (RemoteException ex) {
ex.printStackTrace();
}
}
articles.remove(i);
i--;
}
}
if(articles.size()==0) {
isThreadActive=false; t_listener = null;
} else{
try {
t_listener.sleep(60000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}
}