Skip to content

Commit 30565fe

Browse files
committed
feat: Add scaffold
1 parent 7ac949e commit 30565fe

File tree

7 files changed

+1343
-0
lines changed

7 files changed

+1343
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
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 com.hivemq.protocols.fsm;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
21+
import java.util.function.Function;
22+
23+
public enum ProtocolAdapterConnectionState {
24+
Closed(context -> 0),
25+
Closing(context -> 0),
26+
Connected(context -> 0),
27+
Connecting(context -> 0),
28+
Disconnected(context -> 0),
29+
Disconnecting(context -> 0),
30+
Error(context -> 0),
31+
ErrorClosing(context -> 0),
32+
;
33+
34+
private final @NotNull Function<Object, Integer> transitionFunction;
35+
36+
ProtocolAdapterConnectionState(@NotNull final Function<Object, Integer> transitionFunction) {
37+
this.transitionFunction = transitionFunction;
38+
}
39+
40+
public @NotNull Integer transition(
41+
final @NotNull ProtocolAdapterConnectionState targetState,
42+
final @NotNull Object context) {
43+
return transitionFunction.apply(context);
44+
}
45+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
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 com.hivemq.protocols.fsm;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
21+
import java.util.function.BiFunction;
22+
23+
public enum ProtocolAdapterState {
24+
Starting(ProtocolAdapterState::transitionFromStarting),
25+
Started(ProtocolAdapterState::transitionFromStarted),
26+
Stopping(ProtocolAdapterState::transitionFromStopping),
27+
Stopped(ProtocolAdapterState::transitionFromStopped),
28+
Error(ProtocolAdapterState::transitionFromError),
29+
;
30+
31+
private final @NotNull BiFunction<ProtocolAdapterState, ProtocolAdapterWrapper, ProtocolAdapterTransitionResult>
32+
transitionFunction;
33+
34+
ProtocolAdapterState(@NotNull final BiFunction<ProtocolAdapterState, ProtocolAdapterWrapper, ProtocolAdapterTransitionResult> transitionFunction) {
35+
this.transitionFunction = transitionFunction;
36+
}
37+
38+
public static ProtocolAdapterTransitionResult transitionFromStarted(
39+
final @NotNull ProtocolAdapterState targetState,
40+
final @NotNull ProtocolAdapterWrapper wrapper) {
41+
switch (targetState) {
42+
case Starting:
43+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting);
44+
case Started:
45+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started);
46+
case Stopping:
47+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping);
48+
case Stopped:
49+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped);
50+
default:
51+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error);
52+
}
53+
}
54+
55+
public static ProtocolAdapterTransitionResult transitionFromStarting(
56+
final @NotNull ProtocolAdapterState targetState,
57+
final @NotNull ProtocolAdapterWrapper wrapper) {
58+
switch (targetState) {
59+
case Starting:
60+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting);
61+
case Started:
62+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started);
63+
case Stopping:
64+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping);
65+
case Stopped:
66+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped);
67+
default:
68+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error);
69+
}
70+
}
71+
72+
public static ProtocolAdapterTransitionResult transitionFromStopped(
73+
final @NotNull ProtocolAdapterState targetState,
74+
final @NotNull ProtocolAdapterWrapper wrapper) {
75+
switch (targetState) {
76+
case Starting:
77+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting);
78+
case Started:
79+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started);
80+
case Stopping:
81+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping);
82+
case Stopped:
83+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped);
84+
default:
85+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error);
86+
}
87+
}
88+
89+
public static ProtocolAdapterTransitionResult transitionFromStopping(
90+
final @NotNull ProtocolAdapterState targetState,
91+
final @NotNull ProtocolAdapterWrapper wrapper) {
92+
switch (targetState) {
93+
case Starting:
94+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting);
95+
case Started:
96+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started);
97+
case Stopping:
98+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping);
99+
case Stopped:
100+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped);
101+
default:
102+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error);
103+
}
104+
}
105+
106+
public static ProtocolAdapterTransitionResult transitionFromError(
107+
final @NotNull ProtocolAdapterState targetState,
108+
final @NotNull ProtocolAdapterWrapper wrapper) {
109+
switch (targetState) {
110+
case Starting:
111+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Starting);
112+
case Started:
113+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Started);
114+
case Stopping:
115+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopping);
116+
case Stopped:
117+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Stopped);
118+
default:
119+
return new ProtocolAdapterTransitionResult(ProtocolAdapterState.Error);
120+
}
121+
}
122+
123+
public @NotNull ProtocolAdapterTransitionResult transition(
124+
final @NotNull ProtocolAdapterState targetState,
125+
final @NotNull ProtocolAdapterWrapper wrapper) {
126+
return transitionFunction.apply(targetState, wrapper);
127+
}
128+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
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 com.hivemq.protocols.fsm;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public record ProtocolAdapterTransitionResult(ProtocolAdapterState state, ProtocolAdapterTransitionStatus status,
22+
String message, Throwable error) {
23+
public ProtocolAdapterTransitionResult(final @NotNull ProtocolAdapterState state) {
24+
this(state, ProtocolAdapterTransitionStatus.Success, null, null);
25+
}
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
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 com.hivemq.protocols.fsm;
18+
19+
public enum ProtocolAdapterTransitionStatus {
20+
Success,
21+
Failure,
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2019-present HiveMQ GmbH
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 com.hivemq.protocols.fsm;
18+
19+
import org.jetbrains.annotations.NotNull;
20+
21+
public class ProtocolAdapterWrapper {
22+
protected @NotNull ProtocolAdapterState state;
23+
protected @NotNull ProtocolAdapterConnectionState connectionState;
24+
25+
public ProtocolAdapterWrapper() {
26+
this.state = ProtocolAdapterState.Stopped;
27+
this.connectionState = ProtocolAdapterConnectionState.Closed;
28+
}
29+
30+
public @NotNull ProtocolAdapterState getState() {
31+
return state;
32+
}
33+
34+
public @NotNull ProtocolAdapterConnectionState getConnectionState() {
35+
return connectionState;
36+
}
37+
38+
public @NotNull ProtocolAdapterTransitionStatus transitionTo(final @NotNull ProtocolAdapterState protocolAdapterState) {
39+
final ProtocolAdapterTransitionResult result = state.transition(protocolAdapterState, this);
40+
state = result.state();
41+
// Handle error (logging, throwing exception, etc.)
42+
switch (result.status()) {
43+
default -> {
44+
// TODO
45+
}
46+
}
47+
return result.status();
48+
}
49+
}

0 commit comments

Comments
 (0)