/* * Copyright (C) 2008 Manish Pandya, [manish at meetamanish dot com] * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * */ package org.hooliguns.ninja.telnet; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; /** * A Staus publisher that opens the status socket, listens for status requests * and writes status responses to the requests. * * @author Manish Pandya (July 1 2008) * */ public class StatusPublisher implements Runnable { /** * The status request source */ private BufferedReader in = null; /** * The status response writer */ private BufferedWriter out = null; /** * The socket on which the status request/response are to be posted */ private ServerSocket statusServerSocket = null; /** * The command execution unite implementing Statefull that one can request * state of */ private Statefull statefull = null; /** * Creates a status publisher that is capable of responding to status requests * as a thread * * @param statusServerSocket * the status query socket * @param statefull * implementation of state listener, mostly a * {@link CommandExecutionUnit} whose state this publisher publishes */ public StatusPublisher(ServerSocket statusServerSocket, Statefull statefull) { super(); this.statusServerSocket = statusServerSocket; this.statefull = statefull; } /** * A method that opens the status socket, listens for status requests and * writes status responses to the requests. */ public void socketAcceptAndProcessStatusRequests() { if (statusServerSocket != null) { try { while (true) { Socket statusRequestSocket = statusServerSocket.accept(); in = new BufferedReader(new InputStreamReader(statusRequestSocket .getInputStream())); out = new BufferedWriter(new OutputStreamWriter(statusRequestSocket .getOutputStream())); while (in.readLine() != null) { out.write(statefull.getState()); out.write("\r\n"); out.flush(); } System.out.println("reconnect.."); try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } try { out.close(); } catch (IOException ex) { ex.printStackTrace(); } try { statusServerSocket.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ public void run() { socketAcceptAndProcessStatusRequests(); } }