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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [gnu/] [classpath/] [jdwp/] [transport/] [SocketTransport.java] - Blame information for rev 769

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 769 jeremybenn
/* SocketTransport.java -- a socket transport
2
   Copyright (C) 2005, 2007 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 server [form: "y" or "n"]
96
    String p = (String) properties.get(_PROPERTY_SERVER);
97
    if (p != null)
98
      {
99
        if (p.toLowerCase().equals("y"))
100
          _server = true;
101
      }
102
 
103
    // Get address [form: "hostname:port"]
104
    p = (String) properties.get(_PROPERTY_ADDRESS);
105
    if (p != null)
106
      {
107
        String[] s = p.split(":");
108
        if (s.length == 1)
109
          {
110
            // Port number only. Assume "localhost"
111
            _port = Integer.parseInt(s[0]);
112
            _host = "localhost";
113
          }
114
        else
115
          {
116
            if (s[0].length() == 0)
117
              _host = "localhost";
118
            else
119
              _host = s[0];
120
            _port = Integer.parseInt(s[1]);
121
          }
122
      }
123
  }
124
 
125
  /**
126
   * Initialize this socket connection. This includes
127
   * connecting to the host (or listening for it).
128
   *
129
   * @throws TransportException if a transport-related error occurs
130
   */
131
  public void initialize ()
132
    throws TransportException
133
  {
134
    try
135
      {
136
        if (_server)
137
          {
138
            // Get a server socket
139
            ServerSocketFactory ssf = ServerSocketFactory.getDefault ();
140
            ServerSocket ss = ssf.createServerSocket (_port, 1);
141
            _socket = ss.accept ();
142
          }
143
        else
144
          {
145
            // Get a client socket (the factory will connect it)
146
            SocketFactory sf = SocketFactory.getDefault ();
147
            _socket = sf.createSocket (_host, _port);
148
          }
149
      }
150
    catch (IOException ioe)
151
      {
152
        // This will grab UnknownHostException, too.
153
        throw new TransportException (ioe);
154
      }
155
  }
156
 
157
  /**
158
   * Shutdown the socket. This could cause SocketExceptions
159
   * for anyone blocked on socket i/o
160
   */
161
  public void shutdown ()
162
  {
163
    try
164
      {
165
        _socket.close ();
166
      }
167
    catch (Throwable t)
168
      {
169
        // We don't really care about errors at this point
170
      }
171
  }
172
 
173
  /**
174
   * Returns an <code>InputStream</code> for the transport
175
   *
176
   * @throws IOException if an I/O error occurs creating the stream
177
   *                     or the socket is not connected
178
   */
179
  public InputStream getInputStream ()
180
    throws IOException
181
  {
182
    return _socket.getInputStream ();
183
  }
184
 
185
  /**
186
   * Returns an <code>OutputStream</code> for the transport
187
   *
188
   * @throws IOException if an I/O error occurs creating the stream
189
   *                     or the socket is not connected
190
   */
191
  public OutputStream getOutputStream ()
192
    throws IOException
193
  {
194
    return _socket.getOutputStream ();
195
  }
196
}

powered by: WebSVN 2.1.0

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