1 package org.sim0mq.message.modelcontroller;
2
3 import org.djutils.exceptions.Throw;
4 import org.djutils.serialization.SerializationException;
5 import org.sim0mq.Sim0MQException;
6 import org.sim0mq.message.MessageStatus;
7 import org.sim0mq.message.Sim0MQReply;
8 import org.sim0mq.message.SimulationMessage;
9
10
11
12
13
14
15
16
17
18
19 public class MC1StatusMessage extends Sim0MQReply
20 {
21
22 private final String status;
23
24
25 private final String error;
26
27
28 private static final String MESSAGETYPE = "MC.1";
29
30
31 private static final long serialVersionUID = 20170422L;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public MC1StatusMessage(final Object simulationRunId, final Object senderId, final Object receiverId, final long messageId,
49 final long uniqueId, final String status, final String error) throws Sim0MQException, NullPointerException
50 {
51 super(simulationRunId, senderId, receiverId, MESSAGETYPE, messageId, MessageStatus.NEW, uniqueId);
52 Throw.whenNull(status, "status cannot be null");
53 Throw.whenNull(error, "error cannot be null");
54
55 Throw.when(status.isEmpty(), Sim0MQException.class, "status cannot be empty");
56 Throw.when(!status.equals("started") && !status.equals("running") && !status.equals("ended") && !status.equals("error"),
57 Sim0MQException.class, "status should be one of 'started', 'running', 'ended', 'error'");
58
59 this.status = status;
60 this.error = error;
61 }
62
63
64
65
66 public final String getStatus()
67 {
68 return this.status;
69 }
70
71
72
73
74 public final String getError()
75 {
76 return this.error;
77 }
78
79
80
81
82 public static final String getMessageType()
83 {
84 return MESSAGETYPE;
85 }
86
87
88 @Override
89 public short getNumberOfPayloadFields()
90 {
91 return 3;
92 }
93
94
95 @Override
96 public Object[] createObjectArray()
97 {
98 return new Object[] {getMagicNumber(), getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
99 getMessageId(), getMessageStatus(), getNumberOfPayloadFields(), getReplyToId(), this.status, this.error};
100 }
101
102
103 @Override
104 public byte[] createByteArray() throws Sim0MQException, SerializationException
105 {
106 return SimulationMessage.encodeUTF8(getSimulationRunId(), getSenderId(), getReceiverId(), getMessageTypeId(),
107 getMessageId(), getMessageStatus(), getReplyToId(), this.status, this.error);
108 }
109
110
111
112
113
114
115
116
117 public static MC1StatusMessage createMessage(final Object[] fields, final Object intendedReceiverId) throws Sim0MQException
118 {
119 check(fields, 3, MESSAGETYPE, intendedReceiverId);
120 return new MC1StatusMessage(fields[1], fields[2], fields[3], ((Long) fields[5]).longValue(),
121 ((Long) fields[8]).longValue(), fields[9].toString(), fields[10].toString());
122 }
123
124
125
126
127
128
129
130
131
132
133
134 public static class Builder extends Sim0MQReply.Builder<MC1StatusMessage.Builder>
135 {
136
137 private String status;
138
139
140 private String error;
141
142
143
144
145 public Builder()
146 {
147
148 }
149
150
151
152
153
154 public final Builder setStatus(final String newStatus)
155 {
156 this.status = newStatus;
157 return this;
158 }
159
160
161
162
163
164 public final Builder setError(final String newError)
165 {
166 this.error = newError;
167 return this;
168 }
169
170
171 @Override
172 public MC1StatusMessage build() throws Sim0MQException, NullPointerException
173 {
174 return new MC1StatusMessage(this.simulationRunId, this.senderId, this.receiverId, this.messageId, this.replyToId,
175 this.status, this.error);
176 }
177
178 }
179 }