|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2009, 2014 IBM Corp. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * http://www.eclipse.org/legal/epl-v10.html |
| 10 | + * and the Eclipse Distribution License is available at |
| 11 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 | + * |
| 13 | + * Contributors: |
| 14 | + * Dave Locke - initial API and implementation and/or initial documentation |
| 15 | + */ |
| 16 | +package org.eclipse.paho.mqttv5.client.test.utilities; |
| 17 | + |
| 18 | +import java.nio.ByteBuffer; |
| 19 | +import java.util.Enumeration; |
| 20 | +import java.util.Hashtable; |
| 21 | + |
| 22 | +import org.eclipse.paho.mqttv5.client.MqttClientPersistence; |
| 23 | +import org.eclipse.paho.mqttv5.client.internal.MqttPersistentData; |
| 24 | +import org.eclipse.paho.mqttv5.common.MqttPersistable; |
| 25 | +import org.eclipse.paho.mqttv5.common.MqttPersistenceException; |
| 26 | + |
| 27 | +/** |
| 28 | + * Persistence that uses memory |
| 29 | + * |
| 30 | + * In cases where reliability is not required across client or device |
| 31 | + * restarts memory this memory persistence can be used. In cases where |
| 32 | + * reliability is required like when clean session is set to false |
| 33 | + * then a non-volatile form of persistence should be used. |
| 34 | + */ |
| 35 | +public class TestByteArrayMemoryPersistence implements MqttClientPersistence { |
| 36 | + |
| 37 | + private Hashtable<String, byte[]> data; |
| 38 | + private Hashtable<String, byte[]> dataCache; |
| 39 | + private String clientId; |
| 40 | + private String serverURI; |
| 41 | + |
| 42 | + /* (non-Javadoc) |
| 43 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#close() |
| 44 | + */ |
| 45 | + public void close() throws MqttPersistenceException { |
| 46 | + //data.clear(); |
| 47 | + } |
| 48 | + |
| 49 | + /* (non-Javadoc) |
| 50 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#keys() |
| 51 | + */ |
| 52 | + public Enumeration<String> keys() throws MqttPersistenceException { |
| 53 | + return data.keys(); |
| 54 | + } |
| 55 | + |
| 56 | + /* (non-Javadoc) |
| 57 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#get(java.lang.String) |
| 58 | + */ |
| 59 | + public MqttPersistable get(String key) throws MqttPersistenceException { |
| 60 | + byte[] persistedMessage = data.get(key); |
| 61 | + MqttPersistable message = new MqttPersistentData(key, persistedMessage, 0, persistedMessage.length, null, 0, 0); |
| 62 | + return message; |
| 63 | + } |
| 64 | + |
| 65 | + /* (non-Javadoc) |
| 66 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#open(java.lang.String, java.lang.String) |
| 67 | + */ |
| 68 | + public void open(String clientId, String serverURI) throws MqttPersistenceException { |
| 69 | + this.clientId = clientId; |
| 70 | + this.serverURI = serverURI; |
| 71 | + if(this.data == null){ |
| 72 | + this.data = new Hashtable<String, byte[]>(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /* (non-Javadoc) |
| 77 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#put(java.lang.String, org.eclipse.paho.client.mqttv3.MqttPersistable) |
| 78 | + */ |
| 79 | + public void put(String key, MqttPersistable persistable) throws MqttPersistenceException { |
| 80 | + int length = persistable.getHeaderLength() + persistable.getPayloadLength(); |
| 81 | + ByteBuffer messageByteBuffer = ByteBuffer.wrap(new byte[length]).put(persistable.getHeaderBytes()); |
| 82 | + if(persistable.getPayloadBytes() != null) { |
| 83 | + messageByteBuffer.put(persistable.getPayloadBytes()); |
| 84 | + } |
| 85 | + byte[] messageBytes = messageByteBuffer.array(); |
| 86 | + data.put(key, messageBytes); |
| 87 | + dataCache.put(key, messageBytes); |
| 88 | + } |
| 89 | + |
| 90 | + /* (non-Javadoc) |
| 91 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#remove(java.lang.String) |
| 92 | + */ |
| 93 | + public void remove(String key) throws MqttPersistenceException { |
| 94 | + data.remove(key); |
| 95 | + } |
| 96 | + |
| 97 | + /* (non-Javadoc) |
| 98 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#clear() |
| 99 | + */ |
| 100 | + public void clear() throws MqttPersistenceException { |
| 101 | + data.clear(); |
| 102 | + } |
| 103 | + |
| 104 | + /* (non-Javadoc) |
| 105 | + * @see org.eclipse.paho.client.mqttv3.MqttClientPersistence#containsKey(java.lang.String) |
| 106 | + */ |
| 107 | + public boolean containsKey(String key) throws MqttPersistenceException { |
| 108 | + return data.containsKey(key); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void open(String clientId) throws MqttPersistenceException { |
| 113 | + data = new Hashtable<String, byte[]>(); |
| 114 | + dataCache = new Hashtable<String, byte[]>(); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + // Returns the Data cache |
| 119 | + public Hashtable<String, byte[]> getDataCache() { |
| 120 | + return dataCache; |
| 121 | + } |
| 122 | + |
| 123 | + public Hashtable<String, byte[]> getData() { |
| 124 | + return data; |
| 125 | + } |
| 126 | + |
| 127 | + public void setData(Hashtable<String, byte[]> data) { |
| 128 | + this.data = data; |
| 129 | + } |
| 130 | + |
| 131 | + public String getClientId() { |
| 132 | + return clientId; |
| 133 | + } |
| 134 | + |
| 135 | + public void setClientId(String clientId) { |
| 136 | + this.clientId = clientId; |
| 137 | + } |
| 138 | + |
| 139 | + public String getServerURI() { |
| 140 | + return serverURI; |
| 141 | + } |
| 142 | + |
| 143 | + public void setServerURI(String serverURI) { |
| 144 | + this.serverURI = serverURI; |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | +} |
0 commit comments