Newer
Older
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
package ufrn.imd.controller.impl;
import ufrn.imd.controller.Controller;
import ufrn.imd.log.Log;
import ufrn.imd.message.Client;
import ufrn.imd.service.DepositService;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;
import static ufrn.imd.nameSpace.NameSpace.HOST_DEPOSIT_SERVICE;
import static ufrn.imd.nameSpace.NameSpace.PORT_DEPOSIT_SERVICE;
public class DepositControllerImpl extends UnicastRemoteObject implements Controller {
private volatile List<Client> clients = new ArrayList<>();
public DepositControllerImpl(DepositService service) throws RemoteException, MalformedURLException, AlreadyBoundException {
super();
Log.info(DepositControllerImpl.class, "Starting Deposit service!");
new Notify(service).start();
Log.info(DepositControllerImpl.class,
String.format("Initializing server in %s", HOST_DEPOSIT_SERVICE));
LocateRegistry.createRegistry(PORT_DEPOSIT_SERVICE);
Naming.bind(HOST_DEPOSIT_SERVICE, this);
}
@Override
public void registerClient(Client client) throws RemoteException {
clients.add(client);
Log.info(DepositControllerImpl.class, "new client registred");
Log.info(DepositControllerImpl.class, String.format("total users: %d", clients.size()));
}
private class Notify extends Thread{
private final DepositService service;
public Notify(DepositService service) {
super();
this.service = service;
}
public void run() {
for(;;) {
if(clients.size() > 0) {
Log.info(
DepositControllerImpl.class,
"notyfing the clients!"
);
int i = 0;
for (Client client : clients) {
try {
this.service.deposit(client.getBalance(), client.getAccount());
} catch (RemoteException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}