Обработка выбора сервера обновлений
This commit is contained in:
@@ -6,6 +6,7 @@ import com.rosetta.im.config.Configuration;
|
|||||||
import com.rosetta.im.config.ServerConfiguration;
|
import com.rosetta.im.config.ServerConfiguration;
|
||||||
import com.rosetta.im.event.EventManager;
|
import com.rosetta.im.event.EventManager;
|
||||||
import com.rosetta.im.executors.Executor0Handshake;
|
import com.rosetta.im.executors.Executor0Handshake;
|
||||||
|
import com.rosetta.im.executors.Executor10RequestUpdate;
|
||||||
import com.rosetta.im.executors.Executor11Typeing;
|
import com.rosetta.im.executors.Executor11Typeing;
|
||||||
import com.rosetta.im.executors.Executor15RequestTransport;
|
import com.rosetta.im.executors.Executor15RequestTransport;
|
||||||
import com.rosetta.im.executors.Executor1UserInfo;
|
import com.rosetta.im.executors.Executor1UserInfo;
|
||||||
@@ -23,6 +24,7 @@ import com.rosetta.im.logger.Logger;
|
|||||||
import com.rosetta.im.logger.enums.Color;
|
import com.rosetta.im.logger.enums.Color;
|
||||||
import com.rosetta.im.logger.enums.LogLevel;
|
import com.rosetta.im.logger.enums.LogLevel;
|
||||||
import com.rosetta.im.packet.Packet0Handshake;
|
import com.rosetta.im.packet.Packet0Handshake;
|
||||||
|
import com.rosetta.im.packet.Packet10RequestUpdate;
|
||||||
import com.rosetta.im.packet.Packet11Typeing;
|
import com.rosetta.im.packet.Packet11Typeing;
|
||||||
import com.rosetta.im.packet.Packet15RequestTransport;
|
import com.rosetta.im.packet.Packet15RequestTransport;
|
||||||
import com.rosetta.im.packet.Packet1UserInfo;
|
import com.rosetta.im.packet.Packet1UserInfo;
|
||||||
@@ -145,6 +147,7 @@ public class Boot {
|
|||||||
this.packetManager.registerPacket(7, Packet7Read.class);
|
this.packetManager.registerPacket(7, Packet7Read.class);
|
||||||
this.packetManager.registerPacket(8, Packet8Delivery.class);
|
this.packetManager.registerPacket(8, Packet8Delivery.class);
|
||||||
this.packetManager.registerPacket(9, Packet9DeviceNew.class);
|
this.packetManager.registerPacket(9, Packet9DeviceNew.class);
|
||||||
|
this.packetManager.registerPacket(10, Packet10RequestUpdate.class);
|
||||||
this.packetManager.registerPacket(11, Packet11Typeing.class);
|
this.packetManager.registerPacket(11, Packet11Typeing.class);
|
||||||
this.packetManager.registerPacket(15, Packet15RequestTransport.class);
|
this.packetManager.registerPacket(15, Packet15RequestTransport.class);
|
||||||
this.packetManager.registerPacket(23, Packet23DeviceList.class);
|
this.packetManager.registerPacket(23, Packet23DeviceList.class);
|
||||||
@@ -159,6 +162,7 @@ public class Boot {
|
|||||||
this.packetManager.registerExecutor(4, new Executor4OnlineState(this.onlineManager, 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(6, new Executor6Message(this.clientManager, this.packetManager));
|
||||||
this.packetManager.registerExecutor(7, new Executor7Read(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(11, new Executor11Typeing(this.clientManager, this.packetManager));
|
||||||
this.packetManager.registerExecutor(15, new Executor15RequestTransport(this.serverConfiguration));
|
this.packetManager.registerExecutor(15, new Executor15RequestTransport(this.serverConfiguration));
|
||||||
this.packetManager.registerExecutor(24, new Executor24DeviceResolve(this.clientManager, this.eventManager));
|
this.packetManager.registerExecutor(24, new Executor24DeviceResolve(this.clientManager, this.eventManager));
|
||||||
|
|||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.rosetta.im.executors;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.rosetta.im.config.ServerConfiguration;
|
||||||
|
import com.rosetta.im.packet.Packet10RequestUpdate;
|
||||||
|
import com.rosetta.im.util.RandomUtil;
|
||||||
|
|
||||||
|
import io.orprotocol.ProtocolException;
|
||||||
|
import io.orprotocol.client.Client;
|
||||||
|
import io.orprotocol.packet.PacketExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Исполнитель по своей логике идентичен Executor15RequestTransport,
|
||||||
|
* но код продублирован специально, чтобы не размазывать
|
||||||
|
* его например в Dispatcher. Так читать удобнее
|
||||||
|
*/
|
||||||
|
public class Executor10RequestUpdate extends PacketExecutor<Packet10RequestUpdate> {
|
||||||
|
|
||||||
|
private ServerConfiguration serverConfiguration;
|
||||||
|
|
||||||
|
public Executor10RequestUpdate(ServerConfiguration serverConfiguration) {
|
||||||
|
this.serverConfiguration = serverConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onPacketReceived(Packet10RequestUpdate packet, Client client) throws Exception, ProtocolException {
|
||||||
|
/**
|
||||||
|
* Обратите внимание этот пакет в отличии от Packet15RequestTransport
|
||||||
|
* не требует авторизации. Это сделано на те случаи когда приложение
|
||||||
|
* обновить нужно, а авторизоваться не получается (например если
|
||||||
|
* авторизация сломалась)
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* Если пользователь авторизован, выбираем случайный сервер обновлений и
|
||||||
|
* заполняем им пакет
|
||||||
|
*
|
||||||
|
* TODO: Логика проверки на доступность (health)
|
||||||
|
*/
|
||||||
|
List<String> cdnServers = this.serverConfiguration.getUpdateServers();
|
||||||
|
String selectedServer = cdnServers.get(RandomUtil.randomBetween(0, cdnServers.size() - 1));
|
||||||
|
packet.setServer(selectedServer);
|
||||||
|
/**
|
||||||
|
* Сервер выбран, отправляем готовый пакет клиенту
|
||||||
|
*/
|
||||||
|
client.send(packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -38,7 +38,7 @@ public class Executor15RequestTransport extends PacketExecutor<Packet15RequestTr
|
|||||||
*/
|
*/
|
||||||
List<String> cdnServers = this.serverConfiguration.getTransportServers();
|
List<String> cdnServers = this.serverConfiguration.getTransportServers();
|
||||||
String selectedServer = cdnServers.get(RandomUtil.randomBetween(0, cdnServers.size() - 1));
|
String selectedServer = cdnServers.get(RandomUtil.randomBetween(0, cdnServers.size() - 1));
|
||||||
packet.setTransportServer(selectedServer);
|
packet.setServer(selectedServer);
|
||||||
/**
|
/**
|
||||||
* Сервер выбран, отправляем готовый пакет клиенту
|
* Сервер выбран, отправляем готовый пакет клиенту
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.rosetta.im.packet;
|
||||||
|
|
||||||
|
import com.rosetta.im.packet.base.PacketBaseServer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Получает сервер обновления
|
||||||
|
*/
|
||||||
|
public class Packet10RequestUpdate extends PacketBaseServer {
|
||||||
|
/**
|
||||||
|
* Пустой пакет, так как он наследник PacketBaseServer
|
||||||
|
* который всегда имеет одну структуру.
|
||||||
|
* Смотреть PacketBaseServer для реализации
|
||||||
|
*/
|
||||||
|
}
|
||||||
@@ -1,44 +1,16 @@
|
|||||||
package com.rosetta.im.packet;
|
package com.rosetta.im.packet;
|
||||||
|
|
||||||
import io.orprotocol.Stream;
|
import com.rosetta.im.packet.base.PacketBaseServer;
|
||||||
import io.orprotocol.packet.Packet;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Пакет отправляется клиентом для запроса транспортного сервера, строка в этот момент клиентом
|
* Пакет отправляется клиентом для запроса транспортного сервера, строка в этот момент клиентом
|
||||||
* не заполняется, а уже обратно сервер заполняет строку и записывает туда транспортный сервер
|
* не заполняется, а уже обратно сервер заполняет строку и записывает туда транспортный сервер
|
||||||
* чтобы клиент мог отправлять вложения на него
|
* чтобы клиент мог отправлять вложения на него
|
||||||
*/
|
*/
|
||||||
public class Packet15RequestTransport extends Packet {
|
public class Packet15RequestTransport extends PacketBaseServer {
|
||||||
|
|
||||||
private String transportServer;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void read(Stream stream) {
|
|
||||||
this.transportServer = stream.readString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Stream write() {
|
|
||||||
Stream stream = new Stream();
|
|
||||||
stream.writeInt16(this.packetId);
|
|
||||||
stream.writeString(this.transportServer);
|
|
||||||
return stream;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Получить транспортный сервер
|
* Пустой пакет, так как он наследник PacketBaseServer
|
||||||
* @return транспортный сервер
|
* который всегда имеет одну структуру.
|
||||||
|
* Смотреть PacketBaseServer для реализации
|
||||||
*/
|
*/
|
||||||
public String getTransportServer() {
|
|
||||||
return transportServer;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Установить транспортный сервер
|
|
||||||
* @param transportServer транспортный сервер
|
|
||||||
*/
|
|
||||||
public void setTransportServer(String transportServer) {
|
|
||||||
this.transportServer = transportServer;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.rosetta.im.packet.base;
|
||||||
|
|
||||||
|
import io.orprotocol.Stream;
|
||||||
|
import io.orprotocol.packet.Packet;
|
||||||
|
|
||||||
|
public class PacketBaseServer extends Packet {
|
||||||
|
private String server;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(Stream stream) {
|
||||||
|
this.server = stream.readString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Stream write() {
|
||||||
|
Stream stream = new Stream();
|
||||||
|
stream.writeInt16(this.packetId);
|
||||||
|
stream.writeString(this.server);
|
||||||
|
return stream;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Получить сервер
|
||||||
|
* @return сервер
|
||||||
|
*/
|
||||||
|
public String getServer() {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Установить сервер
|
||||||
|
* @param server сервер
|
||||||
|
*/
|
||||||
|
public void setServer(String server) {
|
||||||
|
this.server = server;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user