-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Los-Socketes/cliente
Lógica básica de cliente
- Loading branch information
Showing
10 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#include "entradateclado.h" | ||
|
||
EntradaTeclado::EntradaTeclado(Queue<std::string>& envio_comandos, Queue<std::string>& comandos_teclado) | ||
: envio_comandos(envio_comandos), comandos_teclado(comandos_teclado), cont(true) {} | ||
|
||
void EntradaTeclado::run() { | ||
try { | ||
// TODO: Implementar SDL para leer los comandos del teclado. | ||
} catch (const ClosedQueue& e) { | ||
syslog(LOG_INFO, "%s", e.what()); | ||
} | ||
} | ||
|
||
void EntradaTeclado::stop() { | ||
cont = false; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef ENTRADATECLADO_H_ | ||
#define ENTRADATECLADO_H_ | ||
|
||
#include <atomic> | ||
#include <iostream> | ||
#include <string> | ||
#include <syslog.h> | ||
|
||
#include "../common/thread.h" | ||
#include "../common/queue.h" | ||
|
||
class EntradaTeclado: public Thread { | ||
private: | ||
Queue<std::string>& envio_comandos; | ||
Queue<std::string>& comandos_teclado; | ||
std::atomic<bool> cont; | ||
|
||
public: | ||
EntradaTeclado(Queue<std::string>& envio_comandos, Queue<std::string>& comandos_teclado); | ||
|
||
// Hilo que se encarga de leer los comandos del teclado y enviarlos al servidor o | ||
// a la cola de comandos locales de teclado. | ||
void run() override; | ||
|
||
// Para cerrar el hilo. | ||
void stop(); | ||
|
||
}; | ||
|
||
#endif // ENTRADATECLADO_H_ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include "enviador.h" | ||
|
||
Enviador::Enviador(Queue<std::string>& envio_comandos) : | ||
envio_comandos(envio_comandos) , cont(true) {} | ||
|
||
void Enviador::run() { | ||
try { | ||
// TODO: Enviar comandos al servidor por protocolo. | ||
// Por ahora no hago nada con ellos. | ||
while(cont){ | ||
envio_comandos.pop(); | ||
} | ||
} catch (const ClosedQueue& e) { | ||
syslog(LOG_INFO, "%s", e.what()); | ||
} | ||
|
||
} | ||
|
||
void Enviador::stop() { | ||
cont = false; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef ENVIADOR_H_ | ||
#define ENVIADOR_H_ | ||
|
||
#include <atomic> | ||
#include <iostream> | ||
#include <string> | ||
#include <syslog.h> | ||
|
||
#include "../common/thread.h" | ||
#include "../common/queue.h" | ||
|
||
class Enviador: public Thread { | ||
private: | ||
Queue<std::string>& envio_comandos; | ||
std::atomic<bool> cont; | ||
|
||
public: | ||
Enviador(Queue<std::string>& envio_comandos); | ||
|
||
// Hilo que se encarga de enviar los comandos al servidor. | ||
void run() override; | ||
|
||
// Para cerrar el hilo. | ||
void stop(); | ||
|
||
}; | ||
|
||
#endif // ENVIADOR_H_ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef ESTADOJUEGO_H_ | ||
#define ESTADOJUEGO_H_ | ||
|
||
struct EstadoJuego { | ||
char coord_x; | ||
char coord_y; | ||
|
||
EstadoJuego(char coord_x, char coord_y) : coord_x(coord_x), coord_y(coord_y) {} | ||
|
||
}; | ||
|
||
#endif // ESTADOJUEGO_H_ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,81 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <syslog.h> | ||
|
||
int main() { | ||
std::cout << "Hola mundo desde el cliente\n"; | ||
#include "estadojuego.h" | ||
#include "entradateclado.h" | ||
#include "recibidor.h" | ||
#include "enviador.h" | ||
#include "../common/thread.h" | ||
#include "../common/queue.h" | ||
|
||
#define HOST 1 | ||
#define PUERTO 2 | ||
#define TAM_QUEUE 100 | ||
|
||
int main(int argc, char* argv[]) { | ||
if (argc != 3) { | ||
std::cout << "Usar como: client <ip/hostname server> "; | ||
std::cout << "<puerto/servicename>" << std::endl; | ||
return 1; | ||
} | ||
try { | ||
bool continuar = true; | ||
EstadoJuego estado_juego(0, 0); | ||
|
||
// Instanciar Socket(argv[HOST], argv[PUERTO]) y | ||
// pasarselo al protocolo. | ||
|
||
// Colas de comunicación. | ||
Queue<EstadoJuego> recepcion_estados(TAM_QUEUE); | ||
Queue<std::string> envio_comandos(TAM_QUEUE); | ||
// Queue<std::string> comandos_teclado(TAM_QUEUE); | ||
|
||
// Hilo de entrada de teclado. | ||
// EntradaTeclado entrada_teclado(comandos_teclado, envio_comandos); | ||
|
||
// Hilos de envío y recepción. | ||
Enviador enviador(envio_comandos); | ||
Recibidor recibidor(recepcion_estados); | ||
|
||
// Iniciar hilos. | ||
// entrada_teclado.start(); | ||
enviador.start(); | ||
recibidor.start(); | ||
|
||
// Loop principal. | ||
while (continuar) { | ||
// Recibir estado del juego (no bloqueante) y actualizar. | ||
recepcion_estados.try_pop(estado_juego); | ||
|
||
// TODO: Dibujar y renderizar. | ||
|
||
// TODO: Recibir comandos de la cola no bloqueante de comandos locales. | ||
// Por ahora solo leo del teclado. | ||
std::string comando; | ||
std::getline(std::cin, comando); | ||
if (comando == "estado") { | ||
std::cout << "Coord X: " << std::to_string(estado_juego.coord_x); | ||
std::cout << " Coord Y: " << std::to_string(estado_juego.coord_y) << std::endl; | ||
} | ||
else if (comando == "salir") { | ||
continuar = false; | ||
} | ||
} | ||
|
||
// comandos_teclado.close(); | ||
// entrada_teclado.stop(); | ||
// entrada_teclado.join(); | ||
recepcion_estados.close(); | ||
envio_comandos.close(); | ||
enviador.stop(); | ||
recibidor.stop(); | ||
enviador.join(); | ||
recibidor.join(); | ||
} catch (const std::exception& e) { | ||
syslog(LOG_CRIT, "[Crit] Error: %s\n", e.what()); | ||
} catch (...) { | ||
syslog(LOG_CRIT, "[Crit] Unknown error.\n"); | ||
} | ||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "recibidor.h" | ||
|
||
Recibidor::Recibidor(Queue<EstadoJuego>& recepcion_estados): | ||
recepcion_estados(recepcion_estados), cont(true) {} | ||
|
||
void Recibidor::run() { | ||
try { | ||
// TODO: Recibir estados del juego de protocolo. | ||
|
||
// Por ahora solo devuelvo un estado con | ||
// coordenadas incrementadas de a 1. | ||
EstadoJuego estado_juego(0, 0); | ||
int i = 0; | ||
while (cont && i++ < 50) { | ||
estado_juego.coord_x++; | ||
estado_juego.coord_y++; | ||
recepcion_estados.push(estado_juego); | ||
} | ||
} catch (const ClosedQueue& e) { | ||
syslog(LOG_INFO, "%s", e.what()); | ||
} | ||
} | ||
|
||
void Recibidor::stop() { | ||
cont = false; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef RECIBIDOR_H_ | ||
#define RECIBIDOR_H_ | ||
|
||
#include <atomic> | ||
#include <syslog.h> | ||
|
||
#include "estadojuego.h" | ||
#include "../common/thread.h" | ||
#include "../common/queue.h" | ||
|
||
class Recibidor: public Thread { | ||
private: | ||
Queue<EstadoJuego>& recepcion_estados; | ||
std::atomic<bool> cont; | ||
|
||
public: | ||
Recibidor(Queue<EstadoJuego>& recepcion_estados); | ||
|
||
// Hilo que se encarga de recibir los estados del juego. | ||
void run() override; | ||
|
||
// Para cerrar el hilo. | ||
void stop(); | ||
|
||
}; | ||
|
||
#endif // RECIBIDOR_H_ |