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 net.anyflow.lannister.serialization.SerializableFactory;
28  
29  public class InboundMessageStatus extends MessageStatus {
30  	public static final InboundMessageStatuses NEXUS = new InboundMessageStatuses();
31  	public static final int ID = 2;
32  
33  	@JsonProperty
34  	private Status status;
35  
36  	public InboundMessageStatus() { // just for Serialization
37  	}
38  
39  	public InboundMessageStatus(String messageKey, String clientId, int messageId, String topicName, Status status) {
40  		super(messageKey, clientId, messageId, topicName);
41  
42  		this.status = status;
43  	}
44  
45  	@Override
46  	public String key() {
47  		return InboundMessageStatuses.key(messageId(), clientId());
48  	}
49  
50  	public Status status() {
51  		return status;
52  	}
53  
54  	public void status(Status status) {
55  		this.status = status;
56  		super.updateTime = new Date();
57  	}
58  
59  	@JsonIgnore
60  	@Override
61  	public int getFactoryId() {
62  		return SerializableFactory.ID;
63  	}
64  
65  	@JsonIgnore
66  	@Override
67  	public int getId() {
68  		return ID;
69  	}
70  
71  	@Override
72  	public void writeData(ObjectDataOutput out) throws IOException {
73  		super.writeData(out);
74  
75  		out.writeByte(status != null ? status.value() : Byte.MIN_VALUE);
76  	}
77  
78  	@Override
79  	public void readData(ObjectDataInput in) throws IOException {
80  		super.readData(in);
81  
82  		byte rawByte = in.readByte();
83  		status = rawByte != Byte.MIN_VALUE ? Status.valueOf(rawByte) : null;
84  	}
85  
86  	public enum Status {
87  		RECEIVED((byte) 0),
88  		PUBRECED((byte) 1);
89  
90  		private byte value;
91  
92  		private Status(byte value) {
93  			this.value = value;
94  		}
95  
96  		public byte value() {
97  			return value;
98  		}
99  
100 		public static Status valueOf(byte value) {
101 			for (Status q : values()) {
102 				if (q.value == value) { return q; }
103 			}
104 			throw new IllegalArgumentException("Invalid ReceiverTargetStatus: " + value);
105 		}
106 	}
107 }