1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
40
41 public class Connection implements DatagramConnection, UDPDatagramConnection, ConnectionImplementation {
42
43
44
45
46 public final static String PROTOCOL = "datagram://";
47
48
49
50
51 private DatagramSocket socket;
52
53
54
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
103
104
105 address = InetAddress.getLocalHost();
106 } else {
107
108
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
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
136 socket = new DatagramSocket(port);
137 } else {
138
139 String host = address.substring(0, index);
140 socket = new DatagramSocket();
141 socket.connect(InetAddress.getByName(host), port);
142 }
143 return this;
144 }
145 }