Skip to content

Commit f5d4dbc

Browse files
committed
Add Cluster and Node starters, enhance command handling, and update dependencies
1 parent 61a194e commit f5d4dbc

11 files changed

Lines changed: 220 additions & 26 deletions

File tree

.idea/copilot.data.migration.ask.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,17 @@ repositories {
5555

5656
dependencies {
5757
testImplementation platform('org.junit:junit-bom:5.10.0')
58+
implementation platform('io.projectreactor:reactor-bom:2022.0.21')
59+
5860
testImplementation 'org.junit.jupiter:junit-jupiter'
5961
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
6062

6163
implementation 'com.google.code.gson:gson:2.10.1'
6264
implementation 'org.jline:jline:3.30.0'
6365
implementation 'io.projectreactor:reactor-core:3.8.1'
66+
implementation 'io.projectreactor.netty:reactor-netty-core'
67+
implementation 'io.projectreactor.netty:reactor-netty-http'
68+
implementation 'org.springframework.boot:spring-boot-starter-webflux:3.2.2'
6469
}
6570

6671
test {

src/main/java/io/nodelink/server/NodeLinkHelper.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
import org.jline.reader.*;
1111
import org.jline.terminal.Terminal;
1212
import org.jline.terminal.TerminalBuilder;
13+
import org.jline.utils.InfoCmp;
1314
import org.jline.utils.InfoCmp.Capability;
1415

15-
import java.nio.file.Paths;
16-
1716
import java.util.List;
1817

1918
public class NodeLinkHelper {
@@ -28,7 +27,11 @@ public class NodeLinkHelper {
2827
private static final String YELLOW = "\u001B[33m";
2928

3029

31-
private static String PRODUCT = "Post Production";
30+
private final String PRODUCT = "Post Production";
31+
32+
public synchronized String getPRODUCT() {
33+
return this.PRODUCT;
34+
}
3235

3336
private Terminal terminal;
3437

@@ -69,8 +72,6 @@ private void initTerminal() {
6972

7073
CommandDispatcher dispatcher = new CommandDispatcher(registry);
7174

72-
CommandLogics logics = new CommandLogics(dispatcher, terminal);
73-
7475
LineReader reader = LineReaderBuilder.builder()
7576
.terminal(terminal)
7677
.completer((lineReader, parsedLine, candidates) -> {
@@ -80,43 +81,62 @@ private void initTerminal() {
8081
candidates.add(new Candidate(s));
8182
}
8283
})
83-
.variable(LineReader.HISTORY_FILE, Paths.get("bin/history.txt"))
8484
.option(LineReader.Option.AUTO_FRESH_LINE, true)
8585
.build();
8686

87+
CommandLogics logics = new CommandLogics(dispatcher, reader, terminal);
88+
8789
fullClearAndRefresh(terminal);
8890

8991
while (true) {
90-
String prompt = GREEN + "Server" + RESET + "@" + YELLOW + "NodeLink" + RESET + "-(" + RED + STATUS + RESET + ")~" + WHITE + "$ " + RESET;
92+
String prompt = GREEN + "Server" + RESET + "@" + YELLOW + "NodeLink" + RESET + "-(" + RED + NodeLink.getHelper().getStatus() + RESET + ")~" + WHITE + "$ " + RESET;
9193

9294
try {
9395
String command = reader.readLine(prompt);
9496

95-
if (command == null || command.equalsIgnoreCase("exit" ) || command.equalsIgnoreCase("quit")) {
96-
System.exit(1);
97+
if (command == null || command.trim().isEmpty()) {
98+
continue;
9799
}
98100

99-
if (command.equalsIgnoreCase("clear")) {
100-
fullClearAndRefresh(terminal);
101-
continue;
101+
try {
102+
boolean handled = dispatcher.dispatch(command);
103+
104+
if (!handled) {
105+
terminal.writer().println("Commande inconnue : " + command);
106+
terminal.writer().println("\n");
107+
}
108+
} catch (Exception e) {
109+
terminal.writer().println("Erreur : La commande '" + command + "' n'est pas reconnue.");
110+
terminal.writer().println("\n");
102111
}
103112

104-
boolean handled = dispatcher.dispatch(command);
105-
if (!handled) {
106-
terminal.writer().println("Commande inconnue : " + command);
113+
terminal.writer().flush();
114+
115+
if (command.equalsIgnoreCase("exit") || command.equalsIgnoreCase("quit")) {
116+
terminal.writer().print("\u001B[r");
117+
terminal.writer().print("\u001B[2J\u001B[3J");
118+
terminal.puts(InfoCmp.Capability.cursor_address, 0, 0);
107119
terminal.flush();
120+
System.exit(0);
108121
}
109122

110123
} catch (UserInterruptException | EndOfFileException e) {
111-
System.exit(1);
124+
terminal.writer().print("\u001B[r");
125+
terminal.writer().print("\u001B[2J\u001B[3J");
126+
terminal.puts(InfoCmp.Capability.cursor_address, 0, 0);
127+
terminal.flush();
128+
System.exit(0);
129+
} catch (Exception e) {
130+
terminal.writer().println("Une erreur système est survenue : " + e.getMessage());
131+
terminal.writer().flush();
112132
}
113133
}
114134
} catch (Exception e) {
115135
e.printStackTrace();
116136
}
117137
}
118138

119-
private void fullClearAndRefresh(Terminal terminal) {
139+
public void fullClearAndRefresh(Terminal terminal) {
120140
terminal.writer().print("\u001B[r");
121141
terminal.writer().print("\u001B[2J\u001B[3J");
122142
terminal.puts(Capability.cursor_address, 0, 0);
@@ -167,6 +187,10 @@ private int getPlainTextLength(String s) {
167187
return s.replaceAll("\u001B\\[[;\\d]*m", "").length();
168188
}
169189

190+
public void displayHelpPage() {
191+
192+
}
193+
170194
private String LOGO() {
171195
return """
172196
\s
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.nodelink.server.app.cluster.api;
2+
3+
import org.springframework.boot.Banner;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.web.reactive.config.WebFluxConfigurer;
7+
import reactor.core.publisher.Mono;
8+
import reactor.core.scheduler.Schedulers;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@SpringBootApplication
14+
public class ClusterStarter implements WebFluxConfigurer {
15+
16+
private static final ClusterStarter INSTANCE = new ClusterStarter();
17+
18+
public static ClusterStarter getClusterStarter() {
19+
return INSTANCE;
20+
}
21+
22+
public Mono<Void> startCluster() {
23+
//connectLib.StoreAndRetrieve().put(connectLib.StoreAndRetrieve().IS_APP_RUNNING, true);
24+
return Mono.fromRunnable(() -> {
25+
SpringApplication app = new SpringApplication(ClusterStarter.class);
26+
app.setBannerMode(Banner.Mode.OFF);
27+
app.setLogStartupInfo(false);
28+
Map<String, Object> props = new HashMap<>();
29+
props.put("server.port", 8080);
30+
props.put("logging.level.root", "OFF");
31+
app.setDefaultProperties(props);
32+
app.run();
33+
}).subscribeOn(Schedulers.boundedElastic()).then();
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.nodelink.server.app.cluster.enums;
2+
3+
public enum DE_EU {
4+
NBG1_DE_EU_BONE("nbg1-de.eu.bone.nodelinkapp.xyz"),
5+
LOCALNODELINK_MASTER("localhost")
6+
;
7+
8+
DE_EU(String address) {}
9+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.nodelink.server.app.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Primary;
6+
7+
@Configuration
8+
public class ControllerConfig {
9+
// @Bean
10+
// @Primary
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.nodelink.server.app.node.api;
2+
3+
import org.springframework.boot.Banner;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.web.reactive.config.WebFluxConfigurer;
7+
import reactor.core.publisher.Mono;
8+
import reactor.core.scheduler.Schedulers;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@SpringBootApplication
14+
public class NodeStarter implements WebFluxConfigurer {
15+
16+
private static final NodeStarter INSTANCE = new NodeStarter();
17+
18+
public static NodeStarter getNodeStarter() {
19+
return INSTANCE;
20+
}
21+
22+
public Mono<Void> startNode() {
23+
//connectLib.StoreAndRetrieve().put(connectLib.StoreAndRetrieve().IS_APP_RUNNING, true);
24+
return Mono.fromRunnable(() -> {
25+
SpringApplication app = new SpringApplication(NodeStarter.class);
26+
app.setBannerMode(Banner.Mode.OFF);
27+
app.setLogStartupInfo(false);
28+
Map<String, Object> props = new HashMap<>();
29+
props.put("server.port", 8080);
30+
props.put("logging.level.root", "OFF");
31+
app.setDefaultProperties(props);
32+
app.run();
33+
}).subscribeOn(Schedulers.boundedElastic()).then();
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.nodelink.server.app.node.enums;
2+
3+
public enum DE_EU {
4+
NBG1_DE_EU_CLUSTER("nbg1-de.eu.cluster.nodelinkapp.xyz"),
5+
LOCALNODELINK_SERVER("localhost")
6+
;
7+
8+
DE_EU(String address) {}
9+
}

src/main/java/io/nodelink/server/command/CommandLogics.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,47 @@
22

33
import io.nodelink.server.NodeLink;
44
import io.nodelink.server.enums.CommandsEnum;
5+
import org.jline.reader.LineReader;
56
import org.jline.terminal.Terminal;
67

78
public class CommandLogics {
89

9-
public CommandLogics(CommandDispatcher dispatcher, Terminal terminal) {
10-
dispatcher.registerHandler(CommandsEnum.SERVICE_SET_CLUSTER, tokens -> {
11-
terminal.writer().println("Mode cluster activé");
10+
private void blankSpace(Terminal terminal) {
11+
terminal.writer().println("\n");
12+
}
13+
14+
public CommandLogics(CommandDispatcher dispatcher, LineReader reader, Terminal terminal) {
15+
16+
17+
dispatcher.registerHandler(CommandsEnum.CLEAR, tokens -> {
18+
NodeLink.getHelper().fullClearAndRefresh(terminal);
19+
});
20+
21+
dispatcher.registerHandler(CommandsEnum.HELP, tokens -> {
22+
//NodeLink.getHelper().displayHelp(terminal);
23+
});
1224

25+
dispatcher.registerHandler(CommandsEnum.SERVICE_SET_CLUSTER, tokens -> {
1326
NodeLink.getHelper().updateStatus("Cluster");
27+
28+
terminal.writer().println("Mode cluster activé");
29+
blankSpace(terminal);
1430
});
1531

1632
dispatcher.registerHandler(CommandsEnum.SERVICE_SET_BONE, tokens -> {
33+
NodeLink.getHelper().updateStatus("Bone");
34+
1735
terminal.writer().println("Mode bone activé");
36+
blankSpace(terminal);
37+
});
1838

19-
NodeLink.getHelper().updateStatus("Bone");
39+
dispatcher.registerHandler(CommandsEnum.SERVICE_MODE_STATUS, tokens -> {
40+
String STATUS = NodeLink.getHelper().getStatus();
41+
String PRODUCT = NodeLink.getHelper().getPRODUCT();
42+
43+
terminal.writer().println("Statut actuel : " + STATUS + " (" + PRODUCT + ")");
44+
terminal.writer().println("Opérationnel ? :" + " RED STATUS");
45+
blankSpace(terminal);
2046
});
2147
}
2248
}

src/main/java/io/nodelink/server/enums/CommandsEnum.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,57 @@
44
import io.nodelink.server.provider.CommandsProvider;
55

66
public enum CommandsEnum implements CommandsProvider {
7+
CLEAR {
8+
@Override
9+
public CommandNode getCommandNode() {
10+
CommandNode clear = new CommandNode("clear");
11+
12+
clear.setOwner(this);
13+
return clear;
14+
}
15+
},
16+
17+
HELP {
18+
@Override
19+
public CommandNode getCommandNode() {
20+
CommandNode help = new CommandNode("help");
21+
22+
help.setOwner(this);
23+
return help;
24+
}
25+
},
26+
727
SERVICE {
828
@Override
929
public CommandNode getCommandNode() {
1030
CommandNode service = new CommandNode("service");
1131
CommandNode set = new CommandNode("set");
32+
CommandNode mode = new CommandNode("mode");
1233

13-
set.addChild(new CommandNode("status"));
14-
set.addChild(new CommandNode("info"));
15-
34+
CommandNode info = new CommandNode("info");
35+
CommandNode status = new CommandNode("status");
1636

1737
service.addChild(set);
38+
set.addChild(mode);
39+
40+
mode.addChild(info);
41+
mode.addChild(status);
1842

1943
return service;
2044
}
2145
},
2246

47+
SERVICE_MODE_STATUS {
48+
@Override
49+
public CommandNode getCommandNode() {
50+
CommandNode root = new CommandNode("service");
51+
CommandNode mode = root.child("mode");
52+
CommandNode status = mode.child("status");
53+
status.setOwner(this);
54+
return root;
55+
}
56+
},
57+
2358
SERVICE_SET_BONE {
2459
@Override
2560
public CommandNode getCommandNode() {

0 commit comments

Comments
 (0)