|
1 /* |
|
2 * otr4j, the open source java otr librar |
|
3 * |
|
4 * Distributable under LGPL license. |
|
5 * See terms of license at gnu.org. |
|
6 */ |
|
7 |
|
8 package net.java.otr4j; |
|
9 |
|
10 import java.security.PublicKey; |
|
11 import java.util.Hashtable; |
|
12 import java.util.List; |
|
13 import java.util.Map; |
|
14 import java.util.Vector; |
|
15 |
|
16 import net.java.otr4j.session.Session; |
|
17 import net.java.otr4j.session.SessionID; |
|
18 import net.java.otr4j.session.SessionImpl; |
|
19 import net.java.otr4j.session.SessionStatus; |
|
20 |
|
21 /** |
|
22 * |
|
23 * @author George Politis |
|
24 * |
|
25 */ |
|
26 public class OtrEngineImpl implements OtrEngine { |
|
27 |
|
28 public OtrEngineImpl(OtrEngineHost host) { |
|
29 if (host == null) |
|
30 throw new IllegalArgumentException("OtrEgineHost is required."); |
|
31 |
|
32 this.setHost(host); |
|
33 } |
|
34 |
|
35 private OtrEngineHost host; |
|
36 private Map<SessionID, Session> sessions; |
|
37 |
|
38 private Session getSession(SessionID sessionID) { |
|
39 |
|
40 if (sessionID == null || sessionID.equals(SessionID.Empty)) |
|
41 throw new IllegalArgumentException(); |
|
42 |
|
43 if (sessions == null) |
|
44 sessions = new Hashtable<SessionID, Session>(); |
|
45 |
|
46 if (!sessions.containsKey(sessionID)) { |
|
47 Session session = new SessionImpl(sessionID, getHost()); |
|
48 sessions.put(sessionID, session); |
|
49 |
|
50 session.addOtrEngineListener(new OtrEngineListener() { |
|
51 |
|
52 public void sessionStatusChanged(SessionID sessionID) { |
|
53 for (OtrEngineListener l : listeners) |
|
54 l.sessionStatusChanged(sessionID); |
|
55 } |
|
56 }); |
|
57 return session; |
|
58 } else |
|
59 return sessions.get(sessionID); |
|
60 } |
|
61 |
|
62 public SessionStatus getSessionStatus(SessionID sessionID) { |
|
63 return this.getSession(sessionID).getSessionStatus(); |
|
64 } |
|
65 |
|
66 public String transformReceiving(SessionID sessionID, String msgText) |
|
67 throws OtrException { |
|
68 return this.getSession(sessionID).transformReceiving(msgText); |
|
69 } |
|
70 |
|
71 public String transformSending(SessionID sessionID, String msgText) |
|
72 throws OtrException { |
|
73 return this.getSession(sessionID).transformSending(msgText, null); |
|
74 } |
|
75 |
|
76 public void endSession(SessionID sessionID) throws OtrException { |
|
77 this.getSession(sessionID).endSession(); |
|
78 } |
|
79 |
|
80 public void startSession(SessionID sessionID) throws OtrException { |
|
81 this.getSession(sessionID).startSession(); |
|
82 } |
|
83 |
|
84 private void setHost(OtrEngineHost host) { |
|
85 this.host = host; |
|
86 } |
|
87 |
|
88 private OtrEngineHost getHost() { |
|
89 return host; |
|
90 } |
|
91 |
|
92 public void refreshSession(SessionID sessionID) throws OtrException { |
|
93 this.getSession(sessionID).refreshSession(); |
|
94 } |
|
95 |
|
96 public PublicKey getRemotePublicKey(SessionID sessionID) { |
|
97 return this.getSession(sessionID).getRemotePublicKey(); |
|
98 } |
|
99 |
|
100 private List<OtrEngineListener> listeners = new Vector<OtrEngineListener>(); |
|
101 |
|
102 public void addOtrEngineListener(OtrEngineListener l) { |
|
103 synchronized (listeners) { |
|
104 if (!listeners.contains(l)) |
|
105 listeners.add(l); |
|
106 } |
|
107 } |
|
108 |
|
109 public void removeOtrEngineListener(OtrEngineListener l) { |
|
110 synchronized (listeners) { |
|
111 listeners.remove(l); |
|
112 } |
|
113 } |
|
114 } |