Конфигурационный файл
This commit is contained in:
8
pom.xml
8
pom.xml
@@ -40,5 +40,13 @@
|
||||
<artifactId>jakarta.persistence-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Source: https://mvnrepository.com/artifact/org.yaml/snakeyaml -->
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
12
server.yml
Normal file
12
server.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
#Подключение к БД
|
||||
database:
|
||||
host: db
|
||||
user: your_user
|
||||
password: your_password
|
||||
db: your_database
|
||||
#CDN (транспортные) серверы
|
||||
transport_servers:
|
||||
- https://cdn.rosetta-im.com
|
||||
#Серверы обновлений
|
||||
update_servers:
|
||||
- https://update.rosetta-im.com
|
||||
@@ -2,6 +2,7 @@ package com.rosetta.im;
|
||||
|
||||
import com.rosetta.im.client.ClientManager;
|
||||
import com.rosetta.im.client.OnlineManager;
|
||||
import com.rosetta.im.config.Configuration;
|
||||
import com.rosetta.im.event.EventManager;
|
||||
import com.rosetta.im.executors.Executor0Handshake;
|
||||
import com.rosetta.im.executors.Executor11Typeing;
|
||||
@@ -53,6 +54,7 @@ public class Boot {
|
||||
private ServerAdapter serverAdapter;
|
||||
private ClientManager clientManager;
|
||||
private OnlineManager onlineManager;
|
||||
private Configuration configuration;
|
||||
|
||||
public Boot() {
|
||||
this.packetManager = new PacketManager();
|
||||
@@ -65,6 +67,7 @@ public class Boot {
|
||||
30
|
||||
), packetManager, this.serverAdapter);
|
||||
this.clientManager = new ClientManager(server);
|
||||
this.configuration = new Configuration("server.yml");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,12 +107,19 @@ public class Boot {
|
||||
* @return Boot
|
||||
*/
|
||||
public Boot bootstrap() {
|
||||
this.server.start();
|
||||
this.registerAllPackets();
|
||||
this.registerAllExecutors();
|
||||
this.registerAllEvents();
|
||||
this.printBootMessage();
|
||||
return this;
|
||||
try{
|
||||
this.configuration.loadConfiguration();
|
||||
this.server.start();
|
||||
this.registerAllPackets();
|
||||
this.registerAllExecutors();
|
||||
this.registerAllEvents();
|
||||
this.printBootMessage();
|
||||
return this;
|
||||
}catch(Exception e){
|
||||
this.logger.error("Booting error, stack trace:");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void registerAllEvents() {
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.rosetta.im;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
public static final int PROTOCOL_VERSION = 1;
|
||||
|
||||
}
|
||||
41
src/main/java/com/rosetta/im/config/Configuration.java
Normal file
41
src/main/java/com/rosetta/im/config/Configuration.java
Normal file
@@ -0,0 +1,41 @@
|
||||
package com.rosetta.im.config;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.yaml.snakeyaml.LoaderOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import com.rosetta.im.exception.ConfigurationException;
|
||||
|
||||
public class Configuration {
|
||||
|
||||
private String serverConfigName;
|
||||
|
||||
public Configuration(String serverConfigName) {
|
||||
this.serverConfigName = serverConfigName;
|
||||
}
|
||||
|
||||
public ServerConfiguration loadConfiguration() throws ConfigurationException, Exception {
|
||||
if(!Files.exists(Paths.get(this.serverConfigName))){
|
||||
/**
|
||||
* Файл конфигурации с переданным названием не найден
|
||||
*/
|
||||
throw new ConfigurationException(this.serverConfigName + " is missing");
|
||||
}
|
||||
|
||||
LoaderOptions options = new LoaderOptions();
|
||||
Yaml yaml = new Yaml(options);
|
||||
|
||||
InputStream inputStream = Files.newInputStream(Paths.get(this.serverConfigName));
|
||||
/**
|
||||
* SnakeYAML автоматически загрузит обьект ServerConfiguration через рефлексию
|
||||
* обратите внимание название полей в ServerConfiguration должны совпадать с названиями в
|
||||
* server.yml
|
||||
*/
|
||||
ServerConfiguration serverConfiguration = yaml.loadAs(inputStream, ServerConfiguration.class);
|
||||
return serverConfiguration;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.rosetta.im.config;
|
||||
|
||||
public class DatabaseConfiguration {
|
||||
|
||||
public String host;
|
||||
public String user;
|
||||
public String password;
|
||||
public String db;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(String user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getDb() {
|
||||
return db;
|
||||
}
|
||||
|
||||
public void setDb(String db) {
|
||||
this.db = db;
|
||||
}
|
||||
|
||||
}
|
||||
31
src/main/java/com/rosetta/im/config/ServerConfiguration.java
Normal file
31
src/main/java/com/rosetta/im/config/ServerConfiguration.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package com.rosetta.im.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Этот файл отвечает за конфигурацию которая хранится в файле server.yml
|
||||
*/
|
||||
public class ServerConfiguration {
|
||||
|
||||
public DatabaseConfiguration database;
|
||||
|
||||
public List<String> cdn_servers;
|
||||
|
||||
public DatabaseConfiguration getDatabase() {
|
||||
return database;
|
||||
}
|
||||
|
||||
public void setDatabase(DatabaseConfiguration database) {
|
||||
this.database = database;
|
||||
}
|
||||
|
||||
public List<String> getCdnServers() {
|
||||
return cdn_servers;
|
||||
}
|
||||
|
||||
public void setCdnServers(List<String> cdn_servers) {
|
||||
this.cdn_servers = cdn_servers;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.rosetta.im.exception;
|
||||
|
||||
/**
|
||||
* Выбрасывается когда файл конфигурации не найден
|
||||
*/
|
||||
public class ConfigurationException extends Exception {
|
||||
|
||||
public ConfigurationException(String message){
|
||||
super(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.rosetta.im.executors;
|
||||
|
||||
import com.rosetta.im.Configuration;
|
||||
import com.rosetta.im.Failures;
|
||||
import com.rosetta.im.client.ClientManager;
|
||||
import com.rosetta.im.client.tags.ECIAuthentificate;
|
||||
@@ -65,7 +64,7 @@ public class Executor0Handshake extends PacketExecutor<Packet0Handshake> {
|
||||
/**
|
||||
* Проверяем корректность версии протокола
|
||||
*/
|
||||
if(protocolVersion != Configuration.PROTOCOL_VERSION) {
|
||||
if(protocolVersion != 1) {
|
||||
client.disconnect(Failures.UNSUPPORTED_PROTOCOL);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user