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.message;
18  
19  import java.io.IOException;
20  import java.util.Date;
21  
22  import com.fasterxml.jackson.annotation.JsonIgnore;
23  import com.fasterxml.jackson.annotation.JsonProperty;
24  import com.hazelcast.nio.ObjectDataInput;
25  import com.hazelcast.nio.ObjectDataOutput;
26  
27  import io.netty.handler.codec.mqtt.MqttQoS;
28  import net.anyflow.lannister.serialization.SerializableFactory;
29  
30  public class OutboundMessageStatus extends MessageStatus {
31  	public static final OutboundMessageStatuses NEXUS = new OutboundMessageStatuses();
32  	public static final int ID = 3;
33  
34  	@JsonProperty
35  	private Status status;
36  	@JsonProperty
37  	private MqttQoS qos;
38  
39  	public OutboundMessageStatus() { // just for Serialization
40  	}
41  
42  	public OutboundMessageStatus(String messageKey, String clientId, int messageId, String topicName, Status status,
43  			MqttQoS qos) {
44  		super(messageKey, clientId, messageId, topicName);
45  
46  		this.status = status;
47  		this.qos = qos;
48  	}
49  
50  	@Override
51  	public String key() {
52  		return OutboundMessageStatuses.key(messageId(), clientId());
53  	}
54  
55  	public Status status() {
56  		return status;
57  	}
58  
59  	public void status(Status status) {
60  		this.status = status;
61  		super.updateTime = new Date();
62  	}
63  
64  	public MqttQoS qos() {
65  		return qos;
66  	}
67  
68  	@JsonIgnore
69  	@Override
70  	public int getFactoryId() {
71  		return SerializableFactory.ID;
72  	}
73  
74  	@JsonIgnore
75  	@Override
76  	public int getId() {
77  		return ID;
78  	}
79  
80  	@Override
81  	public void writeData(ObjectDataOutput out) throws IOException {
82  		super.writeData(out);
83  
84  		out.writeByte(status != null ? status.value() : Byte.MIN_VALUE);
85  		out.writeInt(qos != null ? qos.value() : Byte.MIN_VALUE);
86  	}
87  
88  	@Override
89  	public void readData(ObjectDataInput in) throws IOException {
90  		super.readData(in);
91  
92  		Byte rawByte = in.readByte();
93  		status = rawByte != Byte.MIN_VALUE ? Status.valueOf(rawByte) : null;
94  
95  		int rawInt = in.readInt();
96  		qos = rawInt != Byte.MIN_VALUE ? MqttQoS.valueOf(rawInt) : null;
97  	}
98  
99  	public enum Status {
100 		TO_PUBLISH((byte) 0),
101 		PUBLISHED((byte) 1),
102 		PUBRECED((byte) 2);
103 
104 		private byte value;
105 
106 		private Status(byte value) {
107 			this.value = value;
108 		}
109 
110 		public byte value() {
111 			return value;
112 		}
113 
114 		public static Status valueOf(byte value) {
115 			for (Status q : values()) {
116 				if (q.value == value) { return q; }
117 			}
118 			throw new IllegalArgumentException("Invalid SenderTargetStatus: " + value);
119 		}
120 	}
121 }