PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/SDTP_Socket_Mongo/src/tpsocket/servidores/ThreadAtenderTractor.java

https://gitlab.com/HeanGee/SD_Socket
Java | 108 lines | 78 code | 11 blank | 19 comment | 3 complexity | 30f53aa426f1fe06c5abec7eb9e4ffe3 MD5 | raw file
  1. package tpsocket.servidores;
  2. import tpsocket.clientes.Tractor;
  3. import tpsocket.constantes.Defaults;
  4. import tpsocket.logs.LogFileManager;
  5. import tpsocket.basededatos.ConexionBD;
  6. import com.google.gson.Gson;
  7. import java.io.DataInputStream;
  8. import java.io.DataOutputStream;
  9. import java.io.IOException;
  10. import java.net.Socket;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. import javax.swing.JOptionPane;
  14. /**
  15. *
  16. * @author Fernando
  17. */
  18. public class ThreadAtenderTractor extends Thread{
  19. private final Socket clientSocket;
  20. private DataOutputStream canalSalida;
  21. private DataInputStream canalEntrada;
  22. public ThreadAtenderTractor(Socket clientSocket)
  23. {
  24. super("ThreadAtenderTractor");
  25. this.clientSocket = clientSocket;
  26. }
  27. @Override
  28. public void run()
  29. {
  30. try {
  31. /** Crear canales de entrada y salida. */
  32. this.canalEntrada = new DataInputStream(this.clientSocket.getInputStream());
  33. this.canalSalida = new DataOutputStream(this.clientSocket.getOutputStream());
  34. StringBuilder log = new StringBuilder();
  35. String mensajeGson;
  36. do {
  37. /**Lee el mensaje desde el cliente**/
  38. mensajeGson=this.canalEntrada.readUTF();
  39. /** GUARDAR EN BASE DE DATOS mensajeGson. */
  40. // ConexionBD conec = new ConexionBD("localhost" , 27017,"test"); //Crea un Cliente del MongoDB
  41. // conec.crear(); //crea la conexion a la BD
  42. // System.out.println("***Insertando a la Base de Datos...");
  43. // conec.insertar("Tractor",mensajeGson); //Inserta a la Base de Datos
  44. // System.out.println("***JSON string insertado***");
  45. /** Replay sobre recepcion y procesado de msg. */
  46. this.canalSalida.writeUTF("ok");
  47. /** Logs. */
  48. log.append("Cliente tractor: ").append(mensajeGson);
  49. this.escribirLog(log.toString());
  50. log = log.delete(0, log.length());
  51. /** Si la peticion no es desconexion. */
  52. if(!mensajeGson.equals("no")) {
  53. /** Crea un objeto JSON. */
  54. Gson obJSON = new Gson();
  55. /** Des-gson-nea al String, obteniendo un objeto de tipo Satelite. */
  56. /** Instancia ese objeto Satelite. */
  57. log.append("Construyendo objeto a partir del mensaje...");
  58. this.escribirLog(log.toString());
  59. log = log.delete(0, log.length());
  60. Tractor objTractor = obJSON.fromJson(mensajeGson, Tractor.class);
  61. log.append("Objeto construido correctamente: ").append(objTractor.getClass());
  62. log.append("Mensaje recibido correctamente del Tractor: ").append(objTractor.getIdentificador());
  63. this.escribirLog(log.toString());
  64. log = log.delete(0, log.length());
  65. }
  66. } while(!mensajeGson.equals("no"));
  67. /****/
  68. if(!this.clientSocket.isClosed() || this.clientSocket.isConnected())
  69. {
  70. log.append("Cliente cerro conexion."
  71. + " Cerrando flujos en el servidor de Tractor.");
  72. this.escribirLog(log.toString());
  73. this.canalEntrada.close();
  74. this.canalSalida.close();
  75. this.clientSocket.close();
  76. }
  77. } catch(IOException ex){
  78. System.out.println();
  79. Logger.getLogger(ThreadAtenderSatelite.class.getName()).log(Level.SEVERE, null, ex);
  80. this.escribirLog("I/O Exception desde run() de "+this.getName()+".");
  81. } catch (Throwable ex) {
  82. Logger.getLogger(ThreadAtenderTractor.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84. }
  85. public void escribirLog(String msg)
  86. {
  87. try {
  88. System.out.println("\t[Trac-"+this.clientSocket.getPort()+"]:"+msg);
  89. LogFileManager log = new LogFileManager(Defaults.SERVER_TRACTOR_LOGFILEPATH, true);
  90. log.writeTo("\t[Trac-"+this.clientSocket.getPort()+"]:"+msg);
  91. log.closeWriter();
  92. } catch (IOException ex) {
  93. JOptionPane.showMessageDialog(null, "IOException desde escribirLog() de "+this.getName(), "Exception", JOptionPane.ERROR_MESSAGE);
  94. Logger.getLogger(ThreadAtenderTractor.class.getName()).log(Level.SEVERE, null, ex);
  95. }
  96. }
  97. }