1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.mqtt;
18
19 import io.netty.buffer.ByteBuf;
20 import io.netty.handler.codec.DecoderResult;
21
22
23
24
25 public final class MqttMessageFactory {
26
27 public static MqttMessage newMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader, Object payload) {
28 switch (mqttFixedHeader.messageType()) {
29 case CONNECT :
30 return new MqttConnectMessage(
31 mqttFixedHeader,
32 (MqttConnectVariableHeader) variableHeader,
33 (MqttConnectPayload) payload);
34
35 case CONNACK:
36 return new MqttConnAckMessage(mqttFixedHeader, (MqttConnAckVariableHeader) variableHeader);
37
38 case SUBSCRIBE:
39 return new MqttSubscribeMessage(
40 mqttFixedHeader,
41 (MqttMessageIdVariableHeader) variableHeader,
42 (MqttSubscribePayload) payload);
43
44 case SUBACK:
45 return new MqttSubAckMessage(
46 mqttFixedHeader,
47 (MqttMessageIdVariableHeader) variableHeader,
48 (MqttSubAckPayload) payload);
49
50 case UNSUBACK:
51 return new MqttUnsubAckMessage(
52 mqttFixedHeader,
53 (MqttMessageIdVariableHeader) variableHeader,
54 (MqttUnsubAckPayload) payload);
55
56 case UNSUBSCRIBE:
57 return new MqttUnsubscribeMessage(
58 mqttFixedHeader,
59 (MqttMessageIdVariableHeader) variableHeader,
60 (MqttUnsubscribePayload) payload);
61
62 case PUBLISH:
63 return new MqttPublishMessage(
64 mqttFixedHeader,
65 (MqttPublishVariableHeader) variableHeader,
66 (ByteBuf) payload);
67
68 case PUBACK:
69
70 return new MqttPubAckMessage(mqttFixedHeader, (MqttMessageIdVariableHeader) variableHeader);
71 case PUBREC:
72 case PUBREL:
73 case PUBCOMP:
74
75 return new MqttMessage(mqttFixedHeader, variableHeader);
76
77 case PINGREQ:
78 case PINGRESP:
79 return new MqttMessage(mqttFixedHeader);
80
81 case DISCONNECT:
82 case AUTH:
83
84 return new MqttMessage(mqttFixedHeader,
85 variableHeader);
86
87 default:
88 throw new IllegalArgumentException("unknown message type: " + mqttFixedHeader.messageType());
89 }
90 }
91
92 public static MqttMessage newInvalidMessage(Throwable cause) {
93 return new MqttMessage(null, null, null, DecoderResult.failure(cause));
94 }
95
96 public static MqttMessage newInvalidMessage(MqttFixedHeader mqttFixedHeader, Object variableHeader,
97 Throwable cause) {
98 return new MqttMessage(mqttFixedHeader, variableHeader, null, DecoderResult.failure(cause));
99 }
100
101 private MqttMessageFactory() { }
102 }