/** * Delete all messages (optionally for a specific client) * * @param clientHandle identifier for the client.<br> * If null, all messages are deleted */ @Override public void clearArrivedMessages(String clientHandle) { db = mqttDb.getWritableDatabase(); int rows = 0; if (clientHandle == null) { traceHandler.traceDebug(TAG, "clearArrivedMessages: clearing the table"); rows = db.delete(ARRIVED_MESSAGE_TABLE_NAME, null, null); } else { traceHandler.traceDebug( TAG, "clearArrivedMessages: clearing the table of " + clientHandle + " messages"); rows = db.delete( ARRIVED_MESSAGE_TABLE_NAME, MqttServiceConstants.CLIENT_HANDLE + "='" + clientHandle + "'", null); } traceHandler.traceDebug(TAG, "clearArrivedMessages: rows affected = " + rows); return; }
/** * Delete an MQTT message. * * @param clientHandle identifier for the client which stored the message * @param id the identifying string returned when the message was stored * @return true if the message was found and deleted */ @Override public boolean discardArrived(String clientHandle, String id) { db = mqttDb.getWritableDatabase(); traceHandler.traceDebug(TAG, "discardArrived{" + clientHandle + "}, {" + id + "}"); int rows; try { rows = db.delete( ARRIVED_MESSAGE_TABLE_NAME, MqttServiceConstants.MESSAGE_ID + "='" + id + "' AND " + MqttServiceConstants.CLIENT_HANDLE + "='" + clientHandle + "'", null); } catch (SQLException e) { traceHandler.traceException(TAG, "discardArrived", e); throw e; } if (rows != 1) { traceHandler.traceError( TAG, "discardArrived - Error deleting message {" + id + "} from database: Rows affected = " + rows); return false; } int count = getArrivedRowCount(clientHandle); traceHandler.traceDebug( TAG, "discardArrived - Message deleted successfully. - messages in db for this clientHandle " + count); return true; }
/** * Store an MQTT message * * @param clientHandle identifier for the client storing the message * @param topic The topic on which the message was published * @param message the arrived MQTT message * @return an identifier for the message, so that it can be removed when appropriate */ @Override public String storeArrived(String clientHandle, String topic, MqttMessage message) { db = mqttDb.getWritableDatabase(); traceHandler.traceDebug( TAG, "storeArrived{" + clientHandle + "}, {" + message.toString() + "}"); byte[] payload = message.getPayload(); int qos = message.getQos(); boolean retained = message.isRetained(); boolean duplicate = message.isDuplicate(); ContentValues values = new ContentValues(); String id = java.util.UUID.randomUUID().toString(); values.put(MqttServiceConstants.MESSAGE_ID, id); values.put(MqttServiceConstants.CLIENT_HANDLE, clientHandle); values.put(MqttServiceConstants.DESTINATION_NAME, topic); values.put(MqttServiceConstants.PAYLOAD, payload); values.put(MqttServiceConstants.QOS, qos); values.put(MqttServiceConstants.RETAINED, retained); values.put(MqttServiceConstants.DUPLICATE, duplicate); values.put(MTIMESTAMP, System.currentTimeMillis()); try { db.insertOrThrow(ARRIVED_MESSAGE_TABLE_NAME, null, values); } catch (SQLException e) { traceHandler.traceException(TAG, "onUpgrade", e); throw e; } int count = getArrivedRowCount(clientHandle); traceHandler.traceDebug( TAG, "storeArrived: inserted message with id of {" + id + "} - Number of messages in database for this clientHandle = " + count); return id; }