1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
35
36 public class Connection implements DatagramConnection, UDPDatagramConnection, ConnectionImplementation {
37
38
39
40
41 public final static String PROTOCOL = "datagram://";
42
43
44
45
46 private DatagramSocket socket;
47
48
49
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
98
99
100 address = InetAddress.getLocalHost();
101 } else {
102
103
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
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
131 socket = new DatagramSocket(port);
132 } else {
133
134 String host = address.substring(0, index);
135 socket = new DatagramSocket();
136 socket.connect(InetAddress.getByName(host), port);
137 }
138 return this;
139 }
140 }