Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
681b6feef1 | ||
|
|
d3315900b9 |
@@ -76,43 +76,37 @@ public final class NenjimHubImpl implements NenjimHub {
|
|||||||
"NenjimHub start has already been attempted");
|
"NenjimHub start has already been attempted");
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<NenjimComponentId, ProcessConstructor> processConstructors =
|
Map<NenjimComponentId, ProcessConstructor> processConstructors = createProcessConstructorCatalogue();
|
||||||
createProcessConstructorCatalogue();
|
Set<NenjimComponentId> knownProcessIds = selectableProcessIds(processConstructors);
|
||||||
NenjimHubConfiguration configuration =
|
|
||||||
NenjimHubConfigurationParser.parse(
|
|
||||||
HUB_CONFIGURATION_PATH,
|
|
||||||
selectableProcessIds(processConstructors),
|
|
||||||
REGISTRY_SERVICE_ID,
|
|
||||||
JOURNAL_SERVICE_ID);
|
|
||||||
|
|
||||||
log.info("Initializing Nenjim Registry...");
|
log.debug("Parsing Nenjim configuration...");
|
||||||
NenjimRegistryAssembly registryAssembly =
|
configuration = NenjimHubConfigurationParser.parse(
|
||||||
NenjimRegistryAssembly.create();
|
HUB_CONFIGURATION_PATH,
|
||||||
registryService =
|
knownProcessIds,
|
||||||
registryAssembly.registryService();
|
REGISTRY_SERVICE_ID,
|
||||||
|
JOURNAL_SERVICE_ID
|
||||||
|
);
|
||||||
|
|
||||||
|
log.debug("Initializing Nenjim Registry...");
|
||||||
|
NenjimRegistryAssembly registryAssembly = NenjimRegistryAssembly.create();
|
||||||
|
registryService = registryAssembly.registryService();
|
||||||
NenjimProcess registryProcess = registryAssembly.registryProcess();
|
NenjimProcess registryProcess = registryAssembly.registryProcess();
|
||||||
registryServiceManager = registryAssembly.registryServiceManager();
|
registryServiceManager = registryAssembly.registryServiceManager();
|
||||||
|
|
||||||
registryProcess.start();
|
registryProcess.start();
|
||||||
registryServiceManager.registerComponent(
|
registryServiceManager.registerComponent(
|
||||||
REGISTRY_SERVICE_ID,
|
REGISTRY_SERVICE_ID,
|
||||||
registryService);
|
registryService
|
||||||
|
);
|
||||||
recordStartedProcess(REGISTRY_SERVICE_ID);
|
recordStartedProcess(REGISTRY_SERVICE_ID);
|
||||||
|
|
||||||
log.info("Initializing passive Nenjim components...");
|
log.debug("Initializing passive Nenjim components...");
|
||||||
initializeAndRegisterPassiveComponents(registryServiceManager);
|
initializeAndRegisterPassiveComponents(registryServiceManager);
|
||||||
|
|
||||||
log.info("Constructing and starting configured Nenjim processes...");
|
log.info("Auto-starting configured Nenjim processes...");
|
||||||
for (NenjimComponentId componentId
|
autoStartProcesses(processConstructors);
|
||||||
: configuration.autoStartProcessIds().subList(
|
|
||||||
1,
|
|
||||||
configuration.autoStartProcessIds().size())) {
|
|
||||||
constructRegisterAndStartProcess(
|
|
||||||
componentId,
|
|
||||||
processConstructors);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("Done - Now online!");
|
log.info("Done - Now online!");
|
||||||
|
|
||||||
waitForAndShutdown();
|
waitForAndShutdown();
|
||||||
}
|
}
|
||||||
@@ -123,6 +117,20 @@ public final class NenjimHubImpl implements NenjimHub {
|
|||||||
return List.copyOf(startedProcessIds);
|
return List.copyOf(startedProcessIds);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void autoStartProcesses(Map<NenjimComponentId, ProcessConstructor> processConstructors)
|
||||||
|
throws Exception {
|
||||||
|
List<NenjimComponentId> autoStartProcessIds = configuration
|
||||||
|
.autoStartProcessIds()
|
||||||
|
.subList(1, configuration.autoStartProcessIds().size());
|
||||||
|
|
||||||
|
for (NenjimComponentId componentId : autoStartProcessIds) {
|
||||||
|
constructRegisterAndStartProcess(
|
||||||
|
componentId,
|
||||||
|
processConstructors
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private NenjimProcess constructJournalProcess() {
|
private NenjimProcess constructJournalProcess() {
|
||||||
JournalServiceAssembly journalAssembly =
|
JournalServiceAssembly journalAssembly =
|
||||||
JournalServiceAssembly.create(JOURNAL_ROOT);
|
JournalServiceAssembly.create(JOURNAL_ROOT);
|
||||||
@@ -132,12 +140,17 @@ public final class NenjimHubImpl implements NenjimHub {
|
|||||||
|
|
||||||
private void initializeAndRegisterPassiveComponents(
|
private void initializeAndRegisterPassiveComponents(
|
||||||
NenjimRegistryServiceManager registryServiceManager) {
|
NenjimRegistryServiceManager registryServiceManager) {
|
||||||
initializeBuildingBlocks(registryServiceManager);
|
log.debug("Initializing components...");
|
||||||
|
initializeComponents(registryServiceManager);
|
||||||
|
|
||||||
|
log.debug("Initializing price sources...");
|
||||||
initializePriceSources(registryServiceManager);
|
initializePriceSources(registryServiceManager);
|
||||||
|
|
||||||
|
log.debug("Initializing passive services...");
|
||||||
initializePassiveServices(registryServiceManager);
|
initializePassiveServices(registryServiceManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeBuildingBlocks(
|
private void initializeComponents(
|
||||||
NenjimRegistryServiceManager registryServiceManager) {
|
NenjimRegistryServiceManager registryServiceManager) {
|
||||||
objectCache = new ObjectCacheImpl();
|
objectCache = new ObjectCacheImpl();
|
||||||
registryServiceManager.registerComponent(OBJECT_CACHE_ID, objectCache);
|
registryServiceManager.registerComponent(OBJECT_CACHE_ID, objectCache);
|
||||||
@@ -359,33 +372,20 @@ public final class NenjimHubImpl implements NenjimHub {
|
|||||||
return Map.ofEntries(
|
return Map.ofEntries(
|
||||||
Map.entry(JOURNAL_SERVICE_ID, this::constructJournalProcess),
|
Map.entry(JOURNAL_SERVICE_ID, this::constructJournalProcess),
|
||||||
Map.entry(TICKER_SERVICE_ID, this::constructTickerProcess),
|
Map.entry(TICKER_SERVICE_ID, this::constructTickerProcess),
|
||||||
Map.entry(
|
Map.entry(JUPITER_PERPS_ALARM_ID, this::constructJupiterPerpsAlarmProcess),
|
||||||
JUPITER_PERPS_ALARM_ID,
|
Map.entry(EVELYN_SERVICE_PROD_ID, this::constructEvelynProdProcess),
|
||||||
this::constructJupiterPerpsAlarmProcess),
|
Map.entry(EVELYN_SERVICE_TEST_ID, this::constructEvelynTestProcess),
|
||||||
Map.entry(
|
Map.entry(EVELYN_IOU_BURNER_PROD_ID, this::constructEvelynIOUBurnerProdProcess),
|
||||||
EVELYN_SERVICE_PROD_ID,
|
Map.entry(JAVAFX_RUNTIME_SERVICE_ID, this::constructJavaFxRuntimeProcess),
|
||||||
this::constructEvelynProdProcess),
|
Map.entry(EVELYN_MISSION_CONTROL_ID, this::constructMissionControlProcess),
|
||||||
Map.entry(
|
Map.entry(PROCESS_MANAGER_ID, this::constructProcessManagerProcess)
|
||||||
EVELYN_SERVICE_TEST_ID,
|
);
|
||||||
this::constructEvelynTestProcess),
|
|
||||||
Map.entry(
|
|
||||||
EVELYN_IOU_BURNER_PROD_ID,
|
|
||||||
this::constructEvelynIOUBurnerProdProcess),
|
|
||||||
Map.entry(
|
|
||||||
JAVAFX_RUNTIME_SERVICE_ID,
|
|
||||||
this::constructJavaFxRuntimeProcess),
|
|
||||||
Map.entry(
|
|
||||||
EVELYN_MISSION_CONTROL_ID,
|
|
||||||
this::constructMissionControlProcess),
|
|
||||||
Map.entry(
|
|
||||||
PROCESS_MANAGER_ID,
|
|
||||||
this::constructProcessManagerProcess));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Set<NenjimComponentId> selectableProcessIds(
|
private static Set<NenjimComponentId> selectableProcessIds(
|
||||||
Map<NenjimComponentId, ProcessConstructor> processConstructors) {
|
Map<NenjimComponentId, ProcessConstructor> processConstructors
|
||||||
Set<NenjimComponentId> processIds =
|
) {
|
||||||
new HashSet<>(processConstructors.keySet());
|
Set<NenjimComponentId> processIds = new HashSet<>(processConstructors.keySet());
|
||||||
processIds.add(REGISTRY_SERVICE_ID);
|
processIds.add(REGISTRY_SERVICE_ID);
|
||||||
return Set.copyOf(processIds);
|
return Set.copyOf(processIds);
|
||||||
}
|
}
|
||||||
@@ -416,6 +416,8 @@ public final class NenjimHubImpl implements NenjimHub {
|
|||||||
+ "' is not registered as "
|
+ "' is not registered as "
|
||||||
+ NenjimProcess.class.getName());
|
+ NenjimProcess.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.info("Starting process '" + componentId.value() + "'...");
|
||||||
process.start();
|
process.start();
|
||||||
recordStartedProcess(componentId);
|
recordStartedProcess(componentId);
|
||||||
}
|
}
|
||||||
@@ -533,4 +535,7 @@ public final class NenjimHubImpl implements NenjimHub {
|
|||||||
private JavaFxRuntimeService javaFxRuntimeService;
|
private JavaFxRuntimeService javaFxRuntimeService;
|
||||||
private EvelynMissionControl missionControl;
|
private EvelynMissionControl missionControl;
|
||||||
private NenjimProcessManager processManager;
|
private NenjimProcessManager processManager;
|
||||||
|
|
||||||
|
// Internal state
|
||||||
|
private NenjimHubConfiguration configuration;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,167 +0,0 @@
|
|||||||
package crypto.r35157.cauldron;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.util.logging.*;
|
|
||||||
import java.io.IOException;
|
|
||||||
import com.sun.net.httpserver.HttpServer;
|
|
||||||
import com.sun.net.httpserver.HttpHandler;
|
|
||||||
import com.sun.net.httpserver.HttpExchange;
|
|
||||||
//import org.json.JSONObject;
|
|
||||||
|
|
||||||
public class TickerService {
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
configureLogging();
|
|
||||||
|
|
||||||
logger.info("Initializing TickerService:");
|
|
||||||
logger.info(" Starting embedded SQLLite...");
|
|
||||||
configureDatabase();
|
|
||||||
|
|
||||||
logger.info(" Validating schema...");
|
|
||||||
validateSchema();
|
|
||||||
|
|
||||||
logger.info(" Starting HTTP Service on port 8123...");
|
|
||||||
configureHTTPService();
|
|
||||||
|
|
||||||
logger.info("Ready to service!");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void validateSchema() throws SQLException {
|
|
||||||
boolean exist = doTableExist(dbConnection, "ticks");
|
|
||||||
if(exist == false) {
|
|
||||||
createTable(dbConnection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean doTableExist(Connection dbConnection, String tableName) throws SQLException {
|
|
||||||
String sql = "SELECT name FROM sqlite_master WHERE type='table' AND name=?";
|
|
||||||
|
|
||||||
PreparedStatement pstmt = dbConnection.prepareStatement(sql);
|
|
||||||
pstmt.setString(1, tableName);
|
|
||||||
ResultSet rs = pstmt.executeQuery();
|
|
||||||
|
|
||||||
return rs.next();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void configureLogging() {
|
|
||||||
try {
|
|
||||||
Formatter logFormatter = new CustomLogFormatter();
|
|
||||||
|
|
||||||
// Remove the default console handler
|
|
||||||
Logger rootLogger = Logger.getLogger("");
|
|
||||||
rootLogger.removeHandler(rootLogger.getHandlers()[0]);
|
|
||||||
|
|
||||||
// Add a custom console handler
|
|
||||||
ConsoleHandler consoleHandler = new ConsoleHandler();
|
|
||||||
consoleHandler.setLevel(Level.ALL);
|
|
||||||
consoleHandler.setFormatter(logFormatter);
|
|
||||||
rootLogger.addHandler(consoleHandler);
|
|
||||||
|
|
||||||
// Add a file handler
|
|
||||||
FileHandler fileHandler = new FileHandler("TickerService.log", true);
|
|
||||||
fileHandler.setLevel(Level.ALL);
|
|
||||||
fileHandler.setFormatter(logFormatter);
|
|
||||||
rootLogger.addHandler(fileHandler);
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.severe("Failed to set up logging: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void configureDatabase() throws SQLException {
|
|
||||||
dbConnection = DriverManager.getConnection(DB_URL);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void createTable(Connection dbConnection) throws SQLException {
|
|
||||||
String sql = "CREATE TABLE ticks (" +
|
|
||||||
"epoch INTEGER NOT NULL," +
|
|
||||||
"baseAssetId TEXT NOT NULL," +
|
|
||||||
"quoteAssetId TEXT NOT NULL," +
|
|
||||||
"price REAL NOT NULL," +
|
|
||||||
"PRIMARY KEY (epoch, baseAssetId, quoteAssetId)" +
|
|
||||||
");";
|
|
||||||
|
|
||||||
Statement stmt = dbConnection.createStatement();
|
|
||||||
stmt.execute(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void insertStockPrice(Connection conn, String symbol, double price) throws SQLException {
|
|
||||||
String sql = "INSERT INTO stock_prices(symbol, price) VALUES(?, ?)";
|
|
||||||
|
|
||||||
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
||||||
pstmt.setString(1, symbol);
|
|
||||||
pstmt.setDouble(2, price);
|
|
||||||
pstmt.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void retrieveStockPrices(Connection conn, String symbol) {
|
|
||||||
String sql = "SELECT id, symbol, price, timestamp FROM stock_prices WHERE symbol = ?";
|
|
||||||
|
|
||||||
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
|
||||||
pstmt.setString(1, symbol);
|
|
||||||
ResultSet rs = pstmt.executeQuery();
|
|
||||||
|
|
||||||
while (rs.next()) {
|
|
||||||
System.out.println(rs.getInt("id") + "\t" +
|
|
||||||
rs.getString("symbol") + "\t" +
|
|
||||||
rs.getDouble("price") + "\t" +
|
|
||||||
rs.getString("timestamp"));
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void configureHTTPService() throws Exception{
|
|
||||||
HttpServer server = HttpServer.create(new InetSocketAddress(PORT), BACKLOG);
|
|
||||||
server.createContext("/ticker", new MyHandler());
|
|
||||||
server.setExecutor(null); // Creates a default executor
|
|
||||||
server.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
static class CustomLogFormatter extends Formatter {
|
|
||||||
@Override
|
|
||||||
public String format(LogRecord record) {
|
|
||||||
return String.format("%1$s [%2$s] %3$s%n",
|
|
||||||
record.getLevel().getName(),
|
|
||||||
record.getLoggerName(),
|
|
||||||
record.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static class MyHandler implements HttpHandler {
|
|
||||||
@Override
|
|
||||||
public void handle(HttpExchange exchange) throws IOException {
|
|
||||||
// TODO: Reenable when import is fixed
|
|
||||||
/*
|
|
||||||
JSONObject json = new JSONObject();
|
|
||||||
json.put("message", "Hello, World!");
|
|
||||||
String response = json.toString();
|
|
||||||
|
|
||||||
exchange.getResponseHeaders().set("Content-type", RESPONSE_MIMETYPE);
|
|
||||||
exchange.sendResponseHeaders(HTTP_OK, response.length());
|
|
||||||
OutputStream os = exchange.getResponseBody();
|
|
||||||
os.write(response.getBytes());
|
|
||||||
|
|
||||||
os.close();
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(TickerService.class.getName());
|
|
||||||
private static Connection dbConnection;
|
|
||||||
|
|
||||||
private static final int PORT = 8123;
|
|
||||||
private static final int BACKLOG = 10;
|
|
||||||
private static final String RESPONSE_MIMETYPE = "application/json";
|
|
||||||
private static final int HTTP_OK = 200;
|
|
||||||
private static final String DB_URL = "jdbc:sqlite:ticker.db";
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user