Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jacomma/icm/io/Connection.java


1   /*
2    * $Source: /home/data/cvsroot/src/jacomma/icm/io/Connection.java,v $
3    * $Revision: 1.4 $
4    * $Date: 2000/10/28 20:09:07 $
5    *
6    * This file is part of the jacomma framework
7    * Copyright (c) 2000   Dimitrios Vyzovitis
8    *      mailto:dviz@egnatia.ee.auth.gr
9    *      
10   *
11   *      
12   *      
13   *      
14   *
15   *  This library is free software; you can redistribute it and/or modify
16   *  it under the terms of the GNU Library General Public License as published by
17   *  the Free Software Foundation; either version 2 of the License, or
18   *  (at your option) any later version.
19   *
20   *  This library is distributed in the hope that it will be useful,
21   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23   *  GNU Library General Public License for more details.
24   *
25   *  You should have received a copy of the GNU Library General Public License
26   *  along with this library; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, 
28   *  Boston, MA  02111-1307  USA
29   */
30  
31  package jacomma.icm.io;
32  
33  import java.net.Socket;
34  import java.net.InetAddress;
35  
36  import jacomma.util.Environment;
37  
38  /**
39   * TBA
40   **/
41  public class Connection {
42    
43    public static final int DEFAULT_PORT = 4549;
44  
45    public static final Connection open() 
46      throws java.io.IOException, java.net.UnknownHostException {
47  
48      String cs = (String)Environment.getProperty( "icm.host" );
49      String ps =  (String)Environment.getProperty( "icm.port" );
50      
51      return open( ( cs == null ) ?
52             InetAddress.getLocalHost() : InetAddress.getByName( cs ),
53             ( ps == null ) ?
54             DEFAULT_PORT : Integer.valueOf( ps ).intValue() );
55      
56    }
57    
58    public static final Connection open( InetAddress host, int port ) 
59      throws java.io.IOException {
60      Socket sock = new Socket( host, port );
61      return new Connection( sock, 
62                   sock.getOutputStream(), 
63                   new java.io.BufferedInputStream( sock.getInputStream() ) );
64    }
65    
66    Socket sock_;
67    ObjectWriter writer_;
68    ObjectReader reader_;
69  
70    protected Connection( Socket sock, java.io.OutputStream out, java.io.InputStream in ) {
71      sock_ = sock;
72      writer_ = new ObjectWriter( out );
73      reader_ = new ObjectReader( in );
74    }
75    
76    public void writeObject( Object obj ) throws java.io.IOException {
77      writer_.writeObject( obj );
78    }
79    
80    public void writeObject( Object obj, Object hint )
81      throws java.io.IOException {
82      writer_.writeObject( obj, hint );
83    }
84    
85    public Object readObject() throws java.io.IOException {
86      return reader_.readObject();
87    }
88  
89    public Object readObject( Object hint ) throws java.io.IOException {
90      return reader_.readObject( hint );
91    }
92    
93    public boolean available() throws java.io.IOException {
94      return reader_.available() > 0;
95    }
96  
97    public void flush() throws java.io.IOException {
98      writer_.flush();
99    }
100   
101   public void close() throws java.io.IOException {
102     reader_.close();
103     writer_.close();
104     sock_.close(); // this might be unecessary
105   }
106 
107 
108 }