View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package net.anyflow.lannister.topic;
18  
19  import java.io.IOException;
20  
21  import com.fasterxml.jackson.annotation.JsonIgnore;
22  import com.fasterxml.jackson.annotation.JsonProperty;
23  import com.hazelcast.nio.ObjectDataInput;
24  import com.hazelcast.nio.ObjectDataOutput;
25  
26  import io.netty.handler.codec.mqtt.MqttQoS;
27  import net.anyflow.lannister.plugin.ITopicSubscription;
28  import net.anyflow.lannister.serialization.SerializableFactory;
29  
30  public class TopicSubscription
31  		implements com.hazelcast.nio.serialization.IdentifiedDataSerializable, ITopicSubscription {
32  	public static final TopicSubscriptions NEXUS = new TopicSubscriptions();
33  	public final static int ID = 8;
34  	@JsonProperty
35  	private String clientId;
36  	@JsonProperty
37  	private String topicFilter;
38  	@JsonProperty
39  	private MqttQoS qos;
40  
41  	public TopicSubscription() { // just for Serialization
42  	}
43  
44  	public TopicSubscription(String clientId, String topicFilter, MqttQoS qos) {
45  		this.clientId = clientId;
46  		this.topicFilter = topicFilter;
47  		this.qos = qos;
48  	}
49  
50  	public String key() {
51  		return TopicSubscriptions.key(topicFilter, clientId);
52  	}
53  
54  	@Override
55  	public String clientId() {
56  		return clientId;
57  	}
58  
59  	/*
60  	 * (non-Javadoc)
61  	 * 
62  	 * @see net.anyflow.lannister.topic.ITopicSubscription#topicFilter()
63  	 */
64  	@Override
65  	public String topicFilter() {
66  		return topicFilter;
67  	}
68  
69  	/*
70  	 * (non-Javadoc)
71  	 * 
72  	 * @see net.anyflow.lannister.topic.ITopicSubscription#qos()
73  	 */
74  	@Override
75  	public MqttQoS qos() {
76  		return qos;
77  	}
78  
79  	@JsonIgnore
80  	@Override
81  	public int getFactoryId() {
82  		return SerializableFactory.ID;
83  	}
84  
85  	@JsonIgnore
86  	@Override
87  	public int getId() {
88  		return ID;
89  	}
90  
91  	@Override
92  	public void writeData(ObjectDataOutput out) throws IOException {
93  		out.writeUTF(clientId);
94  		out.writeUTF(topicFilter);
95  		out.writeInt(qos != null ? qos.value() : Integer.MIN_VALUE);
96  	}
97  
98  	@Override
99  	public void readData(ObjectDataInput in) throws IOException {
100 		clientId = in.readUTF();
101 		topicFilter = in.readUTF();
102 
103 		int rawInt = in.readInt();
104 		qos = rawInt != Integer.MIN_VALUE ? MqttQoS.valueOf(rawInt) : null;
105 	}
106 
107 	@Override
108 	public String toString() {
109 		return new StringBuilder().append("clientId=").append(clientId).append(", topicFilter=").append(topicFilter)
110 				.append(", qos=").append(topicFilter).toString();
111 	}
112 }