lpr-b:serverthread
no way to compare when less than two revisions
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
— | lpr-b:serverthread [13/11/2007 alle 18:56 (18 anni fa)] (versione attuale) – creata Marco Danelutto | ||
---|---|---|---|
Linea 1: | Linea 1: | ||
+ | <code java> | ||
+ | package nslookup; | ||
+ | import java.io.IOException; | ||
+ | import java.io.ObjectInputStream; | ||
+ | import java.io.ObjectOutputStream; | ||
+ | import java.net.InetAddress; | ||
+ | import java.net.Socket; | ||
+ | import java.util.HashMap; | ||
+ | import java.util.Map; | ||
+ | |||
+ | public class ServerThread extends Thread { | ||
+ | |||
+ | Map< | ||
+ | Socket s = null; | ||
+ | |||
+ | public ServerThread(Map< | ||
+ | cache = c; | ||
+ | s = ts; | ||
+ | } | ||
+ | |||
+ | public void run() { | ||
+ | try { | ||
+ | System.out.println(" | ||
+ | ObjectInputStream ois = new ObjectInputStream(s.getInputStream()); | ||
+ | Object o = ois.readObject(); | ||
+ | if(o instanceof NameQuery) { | ||
+ | // trattamento della richiesta | ||
+ | NameQuery nq = (NameQuery) o; | ||
+ | String hostName = nq.getHostname(); | ||
+ | System.out.println(" | ||
+ | if(cache.containsKey(hostName) && nq.getCached()) { | ||
+ | System.out.println(hostName+" | ||
+ | Name)); | ||
+ | } else { // host non in cache o cached false | ||
+ | if(cache.containsKey(hostName)) { // se c'era, cancellalo prima di reineri | ||
+ | rlo | ||
+ | InetAddress ia = cache.get(hostName); | ||
+ | cache.remove(hostName); | ||
+ | System.out.println(" | ||
+ | ; | ||
+ | } | ||
+ | InetAddress ia = InetAddress.getByName(hostName); | ||
+ | cache.put(hostName, | ||
+ | System.out.println(" | ||
+ | } | ||
+ | // adesso invia la risposta | ||
+ | ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); | ||
+ | QueryAnswer qa = new QueryAnswer(hostName, | ||
+ | oos.writeObject(qa); | ||
+ | return; | ||
+ | } else { | ||
+ | // tipo non previsto, restituisce un messaggio di errore | ||
+ | ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream()); | ||
+ | System.out.println(" | ||
+ | QueryAnswer qa = new QueryAnswer(); | ||
+ | oos.writeObject(qa); | ||
+ | return; | ||
+ | } | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } catch (ClassNotFoundException e) { | ||
+ | System.out.println(" | ||
+ | return; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ |
lpr-b/serverthread.txt · Ultima modifica: 13/11/2007 alle 18:56 (18 anni fa) da Marco Danelutto