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    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   */
25  
26  package org.microemu.cldc.datagram;
27  
28  import java.io.IOException;
29  import java.net.DatagramSocket;
30  import java.net.InetAddress;
31  
32  import javax.microedition.io.Datagram;
33  import javax.microedition.io.DatagramConnection;
34  import javax.microedition.io.UDPDatagramConnection;
35  
36  import org.microemu.microedition.io.ConnectionImplementation;
37  
38  /**
39   * {@link ConnectionImplementation} for the datagram protocol (UDP).
40   */
41  public class Connection implements DatagramConnection, UDPDatagramConnection, ConnectionImplementation {
42  
43  	/**
44  	 * The datagram protocol constant
45  	 */
46  	public final static String PROTOCOL = "datagram://";
47  
48  	/**
49  	 * The encapsulated {@link DatagramSocket}
50  	 */
51  	private DatagramSocket socket;
52  
53  	/**
54  	 * The connection address in the format <tt>host:port</tt>
55  	 */
56  	private String address;
57  
58  	public void close() throws IOException {
59  		socket.close();
60  	}
61  
62  	public int getMaximumLength() throws IOException {
63  		return Math.min(socket.getReceiveBufferSize(), socket.getSendBufferSize());
64  	}
65  
66  	public int getNominalLength() throws IOException {
67  		return getMaximumLength();
68  	}
69  
70  	public void send(Datagram dgram) throws IOException {
71  		socket.send(((DatagramImpl) dgram).getDatagramPacket());
72  	}
73  
74  	public void receive(Datagram dgram) throws IOException {
75  		socket.receive(((DatagramImpl) dgram).getDatagramPacket());
76  	}
77  
78  	public Datagram newDatagram(int size) throws IOException {
79  		return newDatagram(size, address);
80  	}
81  
82  	public Datagram newDatagram(int size, String addr) throws IOException {
83  		Datagram datagram = new DatagramImpl(size);
84  		datagram.setAddress(addr);
85  		return datagram;
86  	}
87  
88  	public Datagram newDatagram(byte[] buf, int size) throws IOException {
89  		return newDatagram(buf, size, address);
90  	}
91  
92  	public Datagram newDatagram(byte[] buf, int size, String addr) throws IOException {
93  		Datagram datagram = new DatagramImpl(buf, size);
94  		datagram.setAddress(addr);
95  		return datagram;
96  	}
97  
98  	public String getLocalAddress() throws IOException {
99  		InetAddress address = socket.getInetAddress();
100 		if (address == null) {
101 			/*
102 			 * server mode we get the localhost from InetAddress otherwise we
103 			 * get '0.0.0.0'
104 			 */
105 			address = InetAddress.getLocalHost();
106 		} else {
107 			/*
108 			 * client mode we can get the localhost from the socket here
109 			 */
110 			address = socket.getLocalAddress();
111 		}
112 		return address.getHostAddress();
113 	}
114 
115 	public int getLocalPort() throws IOException {
116 		return socket.getLocalPort();
117 	}
118 
119 	public javax.microedition.io.Connection openConnection(String name, int mode, boolean timeouts) throws IOException {
120 		if (!org.microemu.cldc.http.Connection.isAllowNetworkConnection()) {
121 			throw new IOException("No network");
122 		}
123 		if (!name.startsWith(PROTOCOL)) {
124 			throw new IOException("Invalid Protocol " + name);
125 		}
126 		// TODO currently we ignore the mode
127 		address = name.substring(PROTOCOL.length());
128 		int port = -1;
129 		int index = address.indexOf(':');
130 		if (index == -1) {
131 			throw new IOException("port missing");
132 		}
133 		port = Integer.parseInt(address.substring(index + 1));
134 		if (index == 0) {
135 			// server mode
136 			socket = new DatagramSocket(port);
137 		} else {
138 			// client mode
139 			String host = address.substring(0, index);
140 			socket = new DatagramSocket();
141 			socket.connect(InetAddress.getByName(host), port);
142 		}
143 		return this;
144 	}
145 }