Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize on Signaling class #125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ Maven:
Documentation can be found at
[https://saltyrtc.github.io/saltyrtc-client-java/](https://saltyrtc.github.io/saltyrtc-client-java/).

Plase note that instances of this library are not considered thread-safe. Thus, an application
using more than one thread needs to take care of synchronisation itself.

## Manual Testing

To try a development version of the library, you can build a local version to
Expand Down
40 changes: 28 additions & 12 deletions src/main/java/org/saltyrtc/client/SaltyRTC.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SaltyRTC {
private boolean debug = false;

// Reference to signaling class
private Signaling signaling;
private final Signaling signaling;

// Event registry
public final SaltyRTC.Events events = new SaltyRTC.Events();
Expand Down Expand Up @@ -102,30 +102,40 @@ public class SaltyRTC {
}

public KeyStore getKeyStore() {
return this.signaling.getKeyStore();
synchronized (this.signaling) {
return this.signaling.getKeyStore();
}
}

public byte[] getPublicPermanentKey() {
return this.signaling.getPublicPermanentKey();
synchronized (this.signaling) {
return this.signaling.getPublicPermanentKey();
}
}

public byte[] getAuthToken() {
return this.signaling.getAuthToken();
synchronized (this.signaling) {
return this.signaling.getAuthToken();
}
}

/**
* Return the current signaling state.
*/
public SignalingState getSignalingState() {
return this.signaling.getState();
synchronized (this.signaling) {
return this.signaling.getState();
}
}

/**
* Return the negotiated task, or null if no task has been negotiated yet.
*/
@Nullable
public Task getTask() {
return this.signaling.getTask();
synchronized (this.signaling) {
return this.signaling.getTask();
}
}

/**
Expand All @@ -137,7 +147,9 @@ public Task getTask() {
* @throws ConnectionException if setting up the WebSocket connection fails.
*/
public void connect() throws ConnectionException {
this.signaling.connect();
synchronized (this.signaling) {
this.signaling.connect();
}
}

/**
Expand All @@ -147,11 +159,13 @@ public void connect() throws ConnectionException {
* @throws InvalidStateException if the SaltyRTC instance is not currently in the TASK signaling state.
*/
public void sendApplicationMessage(Object data) throws ConnectionException, InvalidStateException {
if (this.signaling.getState() != SignalingState.TASK) {
throw new InvalidStateException(
"Application messages can only be sent in TASK state, not in " + this.signaling.getState().name());
synchronized (this.signaling) {
if (this.signaling.getState() != SignalingState.TASK) {
throw new InvalidStateException(
"Application messages can only be sent in TASK state, not in " + this.signaling.getState().name());
}
this.signaling.sendApplication(new Application(data));
}
this.signaling.sendApplication(new Application(data));
}

/**
Expand All @@ -162,7 +176,9 @@ public void sendApplicationMessage(Object data) throws ConnectionException, Inva
* this method again from within your `SignalingStateChangedEvent` event handlers, or deadlocks may occur!
*/
public void disconnect() {
this.signaling.disconnect();
synchronized (this.signaling) {
this.signaling.disconnect();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public InitiatorSignaling(SaltyRTC saltyRTC, String host, int port,
/**
* Handle signaling errors during peer handshake.
*/
synchronized void handlePeerHandshakeSignalingError(@NonNull SignalingException e, short source) {
void handlePeerHandshakeSignalingError(@NonNull SignalingException e, short source) {
// Simply drop the responder
Responder responder = this.responders.get(source);
if (responder != null) {
Expand Down Expand Up @@ -544,7 +544,7 @@ private void dropResponders() throws SignalingException, ConnectionException {
}

@Override
synchronized void handleSendError(short receiver) throws SignalingException {
void handleSendError(short receiver) throws SignalingException {
// Validate receiver byte
if (!this.isResponderId(receiver)) {
throw new ProtocolException("Outgoing c2c messages must have been sent to a responder");
Expand Down
Loading