Advertisement
Guest User

ServerThread

a guest
Apr 5th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.36 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.net.Socket;
  6. import java.util.ArrayList;
  7.  
  8. /*
  9.  * To change this license header, choose License Headers in Project Properties.
  10.  * To change this template file, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /**
  15.  *
  16.  * @author luis-rei
  17.  */
  18. public class ServerThread extends Thread {
  19.    
  20.     private final Socket sc;
  21.     private ObjectOutputStream oOS;
  22.     private ObjectInputStream oIS;
  23.     public String ip;
  24.    
  25.     // ArrayLists do Servidor;
  26.     public ArrayList<String> IPs;
  27.     public ArrayList<String> Livros;
  28.    
  29.     public ServerThread(Socket sc, ArrayList<String> IPs, ArrayList<String> Livros)
  30.     {
  31.         this.sc = sc;
  32.         this.IPs = new ArrayList<>();
  33.         this.IPs = IPs;
  34.         this.ip = "";
  35.         this.Livros = new ArrayList<>();
  36.         this.Livros = Livros;
  37.     }
  38.    
  39.     @Override
  40.     public void interrupt()
  41.     {
  42.         System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Thread has been interrupted!");
  43.     }
  44.    
  45.     @Override
  46.     public void run() {
  47.         try
  48.         {
  49.             this.oOS = new ObjectOutputStream(this.sc.getOutputStream());
  50.             System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] ObjectOutputStream criado com sucesso!");
  51.  
  52.  
  53.             this.oIS = new ObjectInputStream(this.sc.getInputStream());
  54.             System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] ObjectInputStream criado com sucesso!");
  55.            
  56.             this.ip = this.oIS.readUTF();
  57.            
  58.             IPs.add(this.ip);
  59.            
  60.             System.out.println("IP Address: " + this.ip);
  61.            
  62.             // Vai ler a opção que foi enviada pelo utilizador;
  63.             String option = "";
  64.            
  65.             while(!(option = this.oIS.readUTF()).equals("exit"))
  66.             {
  67.                 if(option.equals("offer_books"))
  68.                 {
  69.                     String bookName = oIS.readUTF();
  70.                     this.Livros.add(bookName);
  71.                     System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] O livro \"" + bookName + "\" foi adicionado com sucesso!");
  72.                 }
  73.                 else if(option.equals("take_books"))
  74.                 {
  75.                     String bookName = oIS.readUTF();
  76.  
  77.                     while(true)
  78.                     {
  79.                         if(!this.Livros.contains(bookName))
  80.                         {
  81.                             oOS.writeUTF("take_books_notexists");
  82.                             oOS.flush();
  83.                         }
  84.                         else if(bookName.contains("none"))
  85.                         {
  86.                             break;
  87.                         }
  88.                         else
  89.                         {
  90.                             this.Livros.remove(bookName);
  91.                             oOS.writeUTF("take_books_success");
  92.                             oOS.flush();
  93.                             break;
  94.                         }
  95.                     }
  96.                 }
  97.                 else if(option.equals("list_books"))
  98.                 {
  99.                     System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Comando utilizado: list_books");              
  100.  
  101.                     oOS.writeUnshared(this.Livros);
  102.                     oOS.flush();
  103.                    
  104.                     for(int i = 0; i < this.Livros.size(); i++)
  105.                     {
  106.                         System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Livro: " + this.Livros.get(i));
  107.                     }
  108.  
  109.                     System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Enviou a lista de livros!");
  110.                 }
  111.                 else
  112.                 {
  113.                     System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Comando desconhecido!");
  114.                 }
  115.                
  116.             }
  117.            
  118.             if(option.equals("exit"))
  119.             {
  120.                 this.interrupt();
  121.                 this.sc.close();
  122.             }
  123.         }
  124.         catch(IOException e)
  125.         {
  126.             System.out.println("IOException: " + e.getMessage());
  127.         }
  128.     }
  129.    
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement