-
Notifications
You must be signed in to change notification settings - Fork 0
/
MudServer.java
129 lines (122 loc) · 4.25 KB
/
MudServer.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
package UP12;
import java.io.FileWriter;
import java.io.IOException;
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.Date;
import java.util.Objects;
import java.util.Optional;
public class MudServer extends UnicastRemoteObject implements Mud.MailServerInterface {
static int PORT = 2020;
static ArrayList<Mud.MailClientInterface> ClientList;
static ArrayList<Integer> clientslogs;
private String userName;
public MudServer(String name) throws RemoteException{
this.userName=name;
ClientList=new ArrayList<>();
clientslogs=new ArrayList<>();
}
public static void main(String[] args) {
try {
MudServer server = new MudServer("Arthur_server");
LocateRegistry.createRegistry(2020);
System.setProperty("java.rmi.server.hostname","192.168.0.20");
Naming.rebind("rmi://" + Mud.mudPrefix + ":" + PORT + "/" + server.userName,server);
Date date = new Date();
System.out.println(date);
System.out.println("Server connecting!");
while(true) {
Thread.sleep(300000);
for(int i=0;i<clientslogs.size();i++) {
clientslogs.set(i, 0);
}
System.out.println("ïðîâåðêà ó ñåðâàêà");
}
} catch (Exception e) {
System.err.println(e.getMessage());
System.out.println("Usage: java FreeTimeServer <username> \n");
System.exit(1);
}
}
@Override
public Mud.MailClientInterface getPerson(String name) throws RemoteException {
Optional<Mud.MailClientInterface> optional = ClientList.stream().filter(p -> {
try {
return Objects.equals(p.getName(), name);
} catch (RemoteException e) {
System.err.println(e.getMessage());
}
return false;
}).findFirst();
System.out.println("Persons are:");
for (Mud.MailClientInterface person : ClientList) {
System.out.println(person.getName());
}
System.out.println("\n");
return optional.orElse(null) ;
}
@Override
public void delClient(Mud.MailClientInterface current) throws RemoteException{
if(ClientList.indexOf(current)>=0) {
clientslogs.set(ClientList.indexOf(current), 1);
}
ClientList.remove(current);
}
@Override
public String getPersons() throws RemoteException{
String res=("Clients:\n");
for(Mud.MailClientInterface i:ClientList){
res+=i.getName()+"\n";
}
return res;
}
@Override
public boolean addPerson(Mud.MailClientInterface current) throws RemoteException {
if(ClientList.indexOf(current)>=0) {
clientslogs.set(ClientList.indexOf(current), 1);
}
Optional<Mud.MailClientInterface> optional = ClientList.stream().filter(p -> {
try {
return Objects.equals(p.getName(), current.getName());
} catch (RemoteException e) {
System.err.println(e.getMessage());
}
return false ;
}).findFirst();
if (!optional.isPresent()) {
ClientList.add(current);
return true;
}
return false;
}
@Override
public boolean printPerson(Mud.MailClientInterface current) throws RemoteException {
if(ClientList.indexOf(current)>=0) {
clientslogs.set(ClientList.indexOf(current), 1);
}
try {
FileWriter nFile = new FileWriter("DATA.txt");
Date date = new Date();
nFile.append(current.getName().toString()+" "+date.toString()+"\n");
nFile.flush();
nFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
return true;
}
public boolean getStatus(Mud.MailClientInterface current) throws RemoteException {
if(clientslogs.get(ClientList.indexOf(current))==0) {
return false;
}
return true;
}
@Override
public void sendMessage(Mud.MailClientInterface name, String message) throws RemoteException {
}
}