Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.ArrayList;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author luis-rei
- */
- public class ServerThread extends Thread {
- private final Socket sc;
- private ObjectOutputStream oOS;
- private ObjectInputStream oIS;
- public String ip;
- // ArrayLists do Servidor;
- public ArrayList<String> IPs;
- public ArrayList<String> Livros;
- public ServerThread(Socket sc, ArrayList<String> IPs, ArrayList<String> Livros)
- {
- this.sc = sc;
- this.IPs = new ArrayList<>();
- this.IPs = IPs;
- this.ip = "";
- this.Livros = new ArrayList<>();
- this.Livros = Livros;
- }
- @Override
- public void interrupt()
- {
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Thread has been interrupted!");
- }
- @Override
- public void run() {
- try
- {
- this.oOS = new ObjectOutputStream(this.sc.getOutputStream());
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] ObjectOutputStream criado com sucesso!");
- this.oIS = new ObjectInputStream(this.sc.getInputStream());
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] ObjectInputStream criado com sucesso!");
- this.ip = this.oIS.readUTF();
- IPs.add(this.ip);
- System.out.println("IP Address: " + this.ip);
- // Vai ler a opção que foi enviada pelo utilizador;
- String option = "";
- while(!(option = this.oIS.readUTF()).equals("exit"))
- {
- if(option.equals("offer_books"))
- {
- String bookName = oIS.readUTF();
- this.Livros.add(bookName);
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] O livro \"" + bookName + "\" foi adicionado com sucesso!");
- }
- else if(option.equals("take_books"))
- {
- String bookName = oIS.readUTF();
- while(true)
- {
- if(!this.Livros.contains(bookName))
- {
- oOS.writeUTF("take_books_notexists");
- oOS.flush();
- }
- else if(bookName.contains("none"))
- {
- break;
- }
- else
- {
- this.Livros.remove(bookName);
- oOS.writeUTF("take_books_success");
- oOS.flush();
- break;
- }
- }
- }
- else if(option.equals("list_books"))
- {
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Comando utilizado: list_books");
- oOS.writeUnshared(this.Livros);
- oOS.flush();
- for(int i = 0; i < this.Livros.size(); i++)
- {
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Livro: " + this.Livros.get(i));
- }
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Enviou a lista de livros!");
- }
- else
- {
- System.out.println("[LOG Thread " + Thread.currentThread().getName() + "] Comando desconhecido!");
- }
- }
- if(option.equals("exit"))
- {
- this.interrupt();
- this.sc.close();
- }
- }
- catch(IOException e)
- {
- System.out.println("IOException: " + e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement