Skip to content

Instantly share code, notes, and snippets.

@volgar1x
Created November 19, 2012 21:27
Show Gist options
  • Save volgar1x/4114085 to your computer and use it in GitHub Desktop.
Save volgar1x/4114085 to your computer and use it in GitHub Desktop.
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