sonixlabs-eventmachine-java 1.0.0.rc.5-java → 1.0.0.rc.7-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,29 +1,29 @@
1
1
  /**
2
2
  * $Id$
3
- *
3
+ *
4
4
  * Author:: Francis Cianfrocca (gmail: blackhedd)
5
5
  * Homepage:: http://rubyeventmachine.com
6
6
  * Date:: 15 Jul 2007
7
- *
7
+ *
8
8
  * See EventMachine and EventMachine::Connection for documentation and
9
9
  * usage examples.
10
- *
10
+ *
11
11
  *
12
12
  *----------------------------------------------------------------------------
13
13
  *
14
14
  * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15
15
  * Gmail: blackhedd
16
- *
16
+ *
17
17
  * This program is free software; you can redistribute it and/or modify
18
18
  * it under the terms of either: 1) the GNU General Public License
19
19
  * as published by the Free Software Foundation; either version 2 of the
20
20
  * License, or (at your option) any later version; or 2) Ruby's License.
21
- *
21
+ *
22
22
  * See the file COPYING for complete licensing information.
23
23
  *
24
24
  *---------------------------------------------------------------------------
25
25
  *
26
- *
26
+ *
27
27
  */
28
28
 
29
29
  package com.rubyeventmachine;
@@ -33,8 +33,8 @@ package com.rubyeventmachine;
33
33
  *
34
34
  */
35
35
  public class EmReactorException extends Exception {
36
- static final long serialVersionUID = 0;
37
- public EmReactorException (String msg) {
38
- super (msg);
39
- }
36
+ static final long serialVersionUID = 0;
37
+ public EmReactorException (String msg) {
38
+ super (msg);
39
+ }
40
40
  }
@@ -1,29 +1,29 @@
1
1
  /**
2
2
  * $Id$
3
- *
3
+ *
4
4
  * Author:: Francis Cianfrocca (gmail: blackhedd)
5
5
  * Homepage:: http://rubyeventmachine.com
6
6
  * Date:: 15 Jul 2007
7
- *
7
+ *
8
8
  * See EventMachine and EventMachine::Connection for documentation and
9
9
  * usage examples.
10
- *
10
+ *
11
11
  *
12
12
  *----------------------------------------------------------------------------
13
13
  *
14
14
  * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15
15
  * Gmail: blackhedd
16
- *
16
+ *
17
17
  * This program is free software; you can redistribute it and/or modify
18
18
  * it under the terms of either: 1) the GNU General Public License
19
19
  * as published by the Free Software Foundation; either version 2 of the
20
20
  * License, or (at your option) any later version; or 2) Ruby's License.
21
- *
21
+ *
22
22
  * See the file COPYING for complete licensing information.
23
23
  *
24
24
  *---------------------------------------------------------------------------
25
25
  *
26
- *
26
+ *
27
27
  */
28
28
 
29
29
 
@@ -34,37 +34,37 @@ import java.io.IOException;
34
34
  import java.nio.channels.ClosedChannelException;
35
35
 
36
36
  public interface EventableChannel {
37
-
38
- public void scheduleOutboundData (ByteBuffer bb);
39
-
40
- public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort);
41
-
42
- public boolean scheduleClose (boolean afterWriting);
43
-
44
- public void startTls();
45
-
46
- public long getBinding();
47
-
48
- public void readInboundData (ByteBuffer dst) throws IOException;
49
-
50
- public void register() throws ClosedChannelException;
51
-
52
- /**
53
- * This is called by the reactor after it finishes running.
54
- * The idea is to free network resources.
55
- */
56
- public void close();
57
-
58
- public boolean writeOutboundData() throws IOException;
59
-
60
- public void setCommInactivityTimeout (long seconds);
61
-
62
- public Object[] getPeerName();
63
- public Object[] getSockName();
64
-
65
- public boolean isWatchOnly();
66
-
67
- public boolean isNotifyReadable();
68
- public boolean isNotifyWritable();
37
+
38
+ public void scheduleOutboundData (ByteBuffer bb);
39
+
40
+ public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort);
41
+
42
+ public boolean scheduleClose (boolean afterWriting);
43
+
44
+ public void startTls();
45
+
46
+ public long getBinding();
47
+
48
+ public void readInboundData (ByteBuffer dst) throws IOException;
49
+
50
+ public void register() throws ClosedChannelException;
51
+
52
+ /**
53
+ * This is called by the reactor after it finishes running.
54
+ * The idea is to free network resources.
55
+ */
56
+ public void close();
57
+
58
+ public boolean writeOutboundData() throws IOException;
59
+
60
+ public void setCommInactivityTimeout (long seconds);
61
+
62
+ public Object[] getPeerName();
63
+ public Object[] getSockName();
64
+
65
+ public boolean isWatchOnly();
66
+
67
+ public boolean isNotifyReadable();
68
+ public boolean isNotifyWritable();
69
69
 
70
70
  }
@@ -1,29 +1,29 @@
1
1
  /**
2
2
  * $Id$
3
- *
3
+ *
4
4
  * Author:: Francis Cianfrocca (gmail: blackhedd)
5
5
  * Homepage:: http://rubyeventmachine.com
6
6
  * Date:: 15 Jul 2007
7
- *
7
+ *
8
8
  * See EventMachine and EventMachine::Connection for documentation and
9
9
  * usage examples.
10
- *
10
+ *
11
11
  *
12
12
  *----------------------------------------------------------------------------
13
13
  *
14
14
  * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15
15
  * Gmail: blackhedd
16
- *
16
+ *
17
17
  * This program is free software; you can redistribute it and/or modify
18
18
  * it under the terms of either: 1) the GNU General Public License
19
19
  * as published by the Free Software Foundation; either version 2 of the
20
20
  * License, or (at your option) any later version; or 2) Ruby's License.
21
- *
21
+ *
22
22
  * See the file COPYING for complete licensing information.
23
23
  *
24
24
  *---------------------------------------------------------------------------
25
25
  *
26
- *
26
+ *
27
27
  */
28
28
 
29
29
 
@@ -39,157 +39,157 @@ import java.io.*;
39
39
  import java.net.*;
40
40
 
41
41
  public class EventableDatagramChannel implements EventableChannel {
42
-
43
- class Packet {
44
- public ByteBuffer bb;
45
- public SocketAddress recipient;
46
- public Packet (ByteBuffer _bb, SocketAddress _recipient) {
47
- bb = _bb;
48
- recipient = _recipient;
49
- }
50
- }
51
-
52
- DatagramChannel channel;
53
- long binding;
54
- Selector selector;
55
- boolean bCloseScheduled;
56
- LinkedList<Packet> outboundQ;
57
- SocketAddress returnAddress;
58
-
59
-
60
- public EventableDatagramChannel (DatagramChannel dc, long _binding, Selector sel) throws ClosedChannelException {
61
- channel = dc;
62
- binding = _binding;
63
- selector = sel;
64
- bCloseScheduled = false;
65
- outboundQ = new LinkedList<Packet>();
66
-
67
- dc.register(selector, SelectionKey.OP_READ, this);
68
- }
69
-
70
- public void scheduleOutboundData (ByteBuffer bb) {
71
- try {
72
- if ((!bCloseScheduled) && (bb.remaining() > 0)) {
73
- outboundQ.addLast(new Packet(bb, returnAddress));
74
- channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this);
75
- }
76
- } catch (ClosedChannelException e) {
77
- throw new RuntimeException ("no outbound data");
78
- }
79
- }
80
-
81
- public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
82
- try {
83
- if ((!bCloseScheduled) && (bb.remaining() > 0)) {
84
- outboundQ.addLast(new Packet (bb, new InetSocketAddress (recipAddress, recipPort)));
85
- channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this);
86
- }
87
- } catch (ClosedChannelException e) {
88
- throw new RuntimeException ("no outbound data");
89
- }
90
- }
91
-
92
- public boolean scheduleClose (boolean afterWriting) {
93
- System.out.println ("NOT SCHEDULING CLOSE ON DATAGRAM");
94
- return false;
95
- }
96
-
97
- public void startTls() {
98
- throw new RuntimeException ("TLS is unimplemented on this Channel");
99
- }
100
-
101
- public long getBinding() {
102
- return binding;
103
- }
104
-
105
- public void register() throws ClosedChannelException {
106
- // TODO
107
- }
108
-
109
- /**
110
- * Terminate with extreme prejudice. Don't assume there will be another pass through
111
- * the reactor core.
112
- */
113
- public void close() {
114
- try {
115
- channel.close();
116
- } catch (IOException e) {
117
- }
118
- }
119
-
120
- public void readInboundData (ByteBuffer dst) {
121
- returnAddress = null;
122
- try {
123
- // If there is no datagram available (we're nonblocking after all),
124
- // then channel.receive returns null.
125
- returnAddress = channel.receive(dst);
126
- } catch (IOException e) {
127
- // probably a no-op. The caller will see the empty (or even partial) buffer
128
- // and presumably do the right thing.
129
- }
130
- }
131
-
132
- public boolean writeOutboundData() {
133
- while (!outboundQ.isEmpty()) {
134
- Packet p = outboundQ.getFirst();
135
- int written = 0;
136
- try {
137
- // With a datagram socket, it's ok to send an empty buffer.
138
- written = channel.send(p.bb, p.recipient);
139
- }
140
- catch (IOException e) {
141
- return false;
142
- }
143
-
144
- /* Did we consume the whole outbound buffer? If yes, pop it off and
145
- * keep looping. If no, the outbound network buffers are full, so break
146
- * out of here. There's a flaw that affects outbound buffers that are intentionally
147
- * empty. We can tell whether they got sent or not. So we assume they were.
148
- * TODO: As implemented, this ALWAYS discards packets if they were at least
149
- * partially written. This matches the behavior of the C++ EM. My judgment
150
- * is that this is less surprising than fragmenting the data and sending multiple
151
- * packets would be. I could be wrong, so this is subject to change.
152
- */
153
-
154
- if ((written > 0) || (p.bb.remaining() == 0))
155
- outboundQ.removeFirst();
156
- else
157
- break;
158
- }
159
-
160
- if (outboundQ.isEmpty()) {
161
- try {
162
- channel.register(selector, SelectionKey.OP_READ, this);
163
- } catch (ClosedChannelException e) {}
164
- }
165
-
166
- // ALWAYS drain the outbound queue before triggering a connection close.
167
- // If anyone wants to close immediately, they're responsible for clearing
168
- // the outbound queue.
169
- return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
170
- }
171
-
172
- public void setCommInactivityTimeout (long seconds) {
173
- // TODO
174
- System.out.println ("DATAGRAM: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
175
- }
176
-
177
- public Object[] getPeerName () {
178
- if (returnAddress != null) {
179
- InetSocketAddress inetAddr = (InetSocketAddress) returnAddress;
180
- return new Object[]{ inetAddr.getPort(), inetAddr.getHostName() };
181
- } else {
182
- return null;
183
- }
184
- }
185
-
186
- public Object[] getSockName () {
187
- DatagramSocket socket = channel.socket();
188
- return new Object[]{ socket.getLocalPort(),
189
- socket.getLocalAddress().getHostAddress() };
190
- }
191
-
192
- public boolean isWatchOnly() { return false; }
193
- public boolean isNotifyReadable() { return false; }
194
- public boolean isNotifyWritable() { return false; }
42
+
43
+ class Packet {
44
+ public ByteBuffer bb;
45
+ public SocketAddress recipient;
46
+ public Packet (ByteBuffer _bb, SocketAddress _recipient) {
47
+ bb = _bb;
48
+ recipient = _recipient;
49
+ }
50
+ }
51
+
52
+ DatagramChannel channel;
53
+ long binding;
54
+ Selector selector;
55
+ boolean bCloseScheduled;
56
+ LinkedList<Packet> outboundQ;
57
+ SocketAddress returnAddress;
58
+
59
+
60
+ public EventableDatagramChannel (DatagramChannel dc, long _binding, Selector sel) throws ClosedChannelException {
61
+ channel = dc;
62
+ binding = _binding;
63
+ selector = sel;
64
+ bCloseScheduled = false;
65
+ outboundQ = new LinkedList<Packet>();
66
+
67
+ dc.register(selector, SelectionKey.OP_READ, this);
68
+ }
69
+
70
+ public void scheduleOutboundData (ByteBuffer bb) {
71
+ try {
72
+ if ((!bCloseScheduled) && (bb.remaining() > 0)) {
73
+ outboundQ.addLast(new Packet(bb, returnAddress));
74
+ channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this);
75
+ }
76
+ } catch (ClosedChannelException e) {
77
+ throw new RuntimeException ("no outbound data");
78
+ }
79
+ }
80
+
81
+ public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
82
+ try {
83
+ if ((!bCloseScheduled) && (bb.remaining() > 0)) {
84
+ outboundQ.addLast(new Packet (bb, new InetSocketAddress (recipAddress, recipPort)));
85
+ channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ, this);
86
+ }
87
+ } catch (ClosedChannelException e) {
88
+ throw new RuntimeException ("no outbound data");
89
+ }
90
+ }
91
+
92
+ public boolean scheduleClose (boolean afterWriting) {
93
+ System.out.println ("NOT SCHEDULING CLOSE ON DATAGRAM");
94
+ return false;
95
+ }
96
+
97
+ public void startTls() {
98
+ throw new RuntimeException ("TLS is unimplemented on this Channel");
99
+ }
100
+
101
+ public long getBinding() {
102
+ return binding;
103
+ }
104
+
105
+ public void register() throws ClosedChannelException {
106
+ // TODO
107
+ }
108
+
109
+ /**
110
+ * Terminate with extreme prejudice. Don't assume there will be another pass through
111
+ * the reactor core.
112
+ */
113
+ public void close() {
114
+ try {
115
+ channel.close();
116
+ } catch (IOException e) {
117
+ }
118
+ }
119
+
120
+ public void readInboundData (ByteBuffer dst) {
121
+ returnAddress = null;
122
+ try {
123
+ // If there is no datagram available (we're nonblocking after all),
124
+ // then channel.receive returns null.
125
+ returnAddress = channel.receive(dst);
126
+ } catch (IOException e) {
127
+ // probably a no-op. The caller will see the empty (or even partial) buffer
128
+ // and presumably do the right thing.
129
+ }
130
+ }
131
+
132
+ public boolean writeOutboundData() {
133
+ while (!outboundQ.isEmpty()) {
134
+ Packet p = outboundQ.getFirst();
135
+ int written = 0;
136
+ try {
137
+ // With a datagram socket, it's ok to send an empty buffer.
138
+ written = channel.send(p.bb, p.recipient);
139
+ }
140
+ catch (IOException e) {
141
+ return false;
142
+ }
143
+
144
+ /* Did we consume the whole outbound buffer? If yes, pop it off and
145
+ * keep looping. If no, the outbound network buffers are full, so break
146
+ * out of here. There's a flaw that affects outbound buffers that are intentionally
147
+ * empty. We can tell whether they got sent or not. So we assume they were.
148
+ * TODO: As implemented, this ALWAYS discards packets if they were at least
149
+ * partially written. This matches the behavior of the C++ EM. My judgment
150
+ * is that this is less surprising than fragmenting the data and sending multiple
151
+ * packets would be. I could be wrong, so this is subject to change.
152
+ */
153
+
154
+ if ((written > 0) || (p.bb.remaining() == 0))
155
+ outboundQ.removeFirst();
156
+ else
157
+ break;
158
+ }
159
+
160
+ if (outboundQ.isEmpty()) {
161
+ try {
162
+ channel.register(selector, SelectionKey.OP_READ, this);
163
+ } catch (ClosedChannelException e) {}
164
+ }
165
+
166
+ // ALWAYS drain the outbound queue before triggering a connection close.
167
+ // If anyone wants to close immediately, they're responsible for clearing
168
+ // the outbound queue.
169
+ return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
170
+ }
171
+
172
+ public void setCommInactivityTimeout (long seconds) {
173
+ // TODO
174
+ System.out.println ("DATAGRAM: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
175
+ }
176
+
177
+ public Object[] getPeerName () {
178
+ if (returnAddress != null) {
179
+ InetSocketAddress inetAddr = (InetSocketAddress) returnAddress;
180
+ return new Object[]{ inetAddr.getPort(), inetAddr.getHostName() };
181
+ } else {
182
+ return null;
183
+ }
184
+ }
185
+
186
+ public Object[] getSockName () {
187
+ DatagramSocket socket = channel.socket();
188
+ return new Object[]{ socket.getLocalPort(),
189
+ socket.getLocalAddress().getHostAddress() };
190
+ }
191
+
192
+ public boolean isWatchOnly() { return false; }
193
+ public boolean isNotifyReadable() { return false; }
194
+ public boolean isNotifyWritable() { return false; }
195
195
  }