Esta semana ha tocado implementar un servidor de Echo multi-thread, es decir, tendremos un Servidor que es capaz de soportar simultáneamente la conexión de múltiples clientes y darle servicio a todos ellos.
Todo esto, con Sockets y utilizando un diseño basado en los siguientes patrones:
- Patrón Wrapper Facade
- Patrón Reactor
- Patrón Acceptor-Connector
Es verdad que arrancar costó, y que la primera idea, la primera solución que se te ocurre para implementarlo está muy lejos de la solución final aplicando todos estos patrones.
Después de terminarlo es cuando te das cuenta que el código resultante es una auténtica gozada:
- Bien estructurado
- Bien organizado
- Fácil de adaptar a nuevos requierimientos
- Muy escalable
- Muy mantenible
- ...
Les dejo el enunciado por si a alguien le interesa intentarlo por su cuenta:
The purpose of this assignment is to deepen your understanding of the Wrapper Facade pattern, the Reactor pattern and the (Acceptor role of the) Acceptor-Connector pattern in the context of Java. In particular, you will write a platform-independent reactive server program that accepts a connection from a client and echoes back what the client sent.
The reactive server program should do the following activities:
- Create an EchoServerHandler so that it echoes back to the client whatever the client sends it.
- When the clients request arrives you need to decide how to handle it in order accept the request and set up the connection.
- When data arrives from the client EchoSeverHandler should be called and echo back the client's input either (a) a "chunk" at a time or (b) a "line" at a time (i.e., until the symbols "\n", "\r", or "\r\n" are read), rather than a character at a time.
- For this assignment, you can simply exit the server process when you're done, which will close down all the open sockets.
Please implement this server program in Java and contain the code in a single file. A second file may optionally be included for a simple console client (clearly named) if you write one during your solution development as a test program and wish to include it for the convenience of evaluators, but it is not required and will not be assessed as part of this exercise, just used as a test driver for your server. You can also use a telnet client (such asputty) as a test driver for your server.
An evaluator should be able to compile it with a command such as "javac Program.java" and execute it with 'java Program port_number' (and 'java Program port_number' for Windows) and your program should successfully run!
Mi solución
Te dejo también un enlace a mi repositorio github por si se te atraganta y quieres ver cómo lo resolví:
https://github.com/moisespd/patrones_coursera/tree/master/SocketsMultiThread