/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package serwerek; import biblioteka.TytulKsiazki; import biblioteka.Uchwyt; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.net.Socket; /** * * @author michalek */ public class ObslugaKlienta implements Runnable, ActionListener { private Socket s; private Uchwyt dane; private ObjectOutputStream output; private ObjectInputStream input; private Object m = ""; public ObslugaKlienta(Socket s_, ObjectInputStream input_, ObjectOutputStream output_, Uchwyt dane) { s = s_; input = input_; output = output_; this.dane = dane; } public void actionPerformed(ActionEvent evt) { // Object zrodlo = evt.getSource(); // if (zrodlo == nazwa) { // m1 = nazwa.getText(); // if (!m1.equals("czesc") && s != null) { // try { // output.writeObject(m1); // } catch (Exception e) { // System.out.println("Wyjatek serwera2 " + e); // } // } // } // repaint(); } public void run() { String pom; try { //komentarz.setText("Serwer startuje na hoscie " // + InetAddress.getLocalHost().getHostName() + "\n"); while (true) { m = (String) input.readObject(); //pom = komentarz.getText(); //komentarz.setText(pom + "Odebrano wiadomosc od klienta: " + m + "\n"); //output.writeObject(m); if (m.equals("PING")) { output.writeObject("PONG"); } if (m.equals("GETTEXT")) { output.writeObject("ISBN?"); m = input.readObject(); if (m instanceof String) { if (m.equals("*")) { output.writeObject(this.dane.toString()); } else { output.writeObject(this.dane.szukaj(1, (String) m).toString()); } } } if (m.equals("GET")) { output.writeObject("ISBN?"); m = input.readObject(); if (m instanceof String) { if (m.equals("*")) { output.writeObject(this.dane); } else { output.writeObject(this.dane.szukaj(1, (String) m)); } } } if (m.equals("INSERT")) { m = input.readObject(); if (m instanceof String) { this.dane.dodajTytulKsiazki((String) m); output.writeObject("OK"); } if (m instanceof TytulKsiazki) { this.dane.dodajTytulKsiazki((TytulKsiazki) m); output.writeObject("OK"); } } if (m.equals("DELETE")) { output.writeObject("ISBN?"); m = input.readObject(); if (m instanceof String) { try { this.dane.usunTytulKsiazki((String) m); this.output.writeObject("Usunięto książkę o numerze " + m); } catch (Exception e) { this.output.writeObject(e.getLocalizedMessage()); } } } if (m.equals("LOGOUT")) { output.writeObject("LOGGED OUT"); break; } } input.close(); output.close(); s.close(); s = null; } catch (Exception e) { System.out.println("Wyjatek serwera " + e); } } }