-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandServer.java
75 lines (65 loc) · 2.61 KB
/
CommandServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
* Java
*
* Copyright 2023 MicroEJ Corp. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be found with this software.
*/
package com.microej.kernel.green.localdeploy;
import java.io.IOException;
import java.util.logging.Level;
import com.microej.kernel.green.Main;
import com.microej.library.kernel.rcommand.local.DeviceInfoCommand;
import com.microej.library.kernel.rcommand.local.InstallCommand;
import com.microej.library.kernel.rcommand.local.ListCommand;
import com.microej.library.kernel.rcommand.local.ListCommandCommand;
import com.microej.library.kernel.rcommand.local.StartCommand;
import com.microej.library.kernel.rcommand.local.StopCommand;
import com.microej.library.kernel.rcommand.local.UninstallCommand;
import ej.rcommand.RemoteCommandManager;
import ej.rcommand.impl.DefaultRemoteCommandManager;
import ej.rcommand.serversocket.RemoteCommandServer;
/**
* CommandServerEntryPoint class is responsible for registering the listener, the logger and start the listener of
* remote commands.
*/
public class CommandServer {
private static final int COMMAND_SERVER_NB_MAX_CONNECTION = 2;
private static final int COMMAND_SERVER_PORT = 4000;
private static final String ADMIN_SERVER = "admin server";
private final RemoteCommandServer adminServer;
private final RemoteCommandManager commandManager;
/**
* Default constructor that registers the listeners, the logger and instantiates the RemoteCommandServer.
*/
public CommandServer() {
this.commandManager = new DefaultRemoteCommandManager();
this.commandManager.registerListener(new InstallCommand());
this.commandManager.registerListener(new DeviceInfoCommand());
this.commandManager.registerListener(new ListCommand());
this.commandManager.registerListener(new StartCommand());
this.commandManager.registerListener(new StopCommand());
this.commandManager.registerListener(new UninstallCommand());
this.commandManager.registerListener(new ListCommandCommand(this.commandManager));
this.adminServer = new RemoteCommandServer(this.commandManager, COMMAND_SERVER_PORT,
COMMAND_SERVER_NB_MAX_CONNECTION);
}
/**
* Starts the remote command server in a new thread.
*/
public synchronized void startServer() {
Main.LOGGER.info("Start the " + ADMIN_SERVER);
new Thread(this.adminServer, ADMIN_SERVER).start();
}
/**
* Stop the remote command server
*/
public synchronized void stopServer() {
Main.LOGGER.info("Stop the " + ADMIN_SERVER);
this.commandManager.stopAll();
try {
this.adminServer.stopRunning();
} catch (IOException e) {
Main.LOGGER.log(Level.INFO, ADMIN_SERVER, e);
}
}
}