Client.java |
1 package com.palantir.blog.processspawner; 2 3 /* 4 * All source code and information in this file is made 5 * available under the following licensing terms: 6 * 7 * Copyright (c) 2009, Palantir Technologies, Inc. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are 12 * met: 13 * 14 * * Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 17 * * Redistributions in binary form must reproduce the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer in the documentation and/or other materials provided 20 * with the distribution. 21 * 22 * * Neither the name of Palantir Technologies, Inc. nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 * 38 * 39 */ 40 41 import java.io.BufferedReader; 42 import java.io.IOException; 43 import java.io.InputStreamReader; 44 import java.io.OutputStreamWriter; 45 import java.io.Writer; 46 import java.net.InetAddress; 47 import java.net.Socket; 48 49 import javax.net.SocketFactory; 50 51 /** 52 * A simple network client. 53 * 54 * @author regs 55 * 56 */ 57 public class Client { 58 59 final Socket connection; 60 final BufferedReader r; 61 final Writer w; 62 63 public Client(InetAddress connectTo, int port) throws IOException { 64 this.connection = SocketFactory.getDefault().createSocket(connectTo,port); 65 this.connection.setTcpNoDelay(true); 66 67 r = new BufferedReader(new InputStreamReader(this.connection.getInputStream())); 68 w = new OutputStreamWriter(this.connection.getOutputStream()); 69 70 } 71 72 public String sendMessage(String msg) throws IOException { 73 if(!this.connection.isConnected()) { 74 throw new IllegalStateException("Socket is not connected!"); 75 } 76 try { 77 w.write(msg + "\n"); 78 w.flush(); 79 String rc = r.readLine(); 80 return rc; 81 } finally { 82 83 } 84 } 85 86 public void close() { 87 try { 88 r.close(); 89 } catch(Exception e) { 90 // close quietly 91 } 92 try { 93 w.close(); 94 } catch(Exception e) { 95 // close quietly 96 } 97 try { 98 connection.close(); 99 } 100 catch(Exception e) { 101 // close quietly 102 } 103 } 104 105 @Override 106 protected void finalize() throws Throwable { 107 super.finalize(); 108 close(); 109 } 110 111 public static final void sendShutdown(InetAddress connectTo, int port) throws IOException { 112 Client c = new Client(connectTo,port); 113 c.sendMessage(Server.SHUTDOWN); 114 } 115} 116