-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineUser.java
More file actions
77 lines (63 loc) · 1.91 KB
/
OnlineUser.java
File metadata and controls
77 lines (63 loc) · 1.91 KB
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
package online_management;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.util.GregorianCalendar;
/**
* This class is used to identify an online user, that is a user connected to the server. Every user has a different
* name from the others and, almost certainly, a different HashCode. This code is generated in the login phase, and
* it's used to check if a message sent by a user comes really by him.
*
* @author Noris
* @date 2015/03/30
*/
public class OnlineUser {
private String username;
private InetAddress ipAddress;
private PrintWriter writer;
private GregorianCalendar connectionDate;
private volatile boolean polled;
public OnlineUser(String username, InetAddress ipAddress) {
this.username = username;
this.ipAddress = ipAddress;
connectionDate = new GregorianCalendar();
polled = true;
}
public String getUsername() {
return username;
}
public InetAddress getIpAddress() {
return ipAddress;
}
/**
* @return the date on which the user has logged in
*/
public GregorianCalendar getConnectionDate() {
return connectionDate;
}
/**
* @return the connection time in milliseconds
*/
public long getConnectionTime() {
GregorianCalendar now = new GregorianCalendar();
return now.getTimeInMillis() - connectionDate.getTimeInMillis();
}
/**
* @return a different value for every online user
*/
@Override
public int hashCode() {
return username.hashCode() * ipAddress.hashCode() * connectionDate.hashCode();
}
public void setOutStream(PrintWriter printWriter) {
this.writer = printWriter;
}
public PrintWriter getOutStream() {
return writer;
}
public boolean isPolled() {
return polled;
}
public void setPolled(boolean polled) {
this.polled = polled;
}
}