Files
rosetta-wss/src/main/java/com/rosetta/im/Boot.java

205 lines
9.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.config.ServerConfiguration;
import com.rosetta.im.event.EventManager;
import com.rosetta.im.executors.Executor0Handshake;
import com.rosetta.im.executors.Executor10RequestUpdate;
import com.rosetta.im.executors.Executor11Typeing;
import com.rosetta.im.executors.Executor15RequestTransport;
import com.rosetta.im.executors.Executor16PushNotification;
import com.rosetta.im.executors.Executor1UserInfo;
import com.rosetta.im.executors.Executor24DeviceResolve;
import com.rosetta.im.executors.Executor3Search;
import com.rosetta.im.executors.Executor4OnlineState;
import com.rosetta.im.executors.Executor6Message;
import com.rosetta.im.executors.Executor7Read;
import com.rosetta.im.listeners.DeviceListListener;
import com.rosetta.im.listeners.HandshakeCompleteListener;
import com.rosetta.im.listeners.OnlineStatusDisconnectListener;
import com.rosetta.im.listeners.OnlineStatusHandshakeCompleteListener;
import com.rosetta.im.listeners.ServerStopListener;
import com.rosetta.im.logger.Logger;
import com.rosetta.im.logger.enums.Color;
import com.rosetta.im.logger.enums.LogLevel;
import com.rosetta.im.packet.Packet0Handshake;
import com.rosetta.im.packet.Packet10RequestUpdate;
import com.rosetta.im.packet.Packet11Typeing;
import com.rosetta.im.packet.Packet15RequestTransport;
import com.rosetta.im.packet.Packet16PushNotification;
import com.rosetta.im.packet.Packet17CreateGroup;
import com.rosetta.im.packet.Packet18GroupInfo;
import com.rosetta.im.packet.Packet19GroupInviteInfo;
import com.rosetta.im.packet.Packet1UserInfo;
import com.rosetta.im.packet.Packet20GroupJoin;
import com.rosetta.im.packet.Packet21GroupLeave;
import com.rosetta.im.packet.Packet22GroupBan;
import com.rosetta.im.packet.Packet23DeviceList;
import com.rosetta.im.packet.Packet24DeviceResolve;
import com.rosetta.im.packet.Packet2Result;
import com.rosetta.im.packet.Packet3Search;
import com.rosetta.im.packet.Packet4OnlineSubscribe;
import com.rosetta.im.packet.Packet5OnlineState;
import com.rosetta.im.packet.Packet6Message;
import com.rosetta.im.packet.Packet7Read;
import com.rosetta.im.packet.Packet8Delivery;
import com.rosetta.im.packet.Packet9DeviceNew;
import io.orprotocol.Server;
import io.orprotocol.Settings;
import io.orprotocol.packet.PacketManager;
/**
* Boot отвечает за инициализацию всех пакетов и их обработчиков,
* а так же событий приложения. Этот Boot отвечает за приложение, а не за протокол.
*
* Нужен он для того, чтобы все части приложения получали одинаковые ссылки на глобальные обьекты приложения, такие как менеджер пакетов,
* менеджер событий, менеджер клиентов и так далее
*/
public class Boot {
private PacketManager packetManager;
private EventManager eventManager;
private Logger logger;
private Server server;
private ServerAdapter serverAdapter;
private ClientManager clientManager;
private OnlineManager onlineManager;
private Configuration configuration;
private ServerConfiguration serverConfiguration;
/**
* Конструктор по умолчанию, использует порт 3000 для сервера
*/
public Boot() {
this(3000);
}
/**
* Инициализатор приложения
* @param port Порт, на котором будет работать сервер. Если не указан, то будет использован порт 3000
*/
public Boot(int port) {
this.packetManager = new PacketManager();
this.eventManager = new EventManager();
this.onlineManager = new OnlineManager();
this.logger = new Logger(LogLevel.INFO);
this.serverAdapter = new ServerAdapter(this.eventManager);
this.server = new Server(new Settings(
port,
30
), packetManager, this.serverAdapter);
this.clientManager = new ClientManager(server);
this.configuration = new Configuration("server.yml");
}
/**
* Получить менеджер пакетов приложения
* @return PacketManager
*/
public PacketManager getPacketManager() {
return this.packetManager;
}
/**
* Получить менеджер событий приложения
* @return EventManager
*/
public EventManager getEventManager() {
return this.eventManager;
}
/**
* Получить менеджера клиентов, нужно для того чтобы отправить пакет списку клиентов например
* @return менеджер клиентов
*/
public ClientManager getClientManager() {
return this.clientManager;
}
/**
* Получить логгер приложения
* @return Logger
*/
public Logger getLogger() {
return this.logger;
}
/**
* Запуск сервера, регистрация пакетов, обработчиков, событий приложения
* @return Boot
*/
public Boot bootstrap() {
try{
this.serverConfiguration = this.configuration.loadConfiguration();
this.server.start();
this.registerAllPackets();
this.registerAllExecutors();
this.registerAllEvents();
this.printBootMessage();
return this;
}catch(Exception e){
this.logger.error(Color.RED + "Booting error, stack trace:");
e.printStackTrace();
return null;
}
}
private void registerAllEvents() {
this.eventManager.registerListener(new ServerStopListener(this.logger));
this.eventManager.registerListener(new HandshakeCompleteListener());
this.eventManager.registerListener(new OnlineStatusHandshakeCompleteListener(this.onlineManager));
this.eventManager.registerListener(new OnlineStatusDisconnectListener(this.onlineManager));
this.eventManager.registerListener(new DeviceListListener(this.clientManager));
}
private void registerAllPackets() {
this.packetManager.registerPacket(0, Packet0Handshake.class);
this.packetManager.registerPacket(1, Packet1UserInfo.class);
this.packetManager.registerPacket(2, Packet2Result.class);
this.packetManager.registerPacket(3, Packet3Search.class);
this.packetManager.registerPacket(4, Packet4OnlineSubscribe.class);
this.packetManager.registerPacket(5, Packet5OnlineState.class);
this.packetManager.registerPacket(6, Packet6Message.class);
this.packetManager.registerPacket(7, Packet7Read.class);
this.packetManager.registerPacket(8, Packet8Delivery.class);
this.packetManager.registerPacket(9, Packet9DeviceNew.class);
this.packetManager.registerPacket(10, Packet10RequestUpdate.class);
this.packetManager.registerPacket(11, Packet11Typeing.class);
//RESERVED 12 PACKET AVATAR (unused)
//RESERVED 13 PACKET KERNEL UPDATE (unused)
//RESERVED 14 PACKET APP UPDATE (unused)
this.packetManager.registerPacket(15, Packet15RequestTransport.class);
this.packetManager.registerPacket(16, Packet16PushNotification.class);
this.packetManager.registerPacket(17, Packet17CreateGroup.class);
this.packetManager.registerPacket(18, Packet18GroupInfo.class);
this.packetManager.registerPacket(19, Packet19GroupInviteInfo.class);
this.packetManager.registerPacket(20, Packet20GroupJoin.class);
this.packetManager.registerPacket(21, Packet21GroupLeave.class);
this.packetManager.registerPacket(22, Packet22GroupBan.class);
this.packetManager.registerPacket(23, Packet23DeviceList.class);
this.packetManager.registerPacket(24, Packet24DeviceResolve.class);
}
private void registerAllExecutors() {
this.packetManager.registerExecutor(0, new Executor0Handshake(this.eventManager, this.clientManager, this.packetManager));
this.packetManager.registerExecutor(1, new Executor1UserInfo());
this.packetManager.registerExecutor(3, new Executor3Search(this.clientManager));
this.packetManager.registerExecutor(4, new Executor4OnlineState(this.onlineManager, this.clientManager));
this.packetManager.registerExecutor(6, new Executor6Message(this.clientManager, this.packetManager));
this.packetManager.registerExecutor(7, new Executor7Read(this.clientManager, this.packetManager));
this.packetManager.registerExecutor(10, new Executor10RequestUpdate(this.serverConfiguration));
this.packetManager.registerExecutor(11, new Executor11Typeing(this.clientManager, this.packetManager));
this.packetManager.registerExecutor(15, new Executor15RequestTransport(this.serverConfiguration));
this.packetManager.registerExecutor(16, new Executor16PushNotification());
this.packetManager.registerExecutor(24, new Executor24DeviceResolve(this.clientManager, this.eventManager));
}
private void printBootMessage() {
this.logger.log(LogLevel.INFO, Color.GREEN + "Boot successful complete");
}
}