redo locking around chat client state

pull/34/head
Zlatin Balevsky 2019-11-16 14:41:47 +00:00
parent 17eff7d77f
commit 286a0a8678
2 changed files with 32 additions and 15 deletions

View File

@ -29,10 +29,10 @@ class ChatClient implements Closeable {
private final TrustService trustService private final TrustService trustService
private final MuWireSettings settings private final MuWireSettings settings
private volatile ChatConnection connection private ChatConnection connection
private volatile boolean connectInProgress private boolean connectInProgress
private volatile long lastRejectionTime private long lastRejectionTime
private volatile Thread connectThread private Thread connectThread
ChatClient(I2PConnector connector, EventBus eventBus, Persona host, Persona me, TrustService trustService, ChatClient(I2PConnector connector, EventBus eventBus, Persona host, Persona me, TrustService trustService,
MuWireSettings settings) { MuWireSettings settings) {
@ -44,7 +44,7 @@ class ChatClient implements Closeable {
this.settings = settings this.settings = settings
} }
void connectIfNeeded() { synchronized void connectIfNeeded() {
if (connection != null || connectInProgress || (System.currentTimeMillis() - lastRejectionTime < REJECTION_BACKOFF)) if (connection != null || connectInProgress || (System.currentTimeMillis() - lastRejectionTime < REJECTION_BACKOFF))
return return
connectInProgress = true connectInProgress = true
@ -52,7 +52,11 @@ class ChatClient implements Closeable {
} }
private void connect() { private void connect() {
connectThread = Thread.currentThread() synchronized(this) {
if (!connectInProgress)
return
connectThread = Thread.currentThread()
}
Endpoint endpoint = null Endpoint endpoint = null
try { try {
eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.CONNECTING, persona : host)) eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.CONNECTING, persona : host))
@ -75,7 +79,9 @@ class ChatClient implements Closeable {
eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.REJECTED, persona : host)) eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.REJECTED, persona : host))
try { dos.close() } catch (IOException ignore) {} try { dos.close() } catch (IOException ignore) {}
endpoint.close() endpoint.close()
lastRejectionTime = System.currentTimeMillis() synchronized(this) {
lastRejectionTime = System.currentTimeMillis()
}
return return
} }
@ -90,8 +96,12 @@ class ChatClient implements Closeable {
if (version != Constants.CHAT_VERSION) if (version != Constants.CHAT_VERSION)
throw new Exception("Unknown chat version $version") throw new Exception("Unknown chat version $version")
connection = new ChatConnection(eventBus, endpoint, host, false, trustService, settings) synchronized(this) {
connection.start() if (!connectInProgress)
return
connection = new ChatConnection(eventBus, endpoint, host, false, trustService, settings)
connection.start()
}
eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.SUCCESSFUL, persona : host, eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.SUCCESSFUL, persona : host,
connection : connection)) connection : connection))
} catch (Exception e) { } catch (Exception e) {
@ -102,24 +112,31 @@ class ChatClient implements Closeable {
endpoint.close() endpoint.close()
} }
} finally { } finally {
connectInProgress = false synchronized(this) {
connectThread = null connectInProgress = false
connectThread = null
}
} }
} }
void disconnected() { synchronized void disconnected() {
connectInProgress = false connectInProgress = false
connection = null connection = null
} }
@Override @Override
public void close() { synchronized public void close() {
connectInProgress = false
connectThread?.interrupt() connectThread?.interrupt()
connection?.close() connection?.close()
eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.DISCONNECTED, persona : host)) eventBus.publish(new ChatConnectionEvent(status : ChatConnectionAttemptStatus.DISCONNECTED, persona : host))
} }
void ping() { synchronized void ping() {
connection?.sendPing() connection?.sendPing()
} }
synchronized void sendChat(ChatMessageEvent e) {
connection?.sendChat(e)
}
} }

View File

@ -51,7 +51,7 @@ class ChatManager {
return return
if (e.sender != me) if (e.sender != me)
return return
clients[e.host]?.connection?.sendChat(e) clients[e.host]?.sendChat(e)
} }
void onChatDisconnectionEvent(ChatDisconnectionEvent e) { void onChatDisconnectionEvent(ChatDisconnectionEvent e) {