libc-eventmachine 0.12.5.42
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +195 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +138 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +15 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
- data/docs/PURE_RUBY +77 -0
- data/docs/README +74 -0
- data/docs/RELEASE_NOTES +96 -0
- data/docs/SMTP +9 -0
- data/docs/SPAWNED_PROCESSES +93 -0
- data/docs/TODO +10 -0
- data/ext/binder.cpp +126 -0
- data/ext/binder.h +48 -0
- data/ext/cmain.cpp +582 -0
- data/ext/cplusplus.cpp +177 -0
- data/ext/ed.cpp +1522 -0
- data/ext/ed.h +380 -0
- data/ext/em.cpp +1947 -0
- data/ext/em.h +186 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +98 -0
- data/ext/eventmachine_cpp.h +96 -0
- data/ext/extconf.rb +129 -0
- data/ext/fastfilereader/extconf.rb +77 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +82 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +351 -0
- data/ext/project.h +119 -0
- data/ext/rubymain.cpp +858 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +423 -0
- data/ext/ssl.h +90 -0
- data/java/src/com/rubyeventmachine/Application.java +196 -0
- data/java/src/com/rubyeventmachine/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
- data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
- data/lib/em/deferrable.rb +208 -0
- data/lib/em/eventable.rb +39 -0
- data/lib/em/future.rb +62 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/processes.rb +68 -0
- data/lib/em/spawnable.rb +88 -0
- data/lib/em/streamer.rb +112 -0
- data/lib/eventmachine.rb +1920 -0
- data/lib/eventmachine_version.rb +31 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/evma.rb +32 -0
- data/lib/jeventmachine.rb +140 -0
- data/lib/pr_eventmachine.rb +1017 -0
- data/lib/protocols/buftok.rb +127 -0
- data/lib/protocols/header_and_content.rb +129 -0
- data/lib/protocols/httpcli2.rb +803 -0
- data/lib/protocols/httpclient.rb +270 -0
- data/lib/protocols/line_and_text.rb +126 -0
- data/lib/protocols/linetext2.rb +161 -0
- data/lib/protocols/memcache.rb +293 -0
- data/lib/protocols/postgres.rb +261 -0
- data/lib/protocols/saslauth.rb +179 -0
- data/lib/protocols/smtpclient.rb +308 -0
- data/lib/protocols/smtpserver.rb +556 -0
- data/lib/protocols/stomp.rb +153 -0
- data/lib/protocols/tcptest.rb +57 -0
- data/tasks/cpp.rake +77 -0
- data/tasks/project.rake +78 -0
- data/tasks/tests.rake +193 -0
- data/tests/test_attach.rb +83 -0
- data/tests/test_basic.rb +231 -0
- data/tests/test_bind.rb +73 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +163 -0
- data/tests/test_error_handler.rb +32 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_eventables.rb +77 -0
- data/tests/test_exc.rb +58 -0
- data/tests/test_futures.rb +214 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +215 -0
- data/tests/test_httpclient2.rb +155 -0
- data/tests/test_kb.rb +61 -0
- data/tests/test_ltp.rb +188 -0
- data/tests/test_ltp2.rb +320 -0
- data/tests/test_next_tick.rb +109 -0
- data/tests/test_processes.rb +56 -0
- data/tests/test_pure.rb +129 -0
- data/tests/test_running.rb +47 -0
- data/tests/test_sasl.rb +74 -0
- data/tests/test_send_file.rb +243 -0
- data/tests/test_servers.rb +80 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +93 -0
- data/tests/test_spawn.rb +329 -0
- data/tests/test_ssl_args.rb +68 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_timers.rb +148 -0
- data/tests/test_ud.rb +43 -0
- data/tests/testem.rb +31 -0
- metadata +230 -0
@@ -0,0 +1,244 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
/**
|
30
|
+
*
|
31
|
+
*/
|
32
|
+
package com.rubyeventmachine;
|
33
|
+
|
34
|
+
/**
|
35
|
+
* @author francis
|
36
|
+
*
|
37
|
+
*/
|
38
|
+
|
39
|
+
import java.nio.channels.*;
|
40
|
+
import java.nio.*;
|
41
|
+
import java.util.*;
|
42
|
+
import java.io.*;
|
43
|
+
import javax.net.ssl.*;
|
44
|
+
import javax.net.ssl.SSLEngineResult.*;
|
45
|
+
|
46
|
+
import java.security.*;
|
47
|
+
|
48
|
+
public class EventableSocketChannel implements EventableChannel {
|
49
|
+
|
50
|
+
// TODO, must refactor this to permit channels that aren't sockets.
|
51
|
+
SocketChannel channel;
|
52
|
+
String binding;
|
53
|
+
Selector selector;
|
54
|
+
LinkedList<ByteBuffer> outboundQ;
|
55
|
+
boolean bCloseScheduled;
|
56
|
+
boolean bConnectPending;
|
57
|
+
|
58
|
+
SSLEngine sslEngine;
|
59
|
+
|
60
|
+
|
61
|
+
SSLContext sslContext;
|
62
|
+
|
63
|
+
|
64
|
+
public EventableSocketChannel (SocketChannel sc, String _binding, Selector sel) throws ClosedChannelException {
|
65
|
+
channel = sc;
|
66
|
+
binding = _binding;
|
67
|
+
selector = sel;
|
68
|
+
bCloseScheduled = false;
|
69
|
+
bConnectPending = false;
|
70
|
+
outboundQ = new LinkedList<ByteBuffer>();
|
71
|
+
|
72
|
+
sc.register(selector, SelectionKey.OP_READ, this);
|
73
|
+
}
|
74
|
+
|
75
|
+
public String getBinding() {
|
76
|
+
return binding;
|
77
|
+
}
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Terminate with extreme prejudice. Don't assume there will be another pass through
|
81
|
+
* the reactor core.
|
82
|
+
*/
|
83
|
+
public void close() {
|
84
|
+
try {
|
85
|
+
channel.close();
|
86
|
+
} catch (IOException e) {
|
87
|
+
}
|
88
|
+
}
|
89
|
+
|
90
|
+
public void scheduleOutboundData (ByteBuffer bb) {
|
91
|
+
try {
|
92
|
+
if ((!bCloseScheduled) && (bb.remaining() > 0)) {
|
93
|
+
if (sslEngine != null) {
|
94
|
+
ByteBuffer b = ByteBuffer.allocate(32*1024); // TODO, preallocate this buffer.
|
95
|
+
sslEngine.wrap(bb, b);
|
96
|
+
b.flip();
|
97
|
+
outboundQ.addLast(b);
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
outboundQ.addLast(bb);
|
101
|
+
}
|
102
|
+
channel.register(selector, SelectionKey.OP_WRITE | SelectionKey.OP_READ | (bConnectPending ? SelectionKey.OP_CONNECT : 0), this);
|
103
|
+
}
|
104
|
+
} catch (ClosedChannelException e) {
|
105
|
+
throw new RuntimeException ("no outbound data");
|
106
|
+
} catch (SSLException e) {
|
107
|
+
throw new RuntimeException ("no outbound data");
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
public void scheduleOutboundDatagram (ByteBuffer bb, String recipAddress, int recipPort) {
|
112
|
+
throw new RuntimeException ("datagram sends not supported on this channel");
|
113
|
+
}
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Called by the reactor when we have selected readable.
|
117
|
+
*/
|
118
|
+
public void readInboundData (ByteBuffer bb) {
|
119
|
+
try {
|
120
|
+
channel.read(bb);
|
121
|
+
} catch (IOException e) {
|
122
|
+
throw new RuntimeException ("i/o error");
|
123
|
+
}
|
124
|
+
}
|
125
|
+
/**
|
126
|
+
* Called by the reactor when we have selected writable.
|
127
|
+
* Return false to indicate an error that should cause the connection to close.
|
128
|
+
* We can get here with an empty outbound buffer if bCloseScheduled is true.
|
129
|
+
* TODO, VERY IMPORTANT: we're here because we selected writable, but it's always
|
130
|
+
* possible to become unwritable between the poll and when we get here. The way
|
131
|
+
* this code is written, we're depending on a nonblocking write NOT TO CONSUME
|
132
|
+
* the whole outbound buffer in this case, rather than firing an exception.
|
133
|
+
* We should somehow verify that this is indeed Java's defined behavior.
|
134
|
+
* Also TODO, see if we can use gather I/O rather than one write at a time.
|
135
|
+
* Ought to be a big performance enhancer.
|
136
|
+
* @return
|
137
|
+
*/
|
138
|
+
public boolean writeOutboundData(){
|
139
|
+
while (!outboundQ.isEmpty()) {
|
140
|
+
ByteBuffer b = outboundQ.getFirst();
|
141
|
+
try {
|
142
|
+
if (b.remaining() > 0)
|
143
|
+
channel.write(b);
|
144
|
+
}
|
145
|
+
catch (IOException e) {
|
146
|
+
return false;
|
147
|
+
}
|
148
|
+
|
149
|
+
// Did we consume the whole outbound buffer? If yes,
|
150
|
+
// pop it off and keep looping. If no, the outbound network
|
151
|
+
// buffers are full, so break out of here.
|
152
|
+
if (b.remaining() == 0)
|
153
|
+
outboundQ.removeFirst();
|
154
|
+
else
|
155
|
+
break;
|
156
|
+
}
|
157
|
+
|
158
|
+
if (outboundQ.isEmpty()) {
|
159
|
+
try {
|
160
|
+
channel.register(selector, SelectionKey.OP_READ, this);
|
161
|
+
} catch (ClosedChannelException e) {
|
162
|
+
}
|
163
|
+
}
|
164
|
+
|
165
|
+
// ALWAYS drain the outbound queue before triggering a connection close.
|
166
|
+
// If anyone wants to close immediately, they're responsible for clearing
|
167
|
+
// the outbound queue.
|
168
|
+
return (bCloseScheduled && outboundQ.isEmpty()) ? false : true;
|
169
|
+
}
|
170
|
+
|
171
|
+
public void setConnectPending() throws ClosedChannelException {
|
172
|
+
channel.register(selector, SelectionKey.OP_CONNECT, this);
|
173
|
+
bConnectPending = true;
|
174
|
+
}
|
175
|
+
|
176
|
+
/**
|
177
|
+
* Called by the reactor when we have selected connectable.
|
178
|
+
* Return false to indicate an error that should cause the connection to close.
|
179
|
+
* @throws ClosedChannelException
|
180
|
+
*/
|
181
|
+
public boolean finishConnecting() throws ClosedChannelException {
|
182
|
+
try {
|
183
|
+
channel.finishConnect();
|
184
|
+
}
|
185
|
+
catch (IOException e) {
|
186
|
+
return false;
|
187
|
+
}
|
188
|
+
bConnectPending = false;
|
189
|
+
channel.register(selector, SelectionKey.OP_READ | (outboundQ.isEmpty() ? 0 : SelectionKey.OP_WRITE), this);
|
190
|
+
return true;
|
191
|
+
}
|
192
|
+
|
193
|
+
public void scheduleClose (boolean afterWriting) {
|
194
|
+
// TODO: What the hell happens here if bConnectPending is set?
|
195
|
+
if (!afterWriting)
|
196
|
+
outboundQ.clear();
|
197
|
+
try {
|
198
|
+
channel.register(selector, SelectionKey.OP_READ|SelectionKey.OP_WRITE, this);
|
199
|
+
} catch (ClosedChannelException e) {
|
200
|
+
throw new RuntimeException ("unable to schedule close"); // TODO, get rid of this.
|
201
|
+
}
|
202
|
+
bCloseScheduled = true;
|
203
|
+
}
|
204
|
+
public void startTls() {
|
205
|
+
if (sslEngine == null) {
|
206
|
+
try {
|
207
|
+
sslContext = SSLContext.getInstance("TLS");
|
208
|
+
sslContext.init(null, null, null); // TODO, fill in the parameters.
|
209
|
+
sslEngine = sslContext.createSSLEngine(); // TODO, should use the parameterized version, to get Kerb stuff and session re-use.
|
210
|
+
sslEngine.setUseClientMode(false);
|
211
|
+
} catch (NoSuchAlgorithmException e) {
|
212
|
+
throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
|
213
|
+
} catch (KeyManagementException e) {
|
214
|
+
throw new RuntimeException ("unable to start TLS"); // TODO, get rid of this.
|
215
|
+
}
|
216
|
+
}
|
217
|
+
System.out.println ("Starting TLS");
|
218
|
+
}
|
219
|
+
|
220
|
+
public ByteBuffer dispatchInboundData (ByteBuffer bb) throws SSLException {
|
221
|
+
if (sslEngine != null) {
|
222
|
+
if (true) throw new RuntimeException ("TLS currently unimplemented");
|
223
|
+
System.setProperty("javax.net.debug", "all");
|
224
|
+
ByteBuffer w = ByteBuffer.allocate(32*1024); // TODO, WRONG, preallocate this buffer.
|
225
|
+
SSLEngineResult res = sslEngine.unwrap(bb, w);
|
226
|
+
if (res.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
|
227
|
+
Runnable r;
|
228
|
+
while ((r = sslEngine.getDelegatedTask()) != null) {
|
229
|
+
r.run();
|
230
|
+
}
|
231
|
+
}
|
232
|
+
System.out.println (bb);
|
233
|
+
w.flip();
|
234
|
+
return w;
|
235
|
+
}
|
236
|
+
else
|
237
|
+
return bb;
|
238
|
+
}
|
239
|
+
|
240
|
+
public void setCommInactivityTimeout (long seconds) {
|
241
|
+
// TODO
|
242
|
+
System.out.println ("SOCKET: SET COMM INACTIVITY UNIMPLEMENTED " + seconds);
|
243
|
+
}
|
244
|
+
}
|
@@ -0,0 +1,38 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
package com.rubyeventmachine;
|
31
|
+
|
32
|
+
public class PeriodicTimer extends Timer {
|
33
|
+
|
34
|
+
public void _fire() {
|
35
|
+
fire();
|
36
|
+
application.addTimer(interval, this);
|
37
|
+
}
|
38
|
+
}
|
@@ -0,0 +1,54 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
package com.rubyeventmachine;
|
31
|
+
|
32
|
+
public class Timer {
|
33
|
+
/**
|
34
|
+
* User code is expected to call a method on a controlling Application,
|
35
|
+
* which will fill in this field so subsequent user code can access it.
|
36
|
+
*/
|
37
|
+
public Application application;
|
38
|
+
public double interval;
|
39
|
+
|
40
|
+
/**
|
41
|
+
* The reactor calls here, and it may be overridden in subclasses.
|
42
|
+
* User code should never call this method.
|
43
|
+
*/
|
44
|
+
public void _fire() {
|
45
|
+
fire();
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* User code is expected to override this method.
|
50
|
+
*/
|
51
|
+
public void fire() {
|
52
|
+
}
|
53
|
+
|
54
|
+
}
|
@@ -0,0 +1,108 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
package com.rubyeventmachine.tests;
|
32
|
+
|
33
|
+
|
34
|
+
import org.junit.After;
|
35
|
+
import org.junit.AfterClass;
|
36
|
+
import org.junit.Before;
|
37
|
+
import org.junit.BeforeClass;
|
38
|
+
import org.junit.Test;
|
39
|
+
import org.junit.Assert;
|
40
|
+
import java.net.*;
|
41
|
+
import java.io.*;
|
42
|
+
import java.nio.*;
|
43
|
+
|
44
|
+
import com.rubyeventmachine.*;
|
45
|
+
|
46
|
+
public class ApplicationTest {
|
47
|
+
|
48
|
+
@BeforeClass
|
49
|
+
public static void setUpBeforeClass() throws Exception {
|
50
|
+
}
|
51
|
+
|
52
|
+
@AfterClass
|
53
|
+
public static void tearDownAfterClass() throws Exception {
|
54
|
+
}
|
55
|
+
|
56
|
+
@Before
|
57
|
+
public void setUp() throws Exception {
|
58
|
+
}
|
59
|
+
|
60
|
+
@After
|
61
|
+
public void tearDown() throws Exception {
|
62
|
+
}
|
63
|
+
|
64
|
+
@Test
|
65
|
+
public void testRunnableArgument() {
|
66
|
+
final Application a = new Application();
|
67
|
+
a.run (new Runnable() {
|
68
|
+
public void run() {
|
69
|
+
a.stop();
|
70
|
+
}
|
71
|
+
});
|
72
|
+
}
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
class F implements ConnectionFactory {
|
77
|
+
public Connection connection() {
|
78
|
+
return new Connection() {
|
79
|
+
public void receiveData (ByteBuffer bb) {
|
80
|
+
application.stop();
|
81
|
+
}
|
82
|
+
};
|
83
|
+
}
|
84
|
+
|
85
|
+
};
|
86
|
+
|
87
|
+
@Test
|
88
|
+
public void testTcpServer() throws EmReactorException {
|
89
|
+
final Application a = new Application();
|
90
|
+
final SocketAddress saddr = new InetSocketAddress ("127.0.0.1", 9008);
|
91
|
+
a.run (new Runnable() {
|
92
|
+
public void run() {
|
93
|
+
try {
|
94
|
+
a.startServer (saddr, new F());
|
95
|
+
} catch (EmReactorException e) { Assert.fail(); }
|
96
|
+
new Thread() {
|
97
|
+
public void run() {
|
98
|
+
try {
|
99
|
+
Socket s = new Socket ("127.0.0.1", 9008);
|
100
|
+
s.getOutputStream().write(new String ("boo").getBytes());
|
101
|
+
} catch (UnknownHostException e) {
|
102
|
+
} catch (IOException e) {}
|
103
|
+
}
|
104
|
+
}.start();
|
105
|
+
}
|
106
|
+
});
|
107
|
+
}
|
108
|
+
}
|
@@ -0,0 +1,124 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
package com.rubyeventmachine.tests;
|
31
|
+
|
32
|
+
|
33
|
+
import org.junit.After;
|
34
|
+
import org.junit.AfterClass;
|
35
|
+
import org.junit.Before;
|
36
|
+
import org.junit.BeforeClass;
|
37
|
+
import org.junit.Test;
|
38
|
+
import java.io.*;
|
39
|
+
import java.nio.*;
|
40
|
+
import java.nio.channels.*;
|
41
|
+
|
42
|
+
import com.rubyeventmachine.*;
|
43
|
+
|
44
|
+
public class ConnectTest {
|
45
|
+
|
46
|
+
@BeforeClass
|
47
|
+
public static void setUpBeforeClass() throws Exception {
|
48
|
+
}
|
49
|
+
|
50
|
+
@AfterClass
|
51
|
+
public static void tearDownAfterClass() throws Exception {
|
52
|
+
}
|
53
|
+
|
54
|
+
@Before
|
55
|
+
public void setUp() throws Exception {
|
56
|
+
}
|
57
|
+
|
58
|
+
@After
|
59
|
+
public void tearDown() throws Exception {
|
60
|
+
}
|
61
|
+
|
62
|
+
@Test
|
63
|
+
public final void test1() throws IOException, ClosedChannelException {
|
64
|
+
Application a = new Application();
|
65
|
+
a.addTimer(0, new Timer() {
|
66
|
+
public void fire() {
|
67
|
+
application.connect("www.bayshorenetworks.com", 80, new Connection() {
|
68
|
+
public void connectionCompleted() {
|
69
|
+
close();
|
70
|
+
}
|
71
|
+
public void unbind() {
|
72
|
+
application.stop();
|
73
|
+
}
|
74
|
+
});
|
75
|
+
}
|
76
|
+
});
|
77
|
+
a.run();
|
78
|
+
}
|
79
|
+
|
80
|
+
@Test
|
81
|
+
public final void test2() throws IOException {
|
82
|
+
class Bays extends Connection {
|
83
|
+
public void connectionCompleted() {
|
84
|
+
sendData (ByteBuffer.wrap( new String ("GET / HTTP/1.1\r\nHost: _\r\n\r\n").getBytes()));
|
85
|
+
}
|
86
|
+
public void receiveData (ByteBuffer b) {
|
87
|
+
System.out.println (new String(b.array()));
|
88
|
+
application.stop();
|
89
|
+
}
|
90
|
+
};
|
91
|
+
|
92
|
+
Application a = new Application();
|
93
|
+
a.addTimer(0, new Timer() {
|
94
|
+
public void fire() {
|
95
|
+
application.connect("www.bayshorenetworks.com", 80, new Bays());
|
96
|
+
}
|
97
|
+
});
|
98
|
+
a.run();
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
class C1 extends Connection {
|
104
|
+
Application application;
|
105
|
+
public C1 (Application a) {
|
106
|
+
application = a;
|
107
|
+
}
|
108
|
+
public void postInit() {
|
109
|
+
application.stop();
|
110
|
+
}
|
111
|
+
}
|
112
|
+
@Test
|
113
|
+
public final void test3() {
|
114
|
+
final Application a = new Application();
|
115
|
+
a.run (new Runnable() {
|
116
|
+
public void run() {
|
117
|
+
a.connect("www.bayshorenetworks.com", 80, new C1(a));
|
118
|
+
}
|
119
|
+
});
|
120
|
+
}
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
}
|
@@ -0,0 +1,80 @@
|
|
1
|
+
/**
|
2
|
+
* $Id$
|
3
|
+
*
|
4
|
+
* Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
* Homepage:: http://rubyeventmachine.com
|
6
|
+
* Date:: 15 Jul 2007
|
7
|
+
*
|
8
|
+
* See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
* usage examples.
|
10
|
+
*
|
11
|
+
*
|
12
|
+
*----------------------------------------------------------------------------
|
13
|
+
*
|
14
|
+
* Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
15
|
+
* Gmail: blackhedd
|
16
|
+
*
|
17
|
+
* This program is free software; you can redistribute it and/or modify
|
18
|
+
* it under the terms of either: 1) the GNU General Public License
|
19
|
+
* as published by the Free Software Foundation; either version 2 of the
|
20
|
+
* License, or (at your option) any later version; or 2) Ruby's License.
|
21
|
+
*
|
22
|
+
* See the file COPYING for complete licensing information.
|
23
|
+
*
|
24
|
+
*---------------------------------------------------------------------------
|
25
|
+
*
|
26
|
+
*
|
27
|
+
*/
|
28
|
+
|
29
|
+
|
30
|
+
package com.rubyeventmachine.tests;
|
31
|
+
|
32
|
+
//import static org.junit.Assert.*;
|
33
|
+
|
34
|
+
import org.junit.After;
|
35
|
+
import org.junit.AfterClass;
|
36
|
+
import org.junit.Before;
|
37
|
+
import org.junit.BeforeClass;
|
38
|
+
import org.junit.Test;
|
39
|
+
|
40
|
+
import com.rubyeventmachine.EmReactor;
|
41
|
+
|
42
|
+
import java.io.*;
|
43
|
+
import java.nio.*;
|
44
|
+
|
45
|
+
|
46
|
+
public class EMTest {
|
47
|
+
|
48
|
+
class ShortTimer extends EmReactor {
|
49
|
+
public void eventCallback (String sig, int eventCode, ByteBuffer data) {
|
50
|
+
System.out.println ("Short Callback "+sig+" "+eventCode+" "+data);
|
51
|
+
this.stop();
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
@BeforeClass
|
56
|
+
public static void setUpBeforeClass() throws Exception {
|
57
|
+
}
|
58
|
+
|
59
|
+
@AfterClass
|
60
|
+
public static void tearDownAfterClass() throws Exception {
|
61
|
+
}
|
62
|
+
|
63
|
+
@Before
|
64
|
+
public void setUp() throws Exception {
|
65
|
+
}
|
66
|
+
|
67
|
+
@After
|
68
|
+
public void tearDown() throws Exception {
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
@Test
|
73
|
+
public final void testOneShort() throws IOException {
|
74
|
+
EmReactor em = new ShortTimer();
|
75
|
+
em.installOneshotTimer(1050);
|
76
|
+
em.run();
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
}
|
@@ -0,0 +1,53 @@
|
|
1
|
+
package com.rubyeventmachine.tests;
|
2
|
+
|
3
|
+
import com.rubyeventmachine.*;
|
4
|
+
import java.net.*;
|
5
|
+
import java.nio.*;
|
6
|
+
|
7
|
+
import org.junit.After;
|
8
|
+
import org.junit.AfterClass;
|
9
|
+
import org.junit.Before;
|
10
|
+
import org.junit.BeforeClass;
|
11
|
+
import org.junit.Test;
|
12
|
+
|
13
|
+
|
14
|
+
public class TestDatagrams {
|
15
|
+
|
16
|
+
@BeforeClass
|
17
|
+
public static void setUpBeforeClass() throws Exception {
|
18
|
+
}
|
19
|
+
|
20
|
+
@AfterClass
|
21
|
+
public static void tearDownAfterClass() throws Exception {
|
22
|
+
}
|
23
|
+
|
24
|
+
@Before
|
25
|
+
public void setUp() throws Exception {
|
26
|
+
}
|
27
|
+
|
28
|
+
@After
|
29
|
+
public void tearDown() throws Exception {
|
30
|
+
}
|
31
|
+
|
32
|
+
class A extends Connection {
|
33
|
+
public void receiveData (ByteBuffer bb) {
|
34
|
+
application.stop();
|
35
|
+
}
|
36
|
+
}
|
37
|
+
class B extends Connection {
|
38
|
+
public void postInit() {
|
39
|
+
this.sendDatagram(ByteBuffer.wrap(new String("ABC").getBytes()), new InetSocketAddress ("127.0.0.1", 9550));
|
40
|
+
}
|
41
|
+
|
42
|
+
}
|
43
|
+
@Test
|
44
|
+
public final void testA() {
|
45
|
+
final Application a = new Application();
|
46
|
+
a.run (new Runnable() {
|
47
|
+
public void run() {
|
48
|
+
a.openDatagramSocket( new InetSocketAddress ("0.0.0.0", 9550), new A() );
|
49
|
+
a.openDatagramSocket( new B() );
|
50
|
+
}
|
51
|
+
});
|
52
|
+
}
|
53
|
+
}
|