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
84
85
86
87
88
89
90
91
92
93
94
95
package ufrn.imd.controller.impl;
import ufrn.imd.controller.Controller;
import ufrn.imd.domain.Client;
import ufrn.imd.service.BalanceService;
import ufrn.imd.service.impl.BalanceServiceImpl;
import ufrn.imd.utils.Log;
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 java.util.Optional;
import static ufrn.imd.utils.NameSpace.HOST_BALANCE_SERVICE;
import static ufrn.imd.utils.NameSpace.PORT_BALANCE_SERVICE;
public class BalanceControllerImpl extends UnicastRemoteObject implements Controller {
private volatile List<Client> clients = new ArrayList<>();
public BalanceControllerImpl(BalanceService service) throws RemoteException, MalformedURLException, AlreadyBoundException {
super();
Log.info(BalanceServiceImpl.class, "Starting Deposit service!");
new Notify(service).start();
Log.info(BalanceServiceImpl.class,
String.format("Initializing server in %s", HOST_BALANCE_SERVICE));
LocateRegistry.createRegistry(PORT_BALANCE_SERVICE);
Naming.bind(HOST_BALANCE_SERVICE, this);
}
@Override
public void registerClient(Client client) throws RemoteException {
clients.add(client);
Log.info(BalanceService.class, "new client registred");
Log.info(BalanceService.class, String.format("total users: %d", clients.size()));
}
private class Notify extends Thread{
private final BalanceService service;
public Notify(BalanceService service) {
super();
this.service = service;
}
public void run() {
for(;;) {
if(clients.size() > 0) {
Log.info(
BalanceService.class,
"notyfing the clients!"
);
int i = 0;
for (Client client : clients) {
try {
this.service.balance(Optional.of(client.getAccount()));
} catch (RemoteException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
try {
Thread.sleep(15 * 1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Log.info(BalanceService.class, "No there's clients!");
}
}
}
}
}