Clean up code style surrounding bootstrap creation

This commit is contained in:
md_5 2013-05-15 19:08:14 +10:00
parent 57793e93f0
commit 8a70af5293
3 changed files with 58 additions and 54 deletions

View File

@ -223,13 +223,7 @@ public class BungeeCord extends ProxyServer
{ {
for ( final ListenerInfo info : config.getListeners() ) for ( final ListenerInfo info : config.getListeners() )
{ {
new ServerBootstrap() ChannelFutureListener listener = new ChannelFutureListener()
.channel( NioServerSocketChannel.class )
.childAttr( PipelineUtils.LISTENER, info )
.childHandler( PipelineUtils.SERVER_CHILD )
.group( eventLoops )
.localAddress( info.getHost() )
.bind().addListener( new ChannelFutureListener()
{ {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception public void operationComplete(ChannelFuture future) throws Exception
@ -243,7 +237,14 @@ public class BungeeCord extends ProxyServer
getLogger().log( Level.WARNING, "Could not bind to host " + info.getHost(), future.cause() ); getLogger().log( Level.WARNING, "Could not bind to host " + info.getHost(), future.cause() );
} }
} }
} ); };
new ServerBootstrap()
.channel( NioServerSocketChannel.class )
.childAttr( PipelineUtils.LISTENER, info )
.childHandler( PipelineUtils.SERVER_CHILD )
.group( eventLoops )
.localAddress( info.getHost() )
.bind().addListener( listener );
} }
} }

View File

@ -98,14 +98,7 @@ public class BungeeServerInfo implements ServerInfo
@Override @Override
public void ping(final Callback<ServerPing> callback) public void ping(final Callback<ServerPing> callback)
{ {
new Bootstrap() ChannelFutureListener listener = new ChannelFutureListener()
.channel( NioSocketChannel.class )
.group( BungeeCord.getInstance().eventLoops )
.handler( PipelineUtils.BASE )
.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable
.remoteAddress( getAddress() )
.connect()
.addListener( new ChannelFutureListener()
{ {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception public void operationComplete(ChannelFuture future) throws Exception
@ -118,6 +111,14 @@ public class BungeeServerInfo implements ServerInfo
callback.done( null, future.cause() ); callback.done( null, future.cause() );
} }
} }
} ); };
new Bootstrap()
.channel( NioSocketChannel.class )
.group( BungeeCord.getInstance().eventLoops )
.handler( PipelineUtils.BASE )
.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable
.remoteAddress( getAddress() )
.connect()
.addListener( listener );
} }
} }

View File

@ -89,34 +89,34 @@ public final class UserConnection implements ProxiedPlayer
@Getter @Getter
private String displayName; private String displayName;
/*========================================================================*/ /*========================================================================*/
public void init() public void init()
{ {
this.displayName = name; this.displayName = name;
Collection<String> g = bungee.getConfigurationAdapter().getGroups( name ); Collection<String> g = bungee.getConfigurationAdapter().getGroups( name );
for ( String s : g ) for ( String s : g )
{ {
addGroups( s ); addGroups( s );
} }
} }
public void sendPacket(DefinedPacket p) public void sendPacket(DefinedPacket p)
{ {
ch.write( p ); ch.write( p );
} }
public void sendPacket(byte[] b) public void sendPacket(byte[] b)
{ {
ch.write( b ); ch.write( b );
} }
@Deprecated @Deprecated
public boolean isActive() public boolean isActive()
{ {
return ch.getHandle().isActive(); return ch.getHandle().isActive();
} }
@Override @Override
public void setDisplayName(String name) public void setDisplayName(String name)
{ {
@ -126,20 +126,20 @@ public final class UserConnection implements ProxiedPlayer
displayName = name; displayName = name;
bungee.getTabListHandler().onConnect( this ); bungee.getTabListHandler().onConnect( this );
} }
@Override @Override
public void connect(ServerInfo target) public void connect(ServerInfo target)
{ {
connect( target, false ); connect( target, false );
} }
public void connectNow(ServerInfo target) public void connectNow(ServerInfo target)
{ {
sendPacket( Packet9Respawn.DIM1_SWITCH ); sendPacket( Packet9Respawn.DIM1_SWITCH );
sendPacket( Packet9Respawn.DIM2_SWITCH ); sendPacket( Packet9Respawn.DIM2_SWITCH );
connect( target ); connect( target );
} }
public void connect(ServerInfo info, final boolean retry) public void connect(ServerInfo info, final boolean retry)
{ {
ServerConnectEvent event = new ServerConnectEvent( this, info ); ServerConnectEvent event = new ServerConnectEvent( this, info );
@ -147,7 +147,7 @@ public final class UserConnection implements ProxiedPlayer
{ {
return; return;
} }
Preconditions.checkArgument( event.getTarget() instanceof BungeeServerInfo, "BungeeCord can only connect to BungeeServerInfo instances" ); Preconditions.checkArgument( event.getTarget() instanceof BungeeServerInfo, "BungeeCord can only connect to BungeeServerInfo instances" );
final BungeeServerInfo target = (BungeeServerInfo) event.getTarget(); // Update in case the event changed target final BungeeServerInfo target = (BungeeServerInfo) event.getTarget(); // Update in case the event changed target
@ -161,13 +161,10 @@ public final class UserConnection implements ProxiedPlayer
sendMessage( ChatColor.RED + "Already connecting to this server!" ); sendMessage( ChatColor.RED + "Already connecting to this server!" );
return; return;
} }
pendingConnects.add( target ); pendingConnects.add( target );
new Bootstrap() ChannelInitializer initializer = new ChannelInitializer()
.channel( NioSocketChannel.class )
.group( BungeeCord.getInstance().eventLoops )
.handler( new ChannelInitializer()
{ {
@Override @Override
protected void initChannel(Channel ch) throws Exception protected void initChannel(Channel ch) throws Exception
@ -175,11 +172,8 @@ public final class UserConnection implements ProxiedPlayer
PipelineUtils.BASE.initChannel( ch ); PipelineUtils.BASE.initChannel( ch );
ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( bungee, UserConnection.this, target ) ); ch.pipeline().get( HandlerBoss.class ).setHandler( new ServerConnector( bungee, UserConnection.this, target ) );
} }
} ) };
.localAddress( getPendingConnection().getListener().getHost() ) ChannelFutureListener listener = new ChannelFutureListener()
.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable
.remoteAddress( target.getAddress() )
.connect().addListener( new ChannelFutureListener()
{ {
@Override @Override
public void operationComplete(ChannelFuture future) throws Exception public void operationComplete(ChannelFuture future) throws Exception
@ -188,7 +182,7 @@ public final class UserConnection implements ProxiedPlayer
{ {
future.channel().close(); future.channel().close();
pendingConnects.remove( target ); pendingConnects.remove( target );
ServerInfo def = ProxyServer.getInstance().getServers().get( getPendingConnection().getListener().getFallbackServer() ); ServerInfo def = ProxyServer.getInstance().getServers().get( getPendingConnection().getListener().getFallbackServer() );
if ( retry & target != def && ( getServer() == null || def != getServer().getInfo() ) ) if ( retry & target != def && ( getServer() == null || def != getServer().getInfo() ) )
{ {
@ -206,9 +200,17 @@ public final class UserConnection implements ProxiedPlayer
} }
} }
} }
} ); };
new Bootstrap()
.channel( NioSocketChannel.class )
.group( BungeeCord.getInstance().eventLoops )
.handler( initializer )
.localAddress( getPendingConnection().getListener().getHost() )
.option( ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000 ) // TODO: Configurable
.remoteAddress( target.getAddress() )
.connect().addListener( listener );
} }
@Override @Override
public synchronized void disconnect(String reason) public synchronized void disconnect(String reason)
{ {
@ -223,20 +225,20 @@ public final class UserConnection implements ProxiedPlayer
} }
} }
} }
@Override @Override
public void chat(String message) public void chat(String message)
{ {
Preconditions.checkState( server != null, "Not connected to server" ); Preconditions.checkState( server != null, "Not connected to server" );
server.getCh().write( new Packet3Chat( message ) ); server.getCh().write( new Packet3Chat( message ) );
} }
@Override @Override
public void sendMessage(String message) public void sendMessage(String message)
{ {
sendPacket( new Packet3Chat( message ) ); sendPacket( new Packet3Chat( message ) );
} }
@Override @Override
public void sendMessages(String... messages) public void sendMessages(String... messages)
{ {
@ -245,25 +247,25 @@ public final class UserConnection implements ProxiedPlayer
sendMessage( message ); sendMessage( message );
} }
} }
@Override @Override
public void sendData(String channel, byte[] data) public void sendData(String channel, byte[] data)
{ {
sendPacket( new PacketFAPluginMessage( channel, data ) ); sendPacket( new PacketFAPluginMessage( channel, data ) );
} }
@Override @Override
public InetSocketAddress getAddress() public InetSocketAddress getAddress()
{ {
return (InetSocketAddress) ch.getHandle().remoteAddress(); return (InetSocketAddress) ch.getHandle().remoteAddress();
} }
@Override @Override
public Collection<String> getGroups() public Collection<String> getGroups()
{ {
return Collections.unmodifiableCollection( groups ); return Collections.unmodifiableCollection( groups );
} }
@Override @Override
public void addGroups(String... groups) public void addGroups(String... groups)
{ {
@ -276,7 +278,7 @@ public final class UserConnection implements ProxiedPlayer
} }
} }
} }
@Override @Override
public void removeGroups(String... groups) public void removeGroups(String... groups)
{ {
@ -289,13 +291,13 @@ public final class UserConnection implements ProxiedPlayer
} }
} }
} }
@Override @Override
public boolean hasPermission(String permission) public boolean hasPermission(String permission)
{ {
return bungee.getPluginManager().callEvent( new PermissionCheckEvent( this, permission, permissions.contains( permission ) ) ).hasPermission(); return bungee.getPluginManager().callEvent( new PermissionCheckEvent( this, permission, permissions.contains( permission ) ) ).hasPermission();
} }
@Override @Override
public void setPermission(String permission, boolean value) public void setPermission(String permission, boolean value)
{ {
@ -307,19 +309,19 @@ public final class UserConnection implements ProxiedPlayer
permissions.remove( permission ); permissions.remove( permission );
} }
} }
@Override @Override
public String toString() public String toString()
{ {
return name; return name;
} }
public void setClientEntityId(int clientEntityId) public void setClientEntityId(int clientEntityId)
{ {
Preconditions.checkState( this.clientEntityId == 0, "Client entityId already set!" ); Preconditions.checkState( this.clientEntityId == 0, "Client entityId already set!" );
this.clientEntityId = clientEntityId; this.clientEntityId = clientEntityId;
} }
@Override @Override
public void setTexturePack(TexturePackInfo pack) public void setTexturePack(TexturePackInfo pack)
{ {