Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package client;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Scanner;
- import javax.swing.JTextArea;
- public class Downloader implements Runnable
- {
- private Socket socket;
- private Scanner in;
- private PrintWriter out;
- private Boolean running;
- private JTextArea lazio, roma;
- private LinkedList<Calciatore> calciatoriLazio, calciatoriRoma;
- public Downloader(Socket socket, JTextArea lazio, JTextArea roma)
- {
- this.calciatoriLazio = new LinkedList<Calciatore>();
- this.calciatoriRoma = new LinkedList<Calciatore>();
- this.socket = socket;
- this.lazio = lazio;
- this.roma = roma;
- try
- {
- in = new Scanner(new InputStreamReader(this.socket.getInputStream()));
- out = new PrintWriter(new OutputStreamWriter(this.socket.getOutputStream()));
- running = false;
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public void run()
- {
- if(!running)
- {
- running = true;
- out.println("start");
- out.flush();
- System.out.println("running");
- while(running)
- {
- if(in.hasNextLine())
- {
- String cmd = in.nextLine();
- if(!cmd.equals("+"))
- {
- String[] linea = cmd.split(",");
- if(linea.length == 3)
- {
- String nome = linea[0];
- String ruolo = linea[1];
- String squadra = linea[2];
- if(squadra.equals("Lazio"))
- {
- Calciatore c = new Calciatore(nome, ruolo, squadra);
- if(!calciatoriLazio.contains(c))
- calciatoriLazio.add(c);
- else
- printDuplicato("LAZIO");
- }
- else
- {
- Calciatore c = new Calciatore(nome, ruolo, squadra);
- if(!calciatoriRoma.contains(c))
- calciatoriRoma.add(c);
- else
- printDuplicato("roma");
- }
- }
- }
- }
- }
- }
- }
- public void stop()
- {
- running = false;
- out.println("stop");
- out.flush();
- }
- public void disconnect()
- {
- out.println("disconnect");
- out.flush();
- try
- {
- socket.close();
- out.close();
- in.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- public void lazio()
- {
- out.println("merda"); //lazio
- out.flush();
- printLazio();
- System.out.println("lazio");
- }
- public void roma()
- {
- out.println("roma");
- out.flush();
- printRoma();
- System.out.println("roma");
- }
- private void printLazio()
- {
- Collections.sort(calciatoriLazio);
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Iterator<Calciatore> it = calciatoriLazio.iterator();
- lazio.setText("");
- while(it.hasNext())
- lazio.setText(lazio.getText() + it.next() + "\n");
- }
- private void printRoma()
- {
- Collections.sort(calciatoriRoma);
- Iterator<Calciatore> it = calciatoriRoma.iterator();
- roma.setText("");
- while(it.hasNext())
- roma.setText(roma.getText() + it.next() + "\n");
- }
- private void printDuplicato(String squadra)
- {
- if(squadra == "LAZIO")
- {
- lazio.append("DUPLICATO RICEVUTO!\n");
- }
- else
- roma.append("DUPLICATO RICEVUTO!\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement