diff options
author | Andrew Comminos <andrew@comminos.com> | 2015-10-25 00:24:07 +0300 |
---|---|---|
committer | Andrew Comminos <andrew@comminos.com> | 2015-10-25 00:24:07 +0300 |
commit | 28495cdc35bf8a2e680ce3dc8eab228a1bb7565c (patch) | |
tree | 168a74894f11b329ea70e4ae4b0040e1be0530e6 /src/main/java/com/morlunk/jumble | |
parent | d5b5ac2e75f96d7f93cb62149c77f8c544084658 (diff) |
Propagate connection-related exceptions as checked until reaching JumbleService's API.
Diffstat (limited to 'src/main/java/com/morlunk/jumble')
4 files changed, 257 insertions, 84 deletions
diff --git a/src/main/java/com/morlunk/jumble/JumbleService.java b/src/main/java/com/morlunk/jumble/JumbleService.java index 127763f..e9fd0c1 100644 --- a/src/main/java/com/morlunk/jumble/JumbleService.java +++ b/src/main/java/com/morlunk/jumble/JumbleService.java @@ -36,6 +36,8 @@ import android.util.Log; import com.morlunk.jumble.audio.AudioOutput; import com.morlunk.jumble.audio.BluetoothScoReceiver; import com.morlunk.jumble.exception.AudioException; +import com.morlunk.jumble.exception.NotConnectedException; +import com.morlunk.jumble.exception.NotSynchronizedException; import com.morlunk.jumble.model.Channel; import com.morlunk.jumble.model.IChannel; import com.morlunk.jumble.model.IUser; @@ -167,12 +169,18 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn mHandler.post(new Runnable() { @Override public void run() { - if(!isConnectionEstablished()) return; - final User currentUser = mModelHandler.getUser(mConnection.getSession()); - if(currentUser == null) return; + try { + if (!isSynchronized()) + throw new NotSynchronizedException(); - currentUser.setTalkState(state); - mCallbacks.onUserTalkStateUpdated(currentUser); + final User currentUser = mModelHandler.getUser(mConnection.getSession()); + if (currentUser == null) return; + + currentUser.setTalkState(state); + mCallbacks.onUserTalkStateUpdated(currentUser); + } catch (NotSynchronizedException e) { + e.printStackTrace(); + } } }); } @@ -280,7 +288,7 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn * the server's model and settings. This is the main state of the service. */ public boolean isSynchronized() { - return mConnectionState == ConnectionState.CONNECTED; + return mConnection != null && mConnection.isSynchronized(); } @Override @@ -321,6 +329,8 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn } catch (AudioException e) { e.printStackTrace(); onConnectionWarning(e.getMessage()); + } catch (NotSynchronizedException e) { + throw new RuntimeException("Connection should be synchronized in callback for synchronization!", e); } mCallbacks.onConnected(); @@ -437,11 +447,15 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn mAudioHandler.shutdown(); } - mAudioHandler = mAudioBuilder.initialize( - mModelHandler.getUser(mConnection.getSession()), - mConnection.getMaxBandwidth(), mConnection.getCodec()); - mConnection.addTCPMessageHandlers(mAudioHandler); - mConnection.addUDPMessageHandlers(mAudioHandler); + try { + mAudioHandler = mAudioBuilder.initialize( + mModelHandler.getUser(mConnection.getSession()), + mConnection.getMaxBandwidth(), mConnection.getCodec()); + mConnection.addTCPMessageHandlers(mAudioHandler); + mConnection.addUDPMessageHandlers(mAudioHandler); + } catch (NotSynchronizedException e) { + throw new RuntimeException("Attempted to create audio handler when not synchronized!"); + } } /** @@ -594,9 +608,9 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn * to a server, and destroyed upon disconnection. * @return the active AudioHandler, or null if there is no active connection. */ - private AudioHandler getAudioHandler() { + private AudioHandler getAudioHandler() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); if (mAudioHandler == null && mConnectionState == ConnectionState.CONNECTED) throw new RuntimeException("Audio handler should always be instantiated while connected!"); return mAudioHandler; @@ -607,9 +621,9 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn * valid for the lifetime of a connection. * @return the active ModelHandler, or null if there is no active connection. */ - private ModelHandler getModelHandler() { + private ModelHandler getModelHandler() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); if (mModelHandler == null && mConnectionState == ConnectionState.CONNECTED) throw new RuntimeException("Model handler should always be instantiated while connected!"); return mModelHandler; @@ -618,11 +632,10 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn /** * Returns the bluetooth service provider, established after synchronization. * @return The {@link BluetoothScoReceiver} attached to this service. - * @throws IllegalStateException if not synchronized or disconnected. */ - private BluetoothScoReceiver getBluetoothReceiver() { + private BluetoothScoReceiver getBluetoothReceiver() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mBluetoothReceiver; } @@ -649,52 +662,92 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn @Override public long getTCPLatency() { - return getConnection().getTCPLatency(); + try { + return getConnection().getTCPLatency(); + } catch (NotConnectedException e) { + throw new IllegalStateException(e); + } } @Override public long getUDPLatency() { - return getConnection().getUDPLatency(); + try { + return getConnection().getUDPLatency(); + } catch (NotConnectedException e) { + throw new IllegalStateException(e); + } } @Override public int getMaxBandwidth() { - return getConnection().getMaxBandwidth(); + try { + return getConnection().getMaxBandwidth(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public int getCurrentBandwidth() { - return getAudioHandler().getCurrentBandwidth(); + try { + return getAudioHandler().getCurrentBandwidth(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public int getServerVersion() { - return getConnection().getServerVersion(); + try { + return getConnection().getServerVersion(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public String getServerRelease() { - return getConnection().getServerRelease(); + try { + return getConnection().getServerRelease(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public String getServerOSName() { - return getConnection().getServerOSName(); + try { + return getConnection().getServerOSName(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public String getServerOSVersion() { - return getConnection().getServerOSVersion(); + try { + return getConnection().getServerOSVersion(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public int getSession() { - return getConnection().getSession(); + try { + return getConnection().getSession(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public IUser getSessionUser() { - return getModelHandler().getUser(getSession()); + try { + return getModelHandler().getUser(getSession()); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override @@ -712,12 +765,20 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn @Override public IUser getUser(int session) { - return getModelHandler().getUser(session); + try { + return getModelHandler().getUser(session); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public IChannel getChannel(int id) { - return getModelHandler().getChannel(id); + try { + return getModelHandler().getChannel(id); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override @@ -727,37 +788,65 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn @Override public int getPermissions() { - return getModelHandler().getPermissions(); + try { + return getModelHandler().getPermissions(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public int getTransmitMode() { - return getAudioHandler().getTransmitMode(); + try { + return getAudioHandler().getTransmitMode(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public JumbleUDPMessageType getCodec() { - return getConnection().getCodec(); + try { + return getConnection().getCodec(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public boolean usingBluetoothSco() { - return getBluetoothReceiver().isBluetoothScoOn(); + try { + return getBluetoothReceiver().isBluetoothScoOn(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public void enableBluetoothSco() { - getBluetoothReceiver().startBluetoothSco(); + try { + getBluetoothReceiver().startBluetoothSco(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public void disableBluetoothSco() { - getBluetoothReceiver().stopBluetoothSco(); + try { + getBluetoothReceiver().stopBluetoothSco(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public boolean isTalking() { - return getAudioHandler().isRecording(); + try { + return getAudioHandler().isRecording(); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override @@ -765,15 +854,19 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn if (getSessionUser().isSelfMuted() || getSessionUser().isMuted()) return; - if (getAudioHandler().getTransmitMode() != Constants.TRANSMIT_PUSH_TO_TALK) { - Log.w(Constants.TAG, "Attempted to set talking state when not using PTT"); - return; - } - try { - getAudioHandler().setTalking(talking); - } catch (AudioException e) { - logError(e.getMessage()); + if (getAudioHandler().getTransmitMode() != Constants.TRANSMIT_PUSH_TO_TALK) { + Log.w(Constants.TAG, "Attempted to set talking state when not using PTT"); + return; + } + + try { + getAudioHandler().setTalking(talking); + } catch (AudioException e) { + logError(e.getMessage()); + } + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); } } @@ -863,31 +956,45 @@ public class JumbleService extends Service implements IJumbleService, JumbleConn @Override public Message sendUserTextMessage(int session, String message) { - Mumble.TextMessage.Builder tmb = Mumble.TextMessage.newBuilder(); - tmb.addSession(session); - tmb.setMessage(message); - getConnection().sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); - - User self = getModelHandler().getUser(getSession()); - User user = getModelHandler().getUser(session); - List<User> users = new ArrayList<User>(1); - users.add(user); - return new Message(getSession(), self.getName(), new ArrayList<Channel>(0), new ArrayList<Channel>(0), users, message); + try { + if (!isSynchronized()) + throw new NotSynchronizedException(); + + Mumble.TextMessage.Builder tmb = Mumble.TextMessage.newBuilder(); + tmb.addSession(session); + tmb.setMessage(message); + getConnection().sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); + + User self = getModelHandler().getUser(getSession()); + User user = getModelHandler().getUser(session); + List<User> users = new ArrayList<User>(1); + users.add(user); + return new Message(getSession(), self.getName(), new ArrayList<Channel>(0), new ArrayList<Channel>(0), users, message); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override public Message sendChannelTextMessage(int channel, String message, boolean tree) { - Mumble.TextMessage.Builder tmb = Mumble.TextMessage.newBuilder(); - if(tree) tmb.addTreeId(channel); - else tmb.addChannelId(channel); - tmb.setMessage(message); - getConnection().sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); - - User self = getModelHandler().getUser(getSession()); - Channel targetChannel = getModelHandler().getChannel(channel); - List<Channel> targetChannels = new ArrayList<Channel>(); - targetChannels.add(targetChannel); - return new Message(getSession(), self.getName(), targetChannels, tree ? targetChannels : new ArrayList<Channel>(0), new ArrayList<User>(0), message); + try { + if (!isSynchronized()) + throw new NotSynchronizedException(); + + Mumble.TextMessage.Builder tmb = Mumble.TextMessage.newBuilder(); + if (tree) tmb.addTreeId(channel); + else tmb.addChannelId(channel); + tmb.setMessage(message); + getConnection().sendTCPMessage(tmb.build(), JumbleTCPMessageType.TextMessage); + + User self = getModelHandler().getUser(getSession()); + Channel targetChannel = getModelHandler().getChannel(channel); + List<Channel> targetChannels = new ArrayList<Channel>(); + targetChannels.add(targetChannel); + return new Message(getSession(), self.getName(), targetChannels, tree ? targetChannels : new ArrayList<Channel>(0), new ArrayList<User>(0), message); + } catch (NotSynchronizedException e) { + throw new IllegalStateException(e); + } } @Override diff --git a/src/main/java/com/morlunk/jumble/exception/NotConnectedException.java b/src/main/java/com/morlunk/jumble/exception/NotConnectedException.java new file mode 100644 index 0000000..c81db73 --- /dev/null +++ b/src/main/java/com/morlunk/jumble/exception/NotConnectedException.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015 Andrew Comminos <andrew@comminos.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.morlunk.jumble.exception; + +/** + * Thrown when a Jumble connection has not yet been established. + * Created by andrew on 24/10/15. + */ +public class NotConnectedException extends Exception { + public NotConnectedException() { + super("Not yet connected"); + } + + public NotConnectedException(String message) { + super(message); + } +} diff --git a/src/main/java/com/morlunk/jumble/exception/NotSynchronizedException.java b/src/main/java/com/morlunk/jumble/exception/NotSynchronizedException.java new file mode 100644 index 0000000..c65fd15 --- /dev/null +++ b/src/main/java/com/morlunk/jumble/exception/NotSynchronizedException.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2015 Andrew Comminos <andrew@comminos.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package com.morlunk.jumble.exception; + +/** + * Called when Jumble has not yet received the ServerSync message from the server. + * Created by andrew on 24/10/15. + */ +public class NotSynchronizedException extends Exception { + public NotSynchronizedException() { + super("Not yet synchronized"); + } + + public NotSynchronizedException(String message) { + super(message); + } +} diff --git a/src/main/java/com/morlunk/jumble/net/JumbleConnection.java b/src/main/java/com/morlunk/jumble/net/JumbleConnection.java index 6fd919f..6f54199 100644 --- a/src/main/java/com/morlunk/jumble/net/JumbleConnection.java +++ b/src/main/java/com/morlunk/jumble/net/JumbleConnection.java @@ -25,6 +25,8 @@ import com.google.protobuf.ByteString; import com.google.protobuf.InvalidProtocolBufferException; import com.google.protobuf.Message; import com.morlunk.jumble.Constants; +import com.morlunk.jumble.exception.NotConnectedException; +import com.morlunk.jumble.exception.NotSynchronizedException; import com.morlunk.jumble.protobuf.Mumble; import com.morlunk.jumble.protocol.JumbleTCPMessageListener; import com.morlunk.jumble.protocol.JumbleUDPMessageListener; @@ -398,45 +400,45 @@ public class JumbleConnection implements JumbleTCP.TCPConnectionListener, Jumble mTrustStoreFormat = format; } - public int getServerVersion() { + public int getServerVersion() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mServerVersion; } - public String getServerRelease() { + public String getServerRelease() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mServerRelease; } - public String getServerOSName() { + public String getServerOSName() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mServerOSName; } - public String getServerOSVersion() { + public String getServerOSVersion() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mServerOSVersion; } - public long getTCPLatency() { + public long getTCPLatency() throws NotConnectedException { if (!isConnected()) - throw new IllegalStateException("Not connected"); + throw new NotConnectedException(); return mLastTCPPing; } - public long getUDPLatency() { + public long getUDPLatency() throws NotConnectedException { if (!isConnected()) - throw new IllegalStateException("Not connected"); + throw new NotConnectedException(); return mLastUDPPing; } - public int getSession() { + public int getSession() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException("Session is set during synchronization"); return mSession; } @@ -444,15 +446,15 @@ public class JumbleConnection implements JumbleTCP.TCPConnectionListener, Jumble * Returns the server-reported maximum input bandwidth, or -1 if not set. * @return the input bandwidth in bps, or -1 if not set. */ - public int getMaxBandwidth() { + public int getMaxBandwidth() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mMaxBandwidth; } - public JumbleUDPMessageType getCodec() { + public JumbleUDPMessageType getCodec() throws NotSynchronizedException { if (!isSynchronized()) - throw new IllegalStateException("Not synchronized"); + throw new NotSynchronizedException(); return mCodec; } |