Created
November 19, 2012 21:27
-
-
Save volgar1x/4114085 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ManyNetworkHandlerFactory { | |
Set<NetworkHandler> create(NetworkClient client); | |
} | |
class NetworkHandlersModule extends AbstractModule { | |
protected void configure() { | |
install(new ManyFactoryModuleBuilder(NetworkClient.class) | |
.bind(AuthenticationHandler.class) | |
.build()); | |
} | |
} | |
class LoginNettyService extends NettyService { | |
@Inject ManyNetworkHandlerFactory factory; | |
protected NetworkClient newClient(NetworkSession session) { | |
DefaultLoginClient client = new DefaultLoginClient(this, session); | |
client.setHandlers(factory.create(client)); | |
return client; | |
} | |
} | |
class AuthenticationHandler implements NetworkHandler { | |
final NetworkClient client; | |
final Repository<User> users; | |
@Inject AuthenticationHandler(NetworkClient client, Repository<User> users) { | |
this.client = client; | |
this.users = users; | |
} | |
protected void write(NetworkMessage msg) { | |
client.getSession().write(msg); | |
} | |
@Handler | |
public void handleIdentificationMessage(IdentificationMessage msg) { | |
User user = users.findBy("name", msg.username); | |
if (user == null) { | |
write(new IdentificationFailureMessage(IdentificationFailureReason.BAD_CREDENTIALS)); | |
} | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment