OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [classpath/] [gnu/] [classpath/] [jdwp/] [transport/] [SocketTransport.java] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* SocketTransport.java -- a socket transport
2
   Copyright (C) 2005 Free Software Foundation
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
terms of your choice, provided that you also meet, for each linked
32
independent module, the terms and conditions of the license of that
33
module.  An independent module is a module which is not derived from
34
or based on this library.  If you modify this library, you may extend
35
this exception to your version of the library, but you are not
36
obligated to do so.  If you do not wish to do so, delete this
37
exception statement from your version. */
38
 
39
 
40
package gnu.classpath.jdwp.transport;
41
 
42
import gnu.classpath.jdwp.transport.ITransport;
43
import gnu.classpath.jdwp.transport.TransportException;
44
 
45
import java.io.InputStream;
46
import java.io.IOException;
47
import java.io.OutputStream;
48
import java.net.ServerSocket;
49
import java.net.Socket;
50
import java.util.HashMap;
51
 
52
import javax.net.ServerSocketFactory;
53
import javax.net.SocketFactory;
54
 
55
/**
56
 * A socket-based transport. This transport uses
57
 * configury string that looks like "name=dt_socket,
58
 * address=localhost:1234,server=y".
59
 *
60
 * @author Keith Seitz (keiths@redhat.com)
61
 */
62
class SocketTransport
63
  implements ITransport
64
{
65
  /**
66
   * Name of this transport
67
   */
68
  public static final String NAME = "dt_socket";
69
 
70
  // Configure properties
71
  private static final String _PROPERTY_ADDRESS = "address";
72
  private static final String _PROPERTY_SERVER = "server";
73
 
74
  // Port number
75
  private int _port;
76
 
77
  // Host name
78
  private String _host;
79
 
80
  // Are we acting as a server?
81
  private boolean _server = false;
82
 
83
  // Socket
84
  private Socket _socket;
85
 
86
  /**
87
   * Setup the connection configuration from the given properties
88
   *
89
   * @param  properties  the properties of the JDWP session
90
   * @throws TransportException for any configury errors
91
   */
92
  public void configure (HashMap properties)
93
    throws TransportException
94
  {
95
    // Get address [form: "hostname:port"]
96
    String p = (String) properties.get (_PROPERTY_ADDRESS);
97
    if (p != null)
98
      {
99
        String[] s = p.split (":");
100
        if (s.length == 2)
101
          {
102
            _host = s[0];
103
            _port = Integer.parseInt (s[1]);
104
          }
105
      }
106
 
107
    // Get server [form: "y" or "n"]
108
    p = (String) properties.get (_PROPERTY_SERVER);
109
    if (p != null)
110
      {
111
        if (p.toLowerCase().equals ("y"))
112
          _server = true;
113
      }
114
  }
115
 
116
  /**
117
   * Initialize this socket connection. This includes
118
   * connecting to the host (or listening for it).
119
   *
120
   * @throws TransportException if a transport-related error occurs
121
   */
122
  public void initialize ()
123
    throws TransportException
124
  {
125
    try
126
      {
127
        if (_server)
128
          {
129
            // Get a server socket
130
            ServerSocketFactory ssf = ServerSocketFactory.getDefault ();
131
            ServerSocket ss = ssf.createServerSocket (_port, 1);
132
            _socket = ss.accept ();
133
          }
134
        else
135
          {
136
            // Get a client socket (the factory will connect it)
137
            SocketFactory sf = SocketFactory.getDefault ();
138
            _socket = sf.createSocket (_host, _port);
139
          }
140
      }
141
    catch (IOException ioe)
142
      {
143
        // This will grab UnknownHostException, too.
144
        throw new TransportException (ioe);
145
      }
146
  }
147
 
148
  /**
149
   * Shutdown the socket. This could cause SocketExceptions
150
   * for anyone blocked on socket i/o
151
   */
152
  public void shutdown ()
153
  {
154
    try
155
      {
156
        _socket.close ();
157
      }
158
    catch (Throwable t)
159
      {
160
        // We don't really care about errors at this point
161
      }
162
  }
163
 
164
  /**
165
   * Returns an <code>InputStream</code> for the transport
166
   *
167
   * @throws IOException if an I/O error occurs creating the stream
168
   *                     or the socket is not connected
169
   */
170
  public InputStream getInputStream ()
171
    throws IOException
172
  {
173
    return _socket.getInputStream ();
174
  }
175
 
176
  /**
177
   * Returns an <code>OutputStream</code> for the transport
178
   *
179
   * @throws IOException if an I/O error occurs creating the stream
180
   *                     or the socket is not connected
181
   */
182
  public OutputStream getOutputStream ()
183
    throws IOException
184
  {
185
    return _socket.getOutputStream ();
186
  }
187
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.