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

Support message buffering before initial connect #286

Open
wants to merge 3 commits into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.eclipse.paho.android.sample.model.ReceivedMessage;
import org.eclipse.paho.android.sample.model.Subscription;
import org.eclipse.paho.android.service.MqttAndroidClient;
import org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
Expand Down Expand Up @@ -84,6 +85,10 @@ public class Connection {
* True if this connection is secured using TLS
**/
private boolean tlsConnection = true;
/**
* Whether the connection should enable the buffer
**/
private boolean bufferEnabled = true;
/**
* Persistence id, used by {@link Persistence}
**/
Expand All @@ -100,9 +105,10 @@ public class Connection {
* @param context The application context
* @param client The MqttAndroidClient which communicates with the service for this connection
* @param tlsConnection true if the connection is secured by SSL
* @param bufferEnabled true if the connection buffer should be enabled
*/
private Connection(String clientHandle, String clientId, String host,
int port, Context context, MqttAndroidClient client, boolean tlsConnection) {
int port, Context context, MqttAndroidClient client, boolean tlsConnection, boolean bufferEnabled) {
//generate the client handle from its hash code
this.clientHandle = clientHandle;
this.clientId = clientId;
Expand All @@ -111,6 +117,7 @@ private Connection(String clientHandle, String clientId, String host,
this.context = context;
this.client = client;
this.tlsConnection = tlsConnection;
this.bufferEnabled = bufferEnabled;
history = new ArrayList<>();
String sb = "Client: " + clientId + " created";
addAction(sb);
Expand All @@ -125,9 +132,11 @@ private Connection(String clientHandle, String clientId, String host,
* @param port the port on the server which the client will attempt to connect to
* @param context the application context
* @param tlsConnection true if the connection is secured by SSL
* @param bufferEnabled true if the connection buffer should be enabled
* @return a new instance of <code>Connection</code>
*/
public static Connection createConnection(String clientHandle, String clientId, String host, int port, Context context, boolean tlsConnection) {
public static Connection createConnection(String clientHandle, String clientId, String host, int port, Context context, boolean tlsConnection,
boolean bufferEnabled) {

String uri;
if (tlsConnection) {
Expand All @@ -137,7 +146,15 @@ public static Connection createConnection(String clientHandle, String clientId,
}

MqttAndroidClient client = new MqttAndroidClient(context, uri, clientId);
return new Connection(clientHandle, clientId, host, port, context, client, tlsConnection);

// add buffer options
if (bufferEnabled) {
DisconnectedBufferOptions bufferOpts = new DisconnectedBufferOptions();
bufferOpts.setBufferEnabled(true);
client.setBufferOpts(bufferOpts);
}

return new Connection(clientHandle, clientId, host, port, context, client, tlsConnection, bufferEnabled);
}

public void updateConnection(String clientId, String host, int port, boolean tlsConnection) {
Expand Down Expand Up @@ -339,6 +356,15 @@ public int isSSL() {
return tlsConnection ? 1 : 0;
}

/**
* Determines if the connections buffer is enabled
*
* @return true buffer is enabled
*/
public boolean isBufferEnabled() {
return bufferEnabled;
}

/**
* Assign a persistence ID to this object
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class EditConnectionFragment extends Fragment {
private EditText serverHostname;
private EditText serverPort;
private Switch cleanSession;
private Switch buffer;
private EditText username;
private EditText password;
private EditText tlsServerKey;
Expand Down Expand Up @@ -66,6 +67,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
serverPort = rootView.findViewById(R.id.add_connection_port);
serverPort.setText("");
cleanSession = rootView.findViewById(R.id.clean_session_switch);
buffer = rootView.findViewById(R.id.buffer_switch);
username = rootView.findViewById(R.id.username);
password = rootView.findViewById(R.id.password);
tlsServerKey = rootView.findViewById(R.id.tls_server_key);
Expand Down Expand Up @@ -169,6 +171,13 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});

buffer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
formModel.setBuffer(isChecked);
}
});

username.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Expand Down Expand Up @@ -338,6 +347,7 @@ private void populateFromConnectionModel(ConnectionModel connectionModel) {
serverHostname.setText(connectionModel.getServerHostName());
serverPort.setText(Integer.toString(connectionModel.getServerPort()));
cleanSession.setChecked(connectionModel.isCleanSession());
buffer.setChecked(connectionModel.isBufferEnabled());
username.setText(connectionModel.getUsername());
password.setText(connectionModel.getPassword());
tlsServerKey.setText(connectionModel.getTlsServerKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void persistAndConnect(ConnectionModel model) {
Log.i(TAG, "Persisting new connection:" + model.getClientHandle());
Connection connection = Connection
.createConnection(model.getClientHandle(), model.getClientId(), model.getServerHostName(), model.getServerPort(), this, model
.isTlsConnection());
.isTlsConnection(), model.isBufferEnabled());
connection.registerChangeListener(changeListener);
connection.changeConnectionStatus(Connection.ConnectionStatus.CONNECTING);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public class Persistence extends SQLiteOpenHelper implements BaseColumns {
* Table column for clean session
**/
private static final String COLUMN_CLEAN_SESSION = "cleanSession";
/**
* Table column for buffer
**/
private static final String COLUMN_BUFFER = "buffer";
/** Table column for **/

//last will
Expand Down Expand Up @@ -156,6 +160,7 @@ public class Persistence extends SQLiteOpenHelper implements BaseColumns {
COLUMN_USER_NAME + TEXT_TYPE + COMMA_SEP +
COLUMN_PASSWORD + TEXT_TYPE + COMMA_SEP +
COLUMN_CLEAN_SESSION + INT_TYPE + COMMA_SEP +
COLUMN_BUFFER + INT_TYPE + COMMA_SEP +
COLUMN_TOPIC + TEXT_TYPE + COMMA_SEP +
COLUMN_MESSAGE + TEXT_TYPE + COMMA_SEP +
COLUMN_QOS + INT_TYPE + COMMA_SEP +
Expand Down Expand Up @@ -271,6 +276,7 @@ private ContentValues getValues(Connection connection) {
//uses "condition ? trueValue: falseValue" for in line converting of values
char[] password = conOpts.getPassword();
values.put(COLUMN_CLEAN_SESSION, conOpts.isCleanSession() ? 1 : 0); //convert boolean to int and then put in values
values.put(COLUMN_BUFFER, connection.isBufferEnabled() ? 1 : 0); //convert boolean to int and then put in values
values.put(COLUMN_PASSWORD, password != null ? String.valueOf(password) : null); //convert char[] to String
values.put(COLUMN_MESSAGE, lastWill != null ? new String(lastWill.getPayload()) : null); // convert byte[] to string
values.put(COLUMN_QOS, lastWill != null ? lastWill.getQos() : 0);
Expand Down Expand Up @@ -339,6 +345,7 @@ public List<Connection> restoreConnections(Context context) throws PersistenceEx
COLUMN_ssl,
COLUMN_KEEP_ALIVE,
COLUMN_CLEAN_SESSION,
COLUMN_BUFFER,
COLUMN_TIME_OUT,
COLUMN_USER_NAME,
COLUMN_PASSWORD,
Expand Down Expand Up @@ -393,6 +400,7 @@ public List<Connection> restoreConnections(Context context) throws PersistenceEx

//get all values that need converting and convert integers to booleans in line using "condition ? trueValue : falseValue"
boolean cleanSession = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_CLEAN_SESSION)) == 1;
boolean bufferEnabled = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_BUFFER)) == 1;
boolean retained = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_RETAINED)) == 1;
boolean ssl = cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ssl)) == 1;

Expand All @@ -410,7 +418,7 @@ public List<Connection> restoreConnections(Context context) throws PersistenceEx
}

//now create the connection object
connection = Connection.createConnection(clientHandle, clientID, host, port, context, ssl);
connection = Connection.createConnection(clientHandle, clientID, host, port, context, ssl, bufferEnabled);
connection.addConnectionOptions(opts);
connection.assignPersistenceId(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class ConnectionModel {
private String serverHostName = "iot.eclipse.org";
private int serverPort = 1883;
private boolean cleanSession = true;
private boolean buffer = true;
private String username = "";
private String password = "";

Expand All @@ -36,6 +37,7 @@ public ConnectionModel(Connection connection) {
serverHostName = connection.getHostName();
serverPort = connection.getPort();
cleanSession = connection.getConnectionOptions().isCleanSession();
buffer = true; // unable to get buffer information from the connection...

if (connection.getConnectionOptions().getUserName() == null) {
username = "";
Expand Down Expand Up @@ -108,6 +110,14 @@ public void setCleanSession(boolean cleanSession) {
this.cleanSession = cleanSession;
}

public boolean isBufferEnabled() {
return buffer;
}

public void setBuffer(boolean buffer) {
this.buffer = buffer;
}

public String getUsername() {
return username;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,34 @@
android:layout_below="@+id/port_group"/>


<!-- Advanced Settings -->
<!-- General Settings -->


<Switch
android:id="@+id/clean_session_switch"
style="@style/Switch"
android:layout_below="@+id/divider3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/port_group"
android:text="@string/add_connection_clean_session_label"/>

<!-- Advanced Settings -->
<!-- General Settings -->

<Switch
android:id="@+id/buffer_switch"
style="@style/Switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/clean_session_switch"
android:text="@string/add_connection_buffer"/>

<TextView
android:id="@+id/advancedTextView"
style="@style/HeaderText"
android:layout_below="@+id/clean_session_switch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/buffer_switch"
android:text="@string/add_connection_advanced_label"/>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@

<!-- Message List Item -->
<string name="message_topic">Topic: </string>
<string name="add_connection_buffer">Enable Message Buffer</string>


</resources>
Loading