2019-05-13 05:28:27 +02:00
package net.ME1312.SubServers.Host.Network ;
import net.ME1312.Galaxi.Library.Callback.Callback ;
2019-08-30 02:01:52 +02:00
import net.ME1312.Galaxi.Library.Config.YAMLSection ;
2020-06-12 07:45:49 +02:00
import net.ME1312.Galaxi.Library.Container.NamedContainer ;
import net.ME1312.Galaxi.Library.Map.ObjectMap ;
2019-05-13 05:28:27 +02:00
import net.ME1312.Galaxi.Library.Util ;
import net.ME1312.Galaxi.Library.Version.Version ;
import net.ME1312.SubData.Client.SubDataClient ;
import net.ME1312.SubData.Client.SubDataProtocol ;
import net.ME1312.SubServers.Host.Event.SubNetworkConnectEvent ;
import net.ME1312.SubServers.Host.Event.SubNetworkDisconnectEvent ;
import net.ME1312.SubServers.Host.ExHost ;
import net.ME1312.SubServers.Host.Network.Packet.* ;
import net.ME1312.SubServers.Host.SubAPI ;
import java.io.File ;
import java.io.IOException ;
import java.net.InetAddress ;
import java.util.HashMap ;
import java.util.logging.Logger ;
public class SubProtocol extends SubDataProtocol {
private static SubProtocol instance ;
private SubProtocol ( ) { }
@SuppressWarnings ( " deprecation " )
public static SubProtocol get ( ) {
if ( instance = = null ) {
instance = new SubProtocol ( ) ;
ExHost host = SubAPI . getInstance ( ) . getInternals ( ) ;
instance . setName ( " SubServers 2 " ) ;
2020-06-12 07:45:49 +02:00
instance . addVersion ( new Version ( " 2.16a+ " ) ) ;
2019-05-13 05:28:27 +02:00
2019-06-18 02:20:46 +02:00
// 00-0F: Object Link Packets
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0001 , PacketLinkExHost . class ) ;
instance . registerPacket ( 0x0001 , new PacketLinkExHost ( host ) ) ;
2019-06-18 02:20:46 +02:00
// 10-2F: Download Packets
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0010 , PacketDownloadLang . class ) ;
instance . registerPacket ( 0x0011 , PacketDownloadPlatformInfo . class ) ;
instance . registerPacket ( 0x0012 , PacketDownloadProxyInfo . class ) ;
instance . registerPacket ( 0x0013 , PacketDownloadHostInfo . class ) ;
instance . registerPacket ( 0x0014 , PacketDownloadGroupInfo . class ) ;
instance . registerPacket ( 0x0015 , PacketDownloadServerInfo . class ) ;
2020-06-12 07:45:49 +02:00
instance . registerPacket ( 0x0016 , PacketDownloadPlayerInfo . class ) ;
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0017 , PacketCheckPermission . class ) ;
2019-10-19 00:24:58 +02:00
instance . registerPacket ( 0x0017 , PacketCheckPermissionResponse . class ) ;
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0010 , new PacketDownloadLang ( host ) ) ;
instance . registerPacket ( 0x0011 , new PacketDownloadPlatformInfo ( ) ) ;
instance . registerPacket ( 0x0012 , new PacketDownloadProxyInfo ( ) ) ;
instance . registerPacket ( 0x0013 , new PacketDownloadHostInfo ( ) ) ;
instance . registerPacket ( 0x0014 , new PacketDownloadGroupInfo ( ) ) ;
instance . registerPacket ( 0x0015 , new PacketDownloadServerInfo ( ) ) ;
2020-06-12 07:45:49 +02:00
instance . registerPacket ( 0x0016 , new PacketDownloadPlayerInfo ( ) ) ;
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0017 , new PacketCheckPermission ( ) ) ;
2019-10-19 00:24:58 +02:00
instance . registerPacket ( 0x0018 , new PacketCheckPermissionResponse ( ) ) ;
2019-05-13 05:28:27 +02:00
2019-06-18 02:20:46 +02:00
// 30-4F: Control Packets
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0030 , PacketCreateServer . class ) ;
instance . registerPacket ( 0x0031 , PacketAddServer . class ) ;
instance . registerPacket ( 0x0032 , PacketStartServer . class ) ;
2019-06-17 05:13:48 +02:00
instance . registerPacket ( 0x0033 , PacketUpdateServer . class ) ;
instance . registerPacket ( 0x0034 , PacketEditServer . class ) ;
instance . registerPacket ( 0x0035 , PacketRestartServer . class ) ;
instance . registerPacket ( 0x0036 , PacketCommandServer . class ) ;
instance . registerPacket ( 0x0037 , PacketStopServer . class ) ;
instance . registerPacket ( 0x0038 , PacketRemoveServer . class ) ;
instance . registerPacket ( 0x0039 , PacketDeleteServer . class ) ;
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0030 , new PacketCreateServer ( ) ) ;
instance . registerPacket ( 0x0031 , new PacketAddServer ( ) ) ;
instance . registerPacket ( 0x0032 , new PacketStartServer ( ) ) ;
2019-06-17 05:13:48 +02:00
instance . registerPacket ( 0x0033 , new PacketUpdateServer ( ) ) ;
instance . registerPacket ( 0x0034 , new PacketEditServer ( ) ) ;
instance . registerPacket ( 0x0035 , new PacketRestartServer ( ) ) ;
instance . registerPacket ( 0x0036 , new PacketCommandServer ( ) ) ;
instance . registerPacket ( 0x0037 , new PacketStopServer ( ) ) ;
instance . registerPacket ( 0x0038 , new PacketRemoveServer ( ) ) ;
instance . registerPacket ( 0x0039 , new PacketDeleteServer ( ) ) ;
2019-05-13 05:28:27 +02:00
2019-06-18 02:20:46 +02:00
// 50-6F: External Host Packets
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0050 , PacketExConfigureHost . class ) ;
instance . registerPacket ( 0x0051 , PacketExDownloadTemplates . class ) ;
instance . registerPacket ( 0x0052 , PacketOutExRequestQueue . class ) ;
instance . registerPacket ( 0x0053 , PacketExCreateServer . class ) ;
instance . registerPacket ( 0x0054 , PacketExAddServer . class ) ;
2019-06-17 05:13:48 +02:00
instance . registerPacket ( 0x0055 , PacketExEditServer . class ) ;
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0056 , PacketOutExLogMessage . class ) ;
2020-06-12 07:45:49 +02:00
instance . registerPacket ( 0x0057 , PacketExRemoveServer . class ) ;
instance . registerPacket ( 0x0058 , PacketExDeleteServer . class ) ;
2019-05-13 05:28:27 +02:00
instance . registerPacket ( 0x0050 , new PacketExConfigureHost ( host ) ) ;
instance . registerPacket ( 0x0051 , new PacketExDownloadTemplates ( host ) ) ;
//instance.registerPacket(0x0052, new PacketOutExRequestQueue(host));
instance . registerPacket ( 0x0053 , new PacketExCreateServer ( host ) ) ;
instance . registerPacket ( 0x0054 , new PacketExAddServer ( host ) ) ;
2019-06-17 05:13:48 +02:00
instance . registerPacket ( 0x0055 , new PacketExEditServer ( host ) ) ;
2019-05-13 05:28:27 +02:00
//instance.registerPacket(0x0056, new PacketOutExLogMessage());
2020-06-12 07:45:49 +02:00
instance . registerPacket ( 0x0057 , new PacketExRemoveServer ( host ) ) ;
instance . registerPacket ( 0x0058 , new PacketExDeleteServer ( host ) ) ;
2019-05-13 05:28:27 +02:00
2019-06-18 02:20:46 +02:00
// 70-7F: External Misc Packets
2019-05-13 05:28:27 +02:00
//instance.registerPacket(0x0070, PacketInExRunEvent.class);
//instance.registerPacket(0x0071, PacketInExReset.class);
//instance.registerPacket(0x0072, PacketInExReload.class);
instance . registerPacket ( 0x0070 , new PacketInExRunEvent ( ) ) ;
instance . registerPacket ( 0x0071 , new PacketInExReset ( host ) ) ;
instance . registerPacket ( 0x0072 , new PacketInExReload ( host ) ) ;
}
return instance ;
}
private Logger getLogger ( int channel ) {
return new net . ME1312 . Galaxi . Library . Log . Logger ( " SubData " + ( ( channel ! = 0 ) ? File . separator + " Sub- " + channel : " " ) ) . toPrimitive ( ) ;
}
@Override
2020-06-12 07:45:49 +02:00
protected SubDataClient sub ( Callback < Runnable > scheduler , Logger logger , InetAddress address , int port , ObjectMap < ? > login ) throws IOException {
2019-05-13 05:28:27 +02:00
ExHost host = SubAPI . getInstance ( ) . getInternals ( ) ;
HashMap < Integer , SubDataClient > map = Util . getDespiteException ( ( ) - > Util . reflect ( ExHost . class . getDeclaredField ( " subdata " ) , host ) , null ) ;
int channel = 1 ;
while ( map . keySet ( ) . contains ( channel ) ) channel + + ;
final int fc = channel ;
2020-06-12 07:45:49 +02:00
SubDataClient subdata = super . open ( scheduler , getLogger ( fc ) , address , port , login ) ;
2019-05-13 05:28:27 +02:00
map . put ( fc , subdata ) ;
subdata . sendPacket ( new PacketLinkExHost ( host , fc ) ) ;
subdata . on . closed ( client - > map . remove ( fc ) ) ;
return subdata ;
}
@SuppressWarnings ( " deprecation " )
@Override
public SubDataClient open ( Callback < Runnable > scheduler , Logger logger , InetAddress address , int port ) throws IOException {
ExHost host = SubAPI . getInstance ( ) . getInternals ( ) ;
HashMap < Integer , SubDataClient > map = Util . getDespiteException ( ( ) - > Util . reflect ( ExHost . class . getDeclaredField ( " subdata " ) , host ) , null ) ;
SubDataClient subdata = super . open ( scheduler , logger , address , port ) ;
subdata . sendPacket ( new PacketLinkExHost ( host , 0 ) ) ;
subdata . sendPacket ( new PacketExConfigureHost ( host ) ) ;
subdata . sendPacket ( new PacketExDownloadTemplates ( host ) ) ;
subdata . sendPacket ( new PacketDownloadLang ( ) ) ;
subdata . sendPacket ( new PacketOutExRequestQueue ( ) ) ;
subdata . on . ready ( client - > host . engine . getPluginManager ( ) . executeEvent ( new SubNetworkConnectEvent ( ( SubDataClient ) client ) ) ) ;
subdata . on . closed ( client - > {
SubNetworkDisconnectEvent event = new SubNetworkDisconnectEvent ( client . get ( ) , client . name ( ) ) ;
host . engine . getPluginManager ( ) . executeEvent ( event ) ;
2019-09-26 18:41:04 +02:00
if ( Util . getDespiteException ( ( ) - > Util . reflect ( ExHost . class . getDeclaredField ( " running " ) , host ) , true ) ) {
Logger log = Util . getDespiteException ( ( ) - > Util . reflect ( SubDataClient . class . getDeclaredField ( " log " ) , client . get ( ) ) , null ) ;
log . info ( " Attempting reconnect in " + host . config . get ( ) . getMap ( " Settings " , new YAMLSection ( ) ) . getMap ( " SubData " , new YAMLSection ( ) ) . getInt ( " Reconnect " , 30 ) + " seconds " ) ;
Util . isException ( ( ) - > Util . reflect ( ExHost . class . getDeclaredMethod ( " connect " , Logger . class , NamedContainer . class ) , host , log , client ) ) ;
2019-10-19 00:24:58 +02:00
} else map . put ( 0 , null ) ;
2019-05-13 05:28:27 +02:00
} ) ;
return subdata ;
}
public SubDataClient open ( InetAddress address , int port ) throws IOException {
return open ( getLogger ( 0 ) , address , port ) ;
}
}