-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIIOPClientImpl.java
238 lines (163 loc) · 4.69 KB
/
IIOPClientImpl.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
package server;
import java.io.IOException;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.security.SecureRandom;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
/**
* Classe che implementa il codice del client. Viene spedita al client al
* momento del login.
*
* @author Pietro Musoni
* @author Carlo Tacchella
*/
public class IIOPClientImpl implements IIOPClient {
/**
*
*/
private static final long serialVersionUID = 1L;
private String myAddress;
//private String path = "/tmp/";
//private static final long serialVersionUID = -5477996558315185651L;
private MainServerIIOP mainServIIOP;
public IIOPClient myStub;
public static BigInteger[] ArrayPrimi = new BigInteger[2];
public IIOPClientImpl(MainServerIIOP ms) throws RemoteException,
ClassNotFoundException {
mainServIIOP = ms;
}
public void act() throws RemoteException {
myStub = (IIOPClient) UnicastRemoteObject.exportObject(this,36000);
try {
myAddress = InetAddress.getLocalHost().getHostAddress();
//register();
finestra_opzioni();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
/*private void register() throws RemoteException {
try {
//MarshalledObject mo = new MarshalledObject(myStub);
//mainServIIOP.addClient(myAddress, mo);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}*/
public String getAddress() throws RemoteException {
return myAddress;
}
public BigInteger getKey() throws RemoteException {
long startTime = System.currentTimeMillis();
BigInteger n= getRandomBigInteger(2048);
boolean check=true;
while(check){
if(!isPrime(n)){
n=n.add(BigInteger.ONE);
//System.out.println(n);
}
else
check=false;
}
long endTime = System.currentTimeMillis();
System.out.println("ho calcolato la chiave in " + (endTime - startTime)/1000 +" s.");
return n;
}
private static BigInteger getRandomBigInteger( int bitLength){
SecureRandom rnd = new SecureRandom();
BigInteger randomNum=BigInteger.ZERO;
BigInteger tanto= BigInteger.valueOf(10).pow(100);
//finchè il numero random è inferiore a 1000 ricalcolo
while(randomNum.compareTo(tanto)==-1){
randomNum=new BigInteger(bitLength, rnd);
}
return randomNum;
}
//passiamo al client JRMP i due numeri primi (biginteger)
public BigInteger[] getArrayPrimi() throws RemoteException{
try{
ArrayPrimi[0].compareTo(BigInteger.ONE);
return ArrayPrimi;
}catch(NullPointerException e){
return null;
}
}
//dato un numero ritorno true se è primo
static boolean isPrime( BigInteger n){
return n.isProbablePrime(100);
}
public void finestra_opzioni() throws RemoteException {
//String serverIP = System.getenv("SERVERIP");
//String codebase = "http://" + serverIP + ":8000/";
String scelta;
int s = 0;
while (s != 3) {
scelta = (String) JOptionPane.showInputDialog(null,
"MENU:\n"
+ "1) Dai disponibilità di calcolo\n"
+ "2) Stampa lista Client\n"
+ "3) Log out", "Menu Admin",
JOptionPane.INFORMATION_MESSAGE, new ImageIcon(),
null, null);
try {
s = Integer.parseInt(scelta);
} catch (NumberFormatException nfe) {
s = 4;
}
switch (s) {
case 1: {
int cont=0;
while (cont==0){
try{
ArrayPrimi[0]= getKey();
ArrayPrimi[1]= getKey();
try {
mainServIIOP.scriviChiave(ArrayPrimi);
} catch (IOException e) {
e.printStackTrace();
}
//se esegue questo codice non ci sono buchi nell'array
System.out.println("L'array è pieno");
cont=1;
try{
Thread.currentThread();
Thread.sleep(2000);
}catch(InterruptedException ie){
}
}catch(NullPointerException e){
System.out.println("è uguale a null");
cont=0;
ArrayPrimi[0]= getKey();
ArrayPrimi[1]= getKey();
}
}
/*while(mainServIIOP.counter < 2) {
mainServIIOP.Key[mainServIIOP.counter] = getKey();
mainServIIOP.counter++;
}*/
System.exit(0);
break;
}
case 2: {
break;
}
case 3: {
System.exit(0);
break;
}
default: {
scelta = null;
JOptionPane.showMessageDialog(null,
"Scelta non valida", "Errore",
JOptionPane.ERROR_MESSAGE, new ImageIcon());
}
}
}
}
}