01: package dalma.endpoints.irc;
02:
03: import java.io.Serializable;
04:
05: /**
06: * An IRC user (and an implicit {@link Session} that represents
07: * a private communication channel between this user.)
08: *
09: * @author Kohsuke Kawaguchi
10: */
11: public class Buddy implements Serializable {
12: /*package*/String name;
13:
14: private final IRCEndPoint endpoint;
15:
16: /**
17: * Non-null if there's an active {@link Session} between this buddy.
18: * Access needs to be synchronized.
19: */
20: private PrivateChat chat;
21:
22: public Buddy(IRCEndPoint endpoint, String name) {
23: this .endpoint = endpoint;
24: this .name = name;
25: }
26:
27: /**
28: * Gets the name of this user.
29: * @return always non-null.
30: */
31: public String getName() {
32: return name;
33: }
34:
35: public boolean isOnline() {
36: // TODO: implement this method later
37: throw new UnsupportedOperationException();
38: // TODO: use ISON command
39: }
40:
41: /**
42: * Starts a new private {@link PrivateChat} with this buddy.
43: *
44: * <p>
45: * If a {@link PrivateChat} session is already in progress with
46: * this buddy, it will be cut off and a new one is created.
47: * The old {@link PrivateChat} object will no longer return
48: * new messages.
49: */
50: public synchronized PrivateChat openChat() {
51: if (chat != null)
52: chat.close();
53: chat = new PrivateChat(endpoint, this );
54: return chat;
55: }
56:
57: synchronized PrivateChat getChat() {
58: return chat;
59: }
60:
61: synchronized void onChatClosed() {
62: chat = null;
63: }
64:
65: private Object writeReplace() {
66: return new Moniker(endpoint, getName());
67: }
68:
69: private static final class Moniker implements Serializable {
70: private final IRCEndPoint endPoint;
71: private final String buddyName;
72:
73: public Moniker(IRCEndPoint endPoint, String buddyName) {
74: this .endPoint = endPoint;
75: this .buddyName = buddyName;
76: }
77:
78: private Object readResolve() {
79: return endPoint.getBuddy(buddyName);
80: }
81:
82: private static final long serialVersionUID = 1L;
83: }
84: }
|