src/net/java/otr4j/OtrEngineImpl.java
changeset 911 ca323cff3ac9
parent 906 0ff0059f2ec3
equal deleted inserted replaced
910:2ef1c6096069 911:ca323cff3ac9
    23  * @author George Politis
    23  * @author George Politis
    24  * 
    24  * 
    25  */
    25  */
    26 public class OtrEngineImpl implements OtrEngine {
    26 public class OtrEngineImpl implements OtrEngine {
    27 
    27 
    28 	public OtrEngineImpl(OtrEngineHost listener) {
    28 	public OtrEngineImpl(OtrEngineHost host) {
    29 		if (listener == null)
    29 		if (host == null)
    30 			throw new IllegalArgumentException("OtrEgineHost is required.");
    30 			throw new IllegalArgumentException("OtrEgineHost is required.");
    31 
    31 
    32 		this.setListener(listener);
    32 		this.setHost(host);
    33 	}
    33 	}
    34 
    34 
    35 	private OtrEngineHost listener;
    35 	private OtrEngineHost host;
    36 	private Map<SessionID, Session> sessions;
    36 	private Map<SessionID, Session> sessions;
    37 
    37 
    38 	private Session getSession(SessionID sessionID) {
    38 	private Session getSession(SessionID sessionID) {
    39 
    39 
    40 		if (sessionID == null || sessionID.equals(SessionID.Empty))
    40 		if (sessionID == null || sessionID.equals(SessionID.Empty))
    42 
    42 
    43 		if (sessions == null)
    43 		if (sessions == null)
    44 			sessions = new Hashtable<SessionID, Session>();
    44 			sessions = new Hashtable<SessionID, Session>();
    45 
    45 
    46 		if (!sessions.containsKey(sessionID)) {
    46 		if (!sessions.containsKey(sessionID)) {
    47 			Session session = new SessionImpl(sessionID, getListener());
    47 			Session session = new SessionImpl(sessionID, getHost());
    48 			sessions.put(sessionID, session);
    48 			sessions.put(sessionID, session);
    49 
    49 
    50 			session.addOtrEngineListener(new OtrEngineListener() {
    50 			session.addOtrEngineListener(new OtrEngineListener() {
    51 
    51 
    52 				public void sessionStatusChanged(SessionID sessionID) {
    52 				public void sessionStatusChanged(SessionID sessionID) {
    53 					for (OtrEngineListener l : listeners)
    53 					for (OtrEngineListener l : listeners)
    54 						l.sessionStatusChanged(sessionID);
    54 						l.sessionStatusChanged(sessionID);
    55 				}
    55 				}
    56 			});
    56 			});
    57 
    57 			return session;
    58 		}
    58 		} else
    59 
    59 			return sessions.get(sessionID);
    60 		return sessions.get(sessionID);
       
    61 	}
    60 	}
    62 
    61 
    63 	public SessionStatus getSessionStatus(SessionID sessionID) {
    62 	public SessionStatus getSessionStatus(SessionID sessionID) {
    64 		return this.getSession(sessionID).getSessionStatus();
    63 		return this.getSession(sessionID).getSessionStatus();
    65 	}
    64 	}
    66 
    65 
    67 	public String transformReceiving(SessionID sessionID, String msgText) {
    66 	public String transformReceiving(SessionID sessionID, String msgText)
    68 		try {
    67 			throws OtrException {
    69 			return this.getSession(sessionID).transformReceiving(msgText);
    68 		return this.getSession(sessionID).transformReceiving(msgText);
    70 		} catch (OtrException e) {
       
    71 			listener.showError(sessionID, e.getMessage());
       
    72 			return null;
       
    73 		}
       
    74 	}
    69 	}
    75 
    70 
    76 	public String transformSending(SessionID sessionID, String msgText) {
    71 	public String transformSending(SessionID sessionID, String msgText)
    77 		try {
    72 			throws OtrException {
    78 			return this.getSession(sessionID).transformSending(msgText, null);
    73 		return this.getSession(sessionID).transformSending(msgText, null);
    79 		} catch (OtrException e) {
       
    80 			listener.showError(sessionID, e.getMessage());
       
    81 			return null;
       
    82 		}
       
    83 	}
    74 	}
    84 
    75 
    85 	public void endSession(SessionID sessionID) {
    76 	public void endSession(SessionID sessionID) throws OtrException {
    86 		try {
    77 		this.getSession(sessionID).endSession();
    87 			this.getSession(sessionID).endSession();
       
    88 		} catch (OtrException e) {
       
    89 			listener.showError(sessionID, e.getMessage());
       
    90 		}
       
    91 	}
    78 	}
    92 
    79 
    93 	public void startSession(SessionID sessionID) {
    80 	public void startSession(SessionID sessionID) throws OtrException {
    94 		try {
    81 		this.getSession(sessionID).startSession();
    95 			this.getSession(sessionID).startSession();
       
    96 		} catch (OtrException e) {
       
    97 			listener.showError(sessionID, e.getMessage());
       
    98 		}
       
    99 	}
    82 	}
   100 
    83 
   101 	private void setListener(OtrEngineHost listener) {
    84 	private void setHost(OtrEngineHost host) {
   102 		this.listener = listener;
    85 		this.host = host;
   103 	}
    86 	}
   104 
    87 
   105 	private OtrEngineHost getListener() {
    88 	private OtrEngineHost getHost() {
   106 		return listener;
    89 		return host;
   107 	}
    90 	}
   108 
    91 
   109 	public void refreshSession(SessionID sessionID) {
    92 	public void refreshSession(SessionID sessionID) throws OtrException {
   110 		try {
    93 		this.getSession(sessionID).refreshSession();
   111 			this.getSession(sessionID).refreshSession();
       
   112 		} catch (OtrException e) {
       
   113 			listener.showError(sessionID, e.getMessage());
       
   114 		}
       
   115 	}
    94 	}
   116 
    95 
   117 	public PublicKey getRemotePublicKey(SessionID sessionID) {
    96 	public PublicKey getRemotePublicKey(SessionID sessionID) {
   118 		return this.getSession(sessionID).getRemotePublicKey();
    97 		return this.getSession(sessionID).getRemotePublicKey();
   119 	}
    98 	}
   123 	public void addOtrEngineListener(OtrEngineListener l) {
   102 	public void addOtrEngineListener(OtrEngineListener l) {
   124 		synchronized (listeners) {
   103 		synchronized (listeners) {
   125 			if (!listeners.contains(l))
   104 			if (!listeners.contains(l))
   126 				listeners.add(l);
   105 				listeners.add(l);
   127 		}
   106 		}
   128 
       
   129 	}
   107 	}
   130 
   108 
   131 	public void removeOtrEngineListener(OtrEngineListener l) {
   109 	public void removeOtrEngineListener(OtrEngineListener l) {
   132 		synchronized (listeners) {
   110 		synchronized (listeners) {
   133 			listeners.remove(l);
   111 			listeners.remove(l);