eventmachine 0.12.2 → 0.12.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/Rakefile +191 -0
  2. data/{COPYING → docs/COPYING} +0 -0
  3. data/docs/ChangeLog +183 -0
  4. data/{DEFERRABLES → docs/DEFERRABLES} +1 -1
  5. data/{EPOLL → docs/EPOLL} +0 -0
  6. data/{GNU → docs/GNU} +0 -0
  7. data/docs/INSTALL +15 -0
  8. data/{KEYBOARD → docs/KEYBOARD} +0 -0
  9. data/{LEGAL → docs/LEGAL} +0 -0
  10. data/{LIGHTWEIGHT_CONCURRENCY → docs/LIGHTWEIGHT_CONCURRENCY} +1 -1
  11. data/{PURE_RUBY → docs/PURE_RUBY} +1 -1
  12. data/{README → docs/README} +1 -1
  13. data/{RELEASE_NOTES → docs/RELEASE_NOTES} +1 -1
  14. data/{SMTP → docs/SMTP} +1 -1
  15. data/{SPAWNED_PROCESSES → docs/SPAWNED_PROCESSES} +1 -1
  16. data/{TODO → docs/TODO} +1 -1
  17. data/ext/binder.cpp +1 -1
  18. data/ext/binder.h +1 -1
  19. data/ext/cmain.cpp +65 -74
  20. data/ext/cplusplus.cpp +1 -1
  21. data/ext/ed.cpp +41 -25
  22. data/ext/ed.h +10 -3
  23. data/ext/em.cpp +39 -12
  24. data/ext/em.h +13 -1
  25. data/ext/emwin.cpp +1 -1
  26. data/ext/emwin.h +1 -1
  27. data/ext/epoll.cpp +1 -1
  28. data/ext/epoll.h +1 -1
  29. data/ext/eventmachine.h +1 -1
  30. data/ext/eventmachine_cpp.h +1 -1
  31. data/ext/extconf.rb +86 -139
  32. data/ext/fastfilereader/extconf.rb +161 -0
  33. data/ext/fastfilereader/mapper.cpp +202 -0
  34. data/ext/fastfilereader/mapper.h +59 -0
  35. data/ext/fastfilereader/rubymain.cpp +127 -0
  36. data/ext/files.cpp +1 -1
  37. data/ext/files.h +1 -1
  38. data/ext/kb.cpp +1 -1
  39. data/ext/page.cpp +1 -1
  40. data/ext/page.h +1 -1
  41. data/ext/pipe.cpp +14 -7
  42. data/ext/project.h +1 -1
  43. data/ext/rubymain.cpp +56 -2
  44. data/ext/sigs.cpp +1 -1
  45. data/ext/sigs.h +1 -1
  46. data/ext/ssl.cpp +1 -1
  47. data/ext/ssl.h +1 -1
  48. data/java/src/com/rubyeventmachine/Application.java +196 -0
  49. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  50. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  51. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  52. data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
  53. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  54. data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
  55. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
  56. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
  57. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  58. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  59. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  60. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
  61. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  62. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  63. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  64. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  65. data/lib/em/deferrable.rb +1 -1
  66. data/lib/em/eventable.rb +1 -1
  67. data/lib/em/future.rb +1 -1
  68. data/lib/em/messages.rb +1 -1
  69. data/lib/em/processes.rb +1 -1
  70. data/lib/em/spawnable.rb +1 -1
  71. data/lib/em/streamer.rb +1 -1
  72. data/lib/eventmachine.rb +138 -116
  73. data/lib/eventmachine_version.rb +2 -2
  74. data/lib/evma.rb +1 -1
  75. data/lib/evma/callback.rb +1 -1
  76. data/lib/evma/container.rb +1 -1
  77. data/lib/evma/factory.rb +1 -1
  78. data/lib/evma/protocol.rb +1 -1
  79. data/lib/evma/reactor.rb +1 -1
  80. data/lib/jeventmachine.rb +7 -2
  81. data/lib/pr_eventmachine.rb +2 -2
  82. data/lib/protocols/buftok.rb +1 -1
  83. data/lib/protocols/header_and_content.rb +1 -1
  84. data/lib/protocols/httpcli2.rb +16 -6
  85. data/lib/protocols/httpclient.rb +7 -1
  86. data/lib/protocols/line_and_text.rb +6 -2
  87. data/lib/protocols/linetext2.rb +12 -14
  88. data/lib/protocols/postgres.rb +1 -1
  89. data/lib/protocols/saslauth.rb +1 -1
  90. data/lib/protocols/smtpclient.rb +1 -1
  91. data/lib/protocols/smtpserver.rb +36 -23
  92. data/lib/protocols/stomp.rb +24 -1
  93. data/lib/protocols/tcptest.rb +1 -1
  94. data/tasks/cpp.rake +77 -0
  95. data/tasks/project.rake +78 -0
  96. data/tasks/tests.rake +192 -0
  97. data/tests/test_attach.rb +1 -1
  98. data/tests/test_basic.rb +65 -12
  99. data/tests/test_defer.rb +13 -29
  100. data/tests/test_epoll.rb +16 -21
  101. data/tests/test_errors.rb +1 -1
  102. data/tests/test_eventables.rb +4 -5
  103. data/tests/test_exc.rb +1 -1
  104. data/tests/test_futures.rb +1 -1
  105. data/tests/test_hc.rb +185 -236
  106. data/tests/test_httpclient.rb +1 -1
  107. data/tests/test_httpclient2.rb +28 -6
  108. data/tests/test_kb.rb +2 -2
  109. data/tests/test_ltp.rb +139 -141
  110. data/tests/test_ltp2.rb +1 -1
  111. data/tests/test_next_tick.rb +1 -1
  112. data/tests/test_processes.rb +1 -1
  113. data/tests/test_pure.rb +3 -2
  114. data/tests/test_running.rb +1 -1
  115. data/tests/test_sasl.rb +1 -1
  116. data/tests/test_send_file.rb +56 -51
  117. data/tests/test_servers.rb +26 -36
  118. data/tests/test_smtpclient.rb +46 -44
  119. data/tests/test_smtpserver.rb +1 -1
  120. data/tests/test_spawn.rb +1 -1
  121. data/tests/test_ssl_args.rb +68 -0
  122. data/tests/test_timers.rb +15 -9
  123. data/tests/test_ud.rb +1 -1
  124. data/tests/testem.rb +30 -4
  125. metadata +69 -34
@@ -0,0 +1,74 @@
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 com.rubyeventmachine.*;
34
+ import java.net.*;
35
+ import org.junit.After;
36
+ import org.junit.AfterClass;
37
+ import org.junit.Before;
38
+ import org.junit.BeforeClass;
39
+ import org.junit.Test;
40
+ import org.junit.Assert;
41
+
42
+ public class TestServers {
43
+
44
+ @BeforeClass
45
+ public static void setUpBeforeClass() throws Exception {
46
+ }
47
+
48
+ @AfterClass
49
+ public static void tearDownAfterClass() throws Exception {
50
+ }
51
+
52
+ @Before
53
+ public void setUp() throws Exception {
54
+ }
55
+
56
+ @After
57
+ public void tearDown() throws Exception {
58
+ }
59
+
60
+
61
+ @Test
62
+ public void testBadServerAddress() {
63
+ final Application a = new Application();
64
+ a.run (new Runnable() {
65
+ public void run() {
66
+ try {
67
+ a.startServer(new InetSocketAddress ("100.100.100.100", 100), new DefaultConnectionFactory());
68
+ Assert.fail ("was supposed to throw a reactor exception");
69
+ } catch (EmReactorException e) {}
70
+ a.stop();
71
+ }
72
+ });
73
+ }
74
+ }
@@ -0,0 +1,89 @@
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 com.rubyeventmachine.*;
33
+ import java.io.*;
34
+
35
+ import org.junit.Assert;
36
+ import org.junit.After;
37
+ import org.junit.AfterClass;
38
+ import org.junit.Before;
39
+ import org.junit.BeforeClass;
40
+ import org.junit.Test;
41
+
42
+
43
+ public class TestTimers {
44
+
45
+ @BeforeClass
46
+ public static void setUpBeforeClass() throws Exception {
47
+ }
48
+
49
+ @AfterClass
50
+ public static void tearDownAfterClass() throws Exception {
51
+ }
52
+
53
+ @Before
54
+ public void setUp() throws Exception {
55
+ }
56
+
57
+ @After
58
+ public void tearDown() throws Exception {
59
+ }
60
+
61
+
62
+
63
+ @Test
64
+ public final void test2() throws IOException {
65
+ Application a = new Application();
66
+ a.addTimer(0, new Timer() {
67
+ public void fire() {
68
+ application.stop();
69
+ }
70
+ });
71
+ a.run();
72
+ Assert.assertEquals (1, 1); // just to make sure the reactor halts.
73
+ }
74
+
75
+ @Test
76
+ public final void test3() throws IOException {
77
+ Application a = new Application();
78
+ a.addTimer (0.1, new PeriodicTimer() {
79
+ int n = 0;
80
+ public void fire() {
81
+ n++;
82
+ if (n == 5)
83
+ application.stop();
84
+ }
85
+ });
86
+ a.run();
87
+ Assert.assertEquals(1, 1);
88
+ }
89
+ }
@@ -1,4 +1,4 @@
1
- # $Id: deferrable.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: eventable.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: future.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: messages.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: processes.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: spawnable.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: streamer.rb 668 2008-01-04 23:00:34Z blackhedd $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -1,4 +1,4 @@
1
- # $Id: eventmachine.rb 785 2008-09-15 09:46:23Z francis $
1
+ # $Id$
2
2
  #
3
3
  # Author:: Francis Cianfrocca (gmail: blackhedd)
4
4
  # Homepage:: http://rubyeventmachine.com
@@ -41,36 +41,37 @@
41
41
  # string be set in order to select the pure-Ruby version.
42
42
  #
43
43
 
44
- =begin
45
- $eventmachine_library ||= nil
44
+
45
+ unless defined?($eventmachine_library)
46
+ $eventmachine_library = ENV['EVENTMACHINE_LIBRARY'] || :cascade
47
+ end
48
+ $eventmachine_library = $eventmachine_library.to_sym
49
+
46
50
  case $eventmachine_library
47
51
  when :pure_ruby
48
52
  require 'pr_eventmachine'
49
53
  when :extension
50
54
  require 'rubyeventmachine'
51
- else
55
+ when :java
56
+ require 'jeventmachine'
57
+ else # :cascade
52
58
  # This is the case that most user code will take.
53
59
  # Prefer the extension if available.
54
60
  begin
55
- require 'rubyeventmachine'
61
+ if RUBY_PLATFORM =~ /java/
62
+ require 'java'
63
+ require 'jeventmachine'
64
+ $eventmachine_library = :java
65
+ else
66
+ require 'rubyeventmachine'
67
+ $eventmachine_library = :extension
68
+ end
56
69
  rescue LoadError
70
+ warn "# EventMachine fell back to pure ruby mode" if $DEBUG
57
71
  require 'pr_eventmachine'
72
+ $eventmachine_library = :pure_ruby
58
73
  end
59
74
  end
60
- =end
61
-
62
-
63
- if RUBY_PLATFORM =~ /java/
64
- require 'java'
65
- require 'jeventmachine'
66
- else
67
- if $eventmachine_library == :pure_ruby or ENV['EVENTMACHINE_LIBRARY'] == "pure_ruby"
68
- require 'pr_eventmachine'
69
- else
70
- require 'rubyeventmachine'
71
- end
72
- end
73
-
74
75
 
75
76
  require "eventmachine_version"
76
77
  require 'em/deferrable'
@@ -171,6 +172,11 @@ require 'shellwords'
171
172
  #
172
173
  #
173
174
  module EventMachine
175
+ class FileNotFoundException < Exception; end
176
+
177
+ class << self
178
+ attr_reader :threadpool
179
+ end
174
180
 
175
181
 
176
182
  # EventMachine::run initializes and runs an event loop.
@@ -230,7 +236,17 @@ module EventMachine
230
236
  (b = blk || block) and add_timer(0, b)
231
237
  run_machine
232
238
  ensure
233
- release_machine
239
+ begin
240
+ release_machine
241
+ ensure
242
+ if @threadpool
243
+ @threadpool.each { |t| t.exit }
244
+ @threadpool.each { |t| t.kill! if t.alive? }
245
+ @threadqueue = nil
246
+ @resultqueue = nil
247
+ end
248
+ @threadpool = nil
249
+ end
234
250
  @reactor_running = false
235
251
  end
236
252
 
@@ -368,7 +384,6 @@ module EventMachine
368
384
  def EventMachine::cancel_timer signature
369
385
  @timers[signature] = proc{} if @timers.has_key?(signature)
370
386
  end
371
- private_class_method :cancel_timer
372
387
 
373
388
 
374
389
  # stop_event_loop may called from within a callback method
@@ -515,7 +530,16 @@ module EventMachine
515
530
  # }
516
531
  #
517
532
  #
518
- def EventMachine::start_server server, port, handler=nil, *args, &block
533
+ def EventMachine::start_server server, port=nil, handler=nil, *args, &block
534
+
535
+ begin
536
+ port = Integer(port)
537
+ rescue ArgumentError, TypeError
538
+ args.unshift handler if handler
539
+ handler = port
540
+ port = nil
541
+ end if port
542
+
519
543
  klass = if (handler and handler.is_a?(Class))
520
544
  handler
521
545
  else
@@ -528,7 +552,11 @@ module EventMachine
528
552
  raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
529
553
  end
530
554
 
531
- s = start_tcp_server server, port
555
+ s = if port
556
+ start_tcp_server server, port
557
+ else
558
+ start_unix_server server
559
+ end
532
560
  @acceptors[s] = [klass,args,block]
533
561
  s
534
562
  end
@@ -543,22 +571,8 @@ module EventMachine
543
571
  EventMachine::stop_tcp_server signature
544
572
  end
545
573
 
546
- def EventMachine::start_unix_domain_server filename, handler=nil, *args, &block
547
- klass = if (handler and handler.is_a?(Class))
548
- handler
549
- else
550
- Class.new( Connection ) {handler and include handler}
551
- end
552
-
553
- arity = klass.instance_method(:initialize).arity
554
- expected = arity >= 0 ? arity : -(arity + 1)
555
- if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
556
- raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
557
- end
558
-
559
- s = start_unix_server filename
560
- @acceptors[s] = [klass,args,block]
561
- s
574
+ def EventMachine::start_unix_domain_server filename, *args, &block
575
+ start_server filename, *args, &block
562
576
  end
563
577
 
564
578
  # EventMachine#connect initiates a TCP connection to a remote
@@ -658,7 +672,15 @@ module EventMachine
658
672
  # to have them behave differently with respect to post_init
659
673
  # if at all possible.
660
674
  #
661
- def EventMachine::connect server, port, handler=nil, *args
675
+ def EventMachine::connect server, port=nil, handler=nil, *args
676
+ begin
677
+ port = Integer(port)
678
+ rescue ArgumentError, TypeError
679
+ args.unshift handler if handler
680
+ handler = port
681
+ port = nil
682
+ end if port
683
+
662
684
  klass = if (handler and handler.is_a?(Class))
663
685
  handler
664
686
  else
@@ -671,7 +693,12 @@ module EventMachine
671
693
  raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
672
694
  end
673
695
 
674
- s = connect_server server, port
696
+ s = if port
697
+ connect_server server, port
698
+ else
699
+ connect_unix_server server
700
+ end
701
+
675
702
  c = klass.new s, *args
676
703
  @conns[s] = c
677
704
  block_given? and yield c
@@ -783,24 +810,8 @@ module EventMachine
783
810
  # For making connections to Unix-domain sockets.
784
811
  # Eventually this has to get properly documented and unified with the TCP-connect methods.
785
812
  # Note how nearly identical this is to EventMachine#connect
786
- def EventMachine::connect_unix_domain socketname, handler=nil, *args
787
- klass = if (handler and handler.is_a?(Class))
788
- handler
789
- else
790
- Class.new( Connection ) {handler and include handler}
791
- end
792
-
793
- arity = klass.instance_method(:initialize).arity
794
- expected = arity >= 0 ? arity : -(arity + 1)
795
- if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
796
- raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
797
- end
798
-
799
- s = connect_unix_server socketname
800
- c = klass.new s, *args
801
- @conns[s] = c
802
- block_given? and yield c
803
- c
813
+ def EventMachine::connect_unix_domain socketname, *args, &blk
814
+ connect socketname, *args, &blk
804
815
  end
805
816
 
806
817
 
@@ -993,27 +1004,30 @@ module EventMachine
993
1004
  # way of initializing @threadqueue because EventMachine is a Module, not a Class, and
994
1005
  # has no constructor.
995
1006
  #
996
- def self::defer op, callback = nil
997
- @need_threadqueue ||= 0
998
- if @need_threadqueue == 0
999
- @need_threadqueue = 1
1007
+ def self::defer op = nil, callback = nil, &blk
1008
+ unless @threadpool
1000
1009
  require 'thread'
1010
+ @threadpool = []
1001
1011
  @threadqueue = Queue.new
1002
1012
  @resultqueue = Queue.new
1003
- 20.times {|ix|
1004
- Thread.new {
1005
- my_ix = ix
1006
- loop {
1007
- op,cback = @threadqueue.pop
1008
- result = op.call
1009
- @resultqueue << [result, cback]
1010
- EventMachine.signal_loopbreak
1011
- }
1012
- }
1013
- }
1013
+ spawn_threadpool
1014
1014
  end
1015
1015
 
1016
- @threadqueue << [op,callback]
1016
+ @threadqueue << [op||blk,callback]
1017
+ end
1018
+
1019
+ def self.spawn_threadpool
1020
+ until @threadpool.size == 20
1021
+ thread = Thread.new do
1022
+ while true
1023
+ op, cback = *@threadqueue.pop
1024
+ result = op.call
1025
+ @resultqueue << [result, cback]
1026
+ EventMachine.signal_loopbreak
1027
+ end
1028
+ end
1029
+ @threadpool << thread
1030
+ end
1017
1031
  end
1018
1032
 
1019
1033
 
@@ -1090,7 +1104,7 @@ module EventMachine
1090
1104
  #--
1091
1105
  # Perhaps misnamed since the underlying function uses socketpair and is full-duplex.
1092
1106
  #
1093
- def self::popen cmd, handler=nil
1107
+ def self::popen cmd, handler=nil, *args
1094
1108
  klass = if (handler and handler.is_a?(Class))
1095
1109
  handler
1096
1110
  else
@@ -1100,7 +1114,7 @@ module EventMachine
1100
1114
  w = Shellwords::shellwords( cmd )
1101
1115
  w.unshift( w.first ) if w.first
1102
1116
  s = invoke_popen( w )
1103
- c = klass.new s
1117
+ c = klass.new s, *args
1104
1118
  @conns[s] = c
1105
1119
  yield(c) if block_given?
1106
1120
  c
@@ -1164,7 +1178,7 @@ module EventMachine
1164
1178
  # code, but the performance impact may be too large.
1165
1179
  #
1166
1180
  if opcode == ConnectionData
1167
- c = @conns[conn_binding] or raise ConnectionNotBound
1181
+ c = @conns[conn_binding] or raise ConnectionNotBound, "received data #{data} for unknown signature: #{conn_binding}"
1168
1182
  c.receive_data data
1169
1183
  elsif opcode == ConnectionUnbound
1170
1184
  if c = @conns.delete( conn_binding )
@@ -1177,7 +1191,7 @@ module EventMachine
1177
1191
  elsif c = @acceptors.delete( conn_binding )
1178
1192
  # no-op
1179
1193
  else
1180
- raise ConnectionNotBound
1194
+ raise ConnectionNotBound, "recieved ConnectionUnbound for an unknown signature: #{conn_binding}"
1181
1195
  end
1182
1196
  elsif opcode == ConnectionAccepted
1183
1197
  accep,args,blk = @acceptors[conn_binding]
@@ -1187,10 +1201,10 @@ module EventMachine
1187
1201
  blk and blk.call(c)
1188
1202
  c # (needed?)
1189
1203
  elsif opcode == TimerFired
1190
- t = @timers.delete( data ) or raise UnknownTimerFired
1204
+ t = @timers.delete( data ) or raise UnknownTimerFired, "timer data: #{data}"
1191
1205
  t.call
1192
1206
  elsif opcode == ConnectionCompleted
1193
- c = @conns[conn_binding] or raise ConnectionNotBound
1207
+ c = @conns[conn_binding] or raise ConnectionNotBound, "received ConnectionCompleted for unknown signature: #{conn_binding}"
1194
1208
  c.connection_completed
1195
1209
  elsif opcode == LoopbreakSignalled
1196
1210
  run_deferred_callbacks
@@ -1348,10 +1362,10 @@ class Connection
1348
1362
  # Override .new so subclasses don't have to call super and can ignore
1349
1363
  # connection-specific arguments
1350
1364
  #
1351
- def self.new sig, *args #:nodoc:
1365
+ def self.new(sig, *args) #:nodoc:
1352
1366
  allocate.instance_eval do
1353
1367
  # Call a superclass's #initialize if it has one
1354
- initialize *args
1368
+ initialize(*args)
1355
1369
 
1356
1370
  # Store signature and run #post_init
1357
1371
  @signature = sig
@@ -1367,6 +1381,10 @@ class Connection
1367
1381
  def initialize(*args) #:nodoc:
1368
1382
  end
1369
1383
 
1384
+ def associate_callback_target(sig) #:nodoc:
1385
+ # no-op for the time being, to match similar no-op in rubymain.cpp
1386
+ end
1387
+
1370
1388
  # EventMachine::Connection#post_init is called by the event loop
1371
1389
  # immediately after the network connection has been established,
1372
1390
  # and before resumption of the network loop.
@@ -1485,7 +1503,9 @@ class Connection
1485
1503
  # connection objects. (Need an example to make that clear.)
1486
1504
  #
1487
1505
  def send_data data
1488
- EventMachine::send_data @signature, data, data.length
1506
+ size = data.bytesize if data.respond_to?(:bytesize)
1507
+ size ||= data.size
1508
+ EventMachine::send_data @signature, data, size
1489
1509
  end
1490
1510
 
1491
1511
  # Returns true if the connection is in an error state, false otherwise.
@@ -1539,11 +1559,16 @@ class Connection
1539
1559
  # behaved than the ones for raw chunks of memory.
1540
1560
  #
1541
1561
  def start_tls args={}
1542
- EventMachine::set_tls_parms(
1543
- @signature,
1544
- args[:private_key_file] || "",
1545
- args[:cert_chain_file] || ""
1546
- )
1562
+ priv_key, cert_chain = args.values_at(:private_key_file, :cert_chain_file)
1563
+
1564
+ [priv_key, cert_chain].each do |file|
1565
+ next if file.nil? or file.empty?
1566
+ raise FileNotFoundException,
1567
+ "Could not find #{file} for start_tls" unless File.exists? file
1568
+ end
1569
+
1570
+ EventMachine::set_tls_parms(@signature, priv_key || '', cert_chain || '')
1571
+
1547
1572
  EventMachine::start_tls @signature
1548
1573
  end
1549
1574
 
@@ -1685,8 +1710,10 @@ class Connection
1685
1710
  EventMachine::add_timer @interval, proc {self.fire}
1686
1711
  end
1687
1712
  def fire
1688
- @code.call
1689
- schedule unless @cancelled
1713
+ unless @cancelled
1714
+ @code.call
1715
+ schedule
1716
+ end
1690
1717
  end
1691
1718
  def cancel
1692
1719
  @cancelled = true
@@ -1705,42 +1732,37 @@ class Connection
1705
1732
  end
1706
1733
  end
1707
1734
 
1708
-
1709
-
1710
-
1711
1735
  end
1712
1736
 
1737
+ # Is inside of protocols/ but not in the namespace?
1738
+ require 'protocols/buftok'
1739
+
1713
1740
  module Protocols
1714
1741
  # In this module, we define standard protocol implementations.
1715
1742
  # They get included from separate source files.
1743
+
1744
+ # TODO / XXX: We're munging the LOAD_PATH!
1745
+ # A good citizen would use eventmachine/protocols/tcptest.
1746
+ # TODO : various autotools are completely useless with the lack of naming
1747
+ # convention, we need to correct that!
1748
+ autoload :TcpConnectTester, 'protocols/tcptest'
1749
+ autoload :HttpClient, 'protocols/httpclient'
1750
+ autoload :LineAndTextProtocol, 'protocols/line_and_text'
1751
+ autoload :HeaderAndContentProtocol, 'protocols/header_and_content'
1752
+ autoload :LineText2, 'protocols/linetext2'
1753
+ autoload :HttpClient2, 'protocols/httpcli2'
1754
+ autoload :Stomp, 'protocols/stomp'
1755
+ autoload :SmtpClient, 'protocols/smtpclient'
1756
+ autoload :SmtpServer, 'protocols/smtpserver'
1757
+ autoload :SASLauth, 'protocols/saslauth'
1758
+
1759
+ #require 'protocols/postgres' UNCOMMENT THIS LINE WHEN THE POSTGRES CODE IS READY FOR PRIME TIME.
1716
1760
  end
1717
1761
 
1718
1762
  end # module EventMachine
1719
1763
 
1720
-
1721
-
1722
1764
  # Save everyone some typing.
1723
1765
  EM = EventMachine
1724
1766
  EM::P = EventMachine::Protocols
1725
1767
 
1726
-
1727
- # At the bottom of this module, we load up protocol handlers that depend on some
1728
- # of the classes defined here. Eventually we should refactor this out so it's
1729
- # laid out in a more logical way.
1730
- #
1731
-
1732
- require 'protocols/tcptest'
1733
- require 'protocols/httpclient'
1734
- require 'protocols/line_and_text'
1735
- require 'protocols/linetext2'
1736
- require 'protocols/header_and_content'
1737
- require 'protocols/httpcli2'
1738
- require 'protocols/stomp'
1739
- require 'protocols/smtpclient'
1740
- require 'protocols/smtpserver'
1741
- require 'protocols/saslauth'
1742
- #require 'protocols/postgres' UNCOMMENT THIS LINE WHEN THE POSTGRES CODE IS READY FOR PRIME TIME.
1743
-
1744
1768
  require 'em/processes'
1745
-
1746
-