View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2006 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2007 Ludovic Dewailly <ludovic.dewailly@dreameffect.org>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  
21  package org.microemu.cldc.datagram;
22  
23  import java.io.IOException;
24  import java.net.DatagramSocket;
25  import java.net.InetAddress;
26  
27  import javax.microedition.io.Datagram;
28  import javax.microedition.io.DatagramConnection;
29  import javax.microedition.io.UDPDatagramConnection;
30  
31  import org.microemu.microedition.io.ConnectionImplementation;
32  
33  /**
34   * {@link ConnectionImplementation} for the datagram protocol (UDP).
35   */
36  public class Connection implements DatagramConnection, UDPDatagramConnection, ConnectionImplementation {
37  
38  	/**
39  	 * The datagram protocol constant
40  	 */
41  	public final static String PROTOCOL = "datagram://";
42  
43  	/**
44  	 * The encapsulated {@link DatagramSocket}
45  	 */
46  	private DatagramSocket socket;
47  
48  	/**
49  	 * The connection address in the format <tt>host:port</tt>
50  	 */
51  	private String address;
52  
53  	public void close() throws IOException {
54  		socket.close();
55  	}
56  
57  	public int getMaximumLength() throws IOException {
58  		return Math.min(socket.getReceiveBufferSize(), socket.getSendBufferSize());
59  	}
60  
61  	public int getNominalLength() throws IOException {
62  		return getMaximumLength();
63  	}
64  
65  	public void send(Datagram dgram) throws IOException {
66  		socket.send(((DatagramImpl) dgram).getDatagramPacket());
67  	}
68  
69  	public void receive(Datagram dgram) throws IOException {
70  		socket.receive(((DatagramImpl) dgram).getDatagramPacket());
71  	}
72  
73  	public Datagram newDatagram(int size) throws IOException {
74  		return newDatagram(size, address);
75  	}
76  
77  	public Datagram newDatagram(int size, String addr) throws IOException {
78  		Datagram datagram = new DatagramImpl(size);
79  		datagram.setAddress(addr);
80  		return datagram;
81  	}
82  
83  	public Datagram newDatagram(byte[] buf, int size) throws IOException {
84  		return newDatagram(buf, size, address);
85  	}
86  
87  	public Datagram newDatagram(byte[] buf, int size, String addr) throws IOException {
88  		Datagram datagram = new DatagramImpl(buf, size);
89  		datagram.setAddress(addr);
90  		return datagram;
91  	}
92  
93  	public String getLocalAddress() throws IOException {
94  		InetAddress address = socket.getInetAddress();
95  		if (address == null) {
96  			/*
97  			 * server mode we get the localhost from InetAddress otherwise we
98  			 * get '0.0.0.0'
99  			 */
100 			address = InetAddress.getLocalHost();
101 		} else {
102 			/*
103 			 * client mode we can get the localhost from the socket here
104 			 */
105 			address = socket.getLocalAddress();
106 		}
107 		return address.getHostAddress();
108 	}
109 
110 	public int getLocalPort() throws IOException {
111 		return socket.getLocalPort();
112 	}
113 
114 	public javax.microedition.io.Connection openConnection(String name, int mode, boolean timeouts) throws IOException {
115 		if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
116 			throw new IOException("No network");
117 		}
118 		if (!name.startsWith(PROTOCOL)) {
119 			throw new IOException("Invalid Protocol " + name);
120 		}
121 		// TODO currently we ignore the mode
122 		address = name.substring(PROTOCOL.length());
123 		int port = -1;
124 		int index = address.indexOf(':');
125 		if (index == -1) {
126 			throw new IOException("port missing");
127 		}
128 		port = Integer.parseInt(address.substring(index + 1));
129 		if (index == 0) {
130 			// server mode
131 			socket = new DatagramSocket(port);
132 		} else {
133 			// client mode
134 			String host = address.substring(0, index);
135 			socket = new DatagramSocket();
136 			socket.connect(InetAddress.getByName(host), port);
137 		}
138 		return this;
139 	}
140 }