mirror of
https://github.com/ProjectSWGCore/NGECore2.git
synced 2026-10-04 19:12:50 -04:00
NGECore2 Code
This commit is contained in:
@@ -0,0 +1,285 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2013 <Project SWG>
|
||||
*
|
||||
* This File is part of NGECore2.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Using NGEngine to work with NGECore2 is making a combined work based on NGEngine.
|
||||
* Therefore all terms and conditions of the GNU Lesser General Public License cover the combination.
|
||||
******************************************************************************/
|
||||
package main;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
|
||||
|
||||
import resources.objects.creature.CreatureObject;
|
||||
import services.CharacterService;
|
||||
import services.ConnectionService;
|
||||
import services.LoginService;
|
||||
import services.MapService;
|
||||
import services.ScriptService;
|
||||
import services.SimulationService;
|
||||
import services.TerrainService;
|
||||
import services.chat.ChatService;
|
||||
import services.command.CommandService;
|
||||
import services.object.ObjectService;
|
||||
import services.object.UpdateService;
|
||||
import engine.clientdata.ClientFileManager;
|
||||
import engine.clientdata.visitors.CrcStringTableVisitor;
|
||||
import engine.clientdata.visitors.DatatableVisitor;
|
||||
import engine.clientdata.visitors.LevelOfDetailVisitor;
|
||||
import engine.clientdata.visitors.MeshVisitor;
|
||||
import engine.clientdata.visitors.PortalVisitor;
|
||||
import engine.clientdata.visitors.TerrainVisitor;
|
||||
import engine.clientdata.visitors.WorldSnapshotVisitor;
|
||||
import engine.clientdata.visitors.WorldSnapshotVisitor.SnapshotChunk;
|
||||
import engine.clients.Client;
|
||||
import engine.resources.common.PHPBB3Auth;
|
||||
import engine.resources.config.Config;
|
||||
import engine.resources.config.DefaultConfig;
|
||||
import engine.resources.database.DatabaseConnection;
|
||||
import engine.resources.database.ObjectDatabase;
|
||||
import engine.resources.objects.SWGObject;
|
||||
import engine.resources.scene.Point3D;
|
||||
import engine.resources.scene.Quaternion;
|
||||
import engine.resources.service.NetworkDispatch;
|
||||
import engine.servers.MINAServer;
|
||||
import engine.servers.PingServer;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
public class NGECore {
|
||||
|
||||
public static boolean didServerCrash = false;
|
||||
|
||||
private Config config = null;
|
||||
|
||||
|
||||
private ConcurrentHashMap<Integer, Client> clients = new ConcurrentHashMap<Integer, Client>();
|
||||
|
||||
// Database
|
||||
|
||||
private DatabaseConnection databaseConnection = null;
|
||||
private DatabaseConnection databaseConnection2 = null;
|
||||
|
||||
// Services
|
||||
public LoginService loginService;
|
||||
public ConnectionService connectionService;
|
||||
public CommandService commandService;
|
||||
public CharacterService characterService;
|
||||
public ObjectService objectService;
|
||||
public MapService mapService;
|
||||
public UpdateService updateService;
|
||||
public TerrainService terrainService;
|
||||
public SimulationService simulationService;
|
||||
public ScriptService scriptService;
|
||||
public ChatService chatService;
|
||||
|
||||
|
||||
// Login Server
|
||||
public NetworkDispatch loginDispatch;
|
||||
private MINAServer loginServer;
|
||||
|
||||
// Zone Server
|
||||
public NetworkDispatch zoneDispatch;
|
||||
private MINAServer zoneServer;
|
||||
|
||||
private ObjectDatabase creatureODB;
|
||||
|
||||
private ObjectDatabase mailODB;
|
||||
|
||||
|
||||
public NGECore() {
|
||||
|
||||
}
|
||||
|
||||
public void start() {
|
||||
config = new Config();
|
||||
config.setFilePath("nge.cfg");
|
||||
if (!(config.loadConfigFile())) {
|
||||
config = DefaultConfig.getConfig();
|
||||
}
|
||||
|
||||
// Database
|
||||
databaseConnection = new DatabaseConnection();
|
||||
databaseConnection.connect(config.getString("DB.URL"), config.getString("DB.NAME"), config.getString("DB.USER"), config.getString("DB.PASS"), "postgresql");
|
||||
|
||||
databaseConnection2 = new DatabaseConnection();
|
||||
|
||||
creatureODB = new ObjectDatabase("creature", true, false, true);
|
||||
mailODB = new ObjectDatabase("mails", true, false, true);
|
||||
|
||||
// Services
|
||||
loginService = new LoginService(this);
|
||||
connectionService = new ConnectionService(this);
|
||||
characterService = new CharacterService(this);
|
||||
|
||||
objectService = new ObjectService(this);
|
||||
terrainService = new TerrainService(this);
|
||||
updateService = new UpdateService(this);
|
||||
mapService = new MapService(this);
|
||||
scriptService = new ScriptService(this);
|
||||
commandService = new CommandService(this);
|
||||
chatService = new ChatService(this);
|
||||
|
||||
// Ping Server
|
||||
try {
|
||||
PingServer pingServer = new PingServer(config.getInt("PING.PORT"));
|
||||
pingServer.bind();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Login Server
|
||||
loginDispatch = new NetworkDispatch(this, false);
|
||||
loginDispatch.addService(loginService);
|
||||
|
||||
loginServer = new MINAServer(loginDispatch, config.getInt("LOGIN.PORT"));
|
||||
loginServer.start();
|
||||
// Zone Server
|
||||
zoneDispatch = new NetworkDispatch(this, true);
|
||||
zoneDispatch.addService(connectionService);
|
||||
zoneDispatch.addService(characterService);
|
||||
zoneDispatch.addService(objectService);
|
||||
zoneDispatch.addService(commandService);
|
||||
zoneDispatch.addService(chatService);
|
||||
|
||||
zoneServer = new MINAServer(zoneDispatch, config.getInt("ZONE.PORT"));
|
||||
zoneServer.start();
|
||||
|
||||
// Planets
|
||||
terrainService.addPlanet(1, "tatooine", "terrain/tatooine.trn", true);
|
||||
|
||||
|
||||
terrainService.loadSnapShotObjects();
|
||||
simulationService = new SimulationService(this);
|
||||
zoneDispatch.addService(simulationService);
|
||||
|
||||
didServerCrash = false;
|
||||
System.out.println("Started Server.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void stop() {
|
||||
System.out.println("Stopping Servers and Connections.");
|
||||
databaseConnection.close();
|
||||
databaseConnection2.close();
|
||||
}
|
||||
|
||||
public void cleanUp() {
|
||||
System.out.println("Cleaning Up...");
|
||||
long memoryUsed = Runtime.getRuntime().freeMemory();
|
||||
System.out.println("Using " + memoryUsed + " bytes of memory.");
|
||||
|
||||
config = null;
|
||||
databaseConnection = null;
|
||||
databaseConnection2 = null;
|
||||
Runtime.getRuntime().gc();
|
||||
System.out.println("Cleaned Up " + (Runtime.getRuntime().freeMemory() - memoryUsed) + " bytes of memory.");
|
||||
}
|
||||
|
||||
public void restart() {
|
||||
stop();
|
||||
cleanUp();
|
||||
try {
|
||||
Thread.sleep(30000);
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
}
|
||||
start();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
NGECore core = new NGECore();
|
||||
|
||||
core.start();
|
||||
|
||||
do {
|
||||
if (didServerCrash) {
|
||||
core.restart();
|
||||
}
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
}
|
||||
} while (true);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* ---------- Getter methods for NGECore ----------
|
||||
*/
|
||||
public Config getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public DatabaseConnection getDatabase1() {
|
||||
return databaseConnection;
|
||||
}
|
||||
|
||||
public DatabaseConnection getDatabase2() {
|
||||
return databaseConnection2;
|
||||
}
|
||||
|
||||
public ObjectDatabase getCreatureODB() {
|
||||
return creatureODB;
|
||||
}
|
||||
|
||||
public ObjectDatabase getMailODB() {
|
||||
return mailODB;
|
||||
}
|
||||
|
||||
public int getActiveClients() {
|
||||
int connections = 0;
|
||||
for (Map.Entry<Integer, Client> c : clients.entrySet()) {
|
||||
if(c.getValue().getSession() != null) {
|
||||
if (c.getValue().getSession().isConnected()) {
|
||||
connections++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return connections;
|
||||
}
|
||||
|
||||
public Client getClient(int connectionID) {
|
||||
return clients.get(connectionID);
|
||||
}
|
||||
|
||||
public ConcurrentHashMap<Integer, Client> getActiveConnectionsMap() {
|
||||
return clients;
|
||||
}
|
||||
|
||||
/*
|
||||
* --------------- Other methods for services ---------------
|
||||
*/
|
||||
public void addClient(Integer connectionID, Client client) {
|
||||
clients.put(connectionID, client);
|
||||
}
|
||||
|
||||
public void removeClient(Integer connectionID) {
|
||||
clients.remove(connectionID);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user