eventmachine 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/DEFERRABLES ADDED
@@ -0,0 +1,138 @@
1
+ $Id: DEFERRABLES 541 2007-09-17 15:08:36Z blackhedd $
2
+
3
+ [DOCUMENT UNDER CONSTRUCTION]
4
+
5
+ EventMachine (EM) adds two different formalisms for lightweight concurrency to the Ruby programmer's toolbox: spawned processes and deferrables. This note will show you how to use spawned processes. For more information, see the separate document LIGHTWEIGHT_CONCURRENCY.
6
+
7
+
8
+ === What are Deferrables?
9
+
10
+ EventMachine's Deferrable borrows heavily from the "deferred" object in Python's "Twisted" event-handling framework. Here's a minimal example that illustrates Deferrable:
11
+
12
+ require 'eventmachine'
13
+
14
+ class MyClass
15
+ include EM::Deferrable
16
+
17
+ def print_value x
18
+ puts "MyClass instance received #{x}"
19
+ end
20
+ end
21
+
22
+ EM.run {
23
+ df = MyClass.new
24
+ df.callback {|x|
25
+ df.print_value(x)
26
+ EM.stop
27
+ }
28
+
29
+ EM::Timer.new(2) {
30
+ df.set_deferred_status :succeeded, 100
31
+ }
32
+ }
33
+
34
+
35
+ This program will spin for two seconds, print out the string "MyClass instance received 100" and then exit. The Deferrable pattern relies on an unusual metaphor that may be unfamiliar to you, unless you've used Python's Twisted. You may need to read the following material through more than once before you get the idea.
36
+
37
+ EventMachine::Deferrable is simply a Ruby Module that you can include in your own classes. (There also is a class named EventMachine::DefaultDeferrable for when you want to create one without including it in code of your own.)
38
+
39
+ An object that includes EventMachine::Deferrable is like any other Ruby object: it can be created whenever you want, returned from your functions, or passed as an argument to other functions.
40
+
41
+ The Deferrable pattern allows you to specify any number of Ruby code blocks (callbacks or errbacks) that will be executed at some future time when the status of the Deferrable object changes.
42
+
43
+ How might that be useful? Well, imagine that you're implementing an HTTP server, but you need to make a call to some other server in order to fulfill a client request.
44
+
45
+ When you receive a request from one of your clients, you can create and return a Deferrable object. Some other section of your program can add a callback to the Deferrable that will cause the client's request to be fulfilled. Simultaneously, you initiate an event-driven or threaded client request to some different server. And then your EM program will continue to process other events and service other client requests.
46
+
47
+ When your client request to the other server completes some time later, you will call the #set_deferred_status method on the Deferrable object, passing either a success or failure status, and an arbitrary number of parameters (which might include the data you received from the other server).
48
+
49
+ At that point, the status of the Deferrable object becomes known, and its callback or errback methods are immediately executed. Callbacks and errbacks are code blocks that are attached to Deferrable objects at any time through the methods #callback and #errback.
50
+
51
+ The deep beauty of this pattern is that it decouples the disposition of one operation (such as a client request to an outboard server) from the subsequent operations that depend on that disposition (which may include responding to a different client or any other operation).
52
+
53
+ The code which invokes the deferred operation (that will eventually result in a success or failure status together with associated data) is completely separate from the code which depends on that status and data. This achieves one of the primary goals for which threading is typically used in sophisticated applications, with none of the nondeterminacy or debugging difficulties of threads.
54
+
55
+ As soon as the deferred status of a Deferrable becomes known by way of a call to #set_deferred_status, the Deferrable will IMMEDIATELY execute all of its callbacks or errbacks in the order in which they were added to the Deferrable.
56
+
57
+ Callbacks and errbacks can be added to a Deferrable object at any time, not just when the object is created. They can even be added after the status of the object has been determined! (In this case, they will be executed immediately when they are added.)
58
+
59
+ A call to Deferrable#set_deferred_status takes :succeeded or :failed as its first argument. (This determines whether the object will call its callbacks or its errbacks.) #set_deferred_status also takes zero or more additional parameters, that will in turn be passed as parameters to the callbacks or errbacks.
60
+
61
+ In general, you can only call #set_deferred_status ONCE on a Deferrable object. A call to #set_deferred_status will not return until all of the associated callbacks or errbacks have been called. If you add callbacks or errbacks AFTER making a call to #set_deferred_status, those additional callbacks or errbacks will execute IMMEDIATELY. Any given callback or errback will be executed AT MOST once.
62
+
63
+ It's possible to call #set_deferred_status AGAIN, during the execution a callback or errback. This makes it possible to change the parameters which will be sent to the callbacks or errbacks farther down the chain, enabling some extremely elegant use-cases. You can transform the data returned from a deferred operation in arbitrary ways as needed by subsequent users, without changing any of the code that generated the original data.
64
+
65
+ A call to #set_deferred_status will not return until all of the associated callbacks or errbacks have been called. If you add callbacks or errbacks AFTER making a call to #set_deferred_status, those additional callbacks or errbacks will execute IMMEDIATELY.
66
+
67
+ Let's look at some more sample code. It turns out that many of the internal protocol implementations in the EventMachine package rely on Deferrable. One of these is EM::Protocols::HttpClient.
68
+
69
+ To make an evented HTTP request, use the module function EM::Protocols::HttpClient#request, which returns a Deferrable object. Here's how:
70
+
71
+ require 'eventmachine'
72
+
73
+ EM.run {
74
+ df = EM::Protocols::HttpClient.request( :host=>"www.example.com", :request=>"/index.html" )
75
+
76
+ df.callback {|response|
77
+ puts "Succeeded: #{response[:content]}"
78
+ EM.stop
79
+ }
80
+
81
+ df.errback {|response|
82
+ puts "ERROR: #{response[:status]}"
83
+ EM.stop
84
+ }
85
+ }
86
+
87
+ (See the documentation of EventMachine::Protocols::HttpClient for information on the object returned by #request.)
88
+
89
+ In this code, we make a call to HttpClient#request, which immediately returns a Deferrable object. In the background, an HTTP client request is being made to www.example.com, although your code will continue to run concurrently.
90
+
91
+ At some future point, the HTTP client request will complete, and the code in EM::Protocols::HttpClient will process either a valid HTTP response (including returned content), or an error.
92
+
93
+ At that point, EM::Protocols::HttpClient will call EM::Deferrable#set_deferred_status on the Deferrable object that was returned to your program, as the return value from EM::Protocols::HttpClient.request. You don't have to do anything to make this happen. All you have to do is tell the Deferrable what to do in case of either success, failure, or both.
94
+
95
+ In our code sample, we set one callback and one errback. The former will be called if the HTTP call succeeds, and the latter if it fails. (For simplicity, we have both of them calling EM#stop to end the program, although real programs would be very unlikely to do this.)
96
+
97
+ Setting callbacks and errbacks is optional. They are handlers to defined events in the lifecycle of the Deferrable event. It's not an error if you fail to set either a callback, an errback, or both. But of course your program will then fail to receive those notifications.
98
+
99
+ If through some bug it turns out that #set_deferred_status is never called on a Deferrable object, then that object's callbacks or errbacks will NEVER be called. It's also possible to set a timeout on a Deferrable. If the timeout elapses before any other call to #set_deferred_status, the Deferrable object will behave as is you had called set_deferred_status(:failed) on it.
100
+
101
+
102
+ Now let's modify the example to illustrate some additional points:
103
+
104
+ require 'eventmachine'
105
+
106
+ EM.run {
107
+ df = EM::Protocols::HttpClient.request( :host=>"www.example.com", :request=>"/index.html" )
108
+
109
+ df.callback {|response|
110
+ df.set_deferred_status :succeeded, response[:content]
111
+ }
112
+
113
+ df.callback {|string|
114
+ puts "Succeeded: #{string}"
115
+ EM.stop
116
+ }
117
+
118
+ df.errback {|response|
119
+ puts "ERROR: #{response[:status]}"
120
+ EM.stop
121
+ }
122
+ }
123
+
124
+
125
+ Just for the sake of illustration, we've now set two callbacks instead of one. If the deferrable operation (the HTTP client-request) succeeds, then both of the callbacks will be executed in order.
126
+
127
+ But notice that we've also made our own call to #set_deferred_status in the first callback. This isn't required, because the HttpClient implementation already made a call to #set_deferred_status. (Otherwise, of course, the callback would not be executing.)
128
+
129
+ But we used #set_deferred_status in the first callback in order to change the parameters that will be sent to subsequent callbacks in the chain. In this way, you can construct powerful sequences of layered functionality. If you want, you can even change the status of the Deferrable from :succeeded to :failed, which would abort the chain of callback calls, and invoke the chain of errbacks instead.
130
+
131
+ Now of course it's somewhat trivial to define two callbacks in the same method, even with the parameter-changing effect we just described. It would be much more interesting to pass the Deferrable to some other function (for example, a function defined in another module or a different gem), that would in turn add callbacks and/or errbacks of its own. That would illustrate the true power of the Deferrable pattern: to isolate the HTTP client-request from other functions that use the data that it returns without caring where those data came from.
132
+
133
+ Remember that you can add a callback or an errback to a Deferrable at any point in time, regardless of whether the status of the deferred operation is known (more precisely, regardless of when #set_deferred_status is called on the object). Even hours or days later.
134
+
135
+ When you add a callback or errback to a Deferrable object on which #set_deferred_status has not yet been called, the callback/errback is queued up for future execution, inside the Deferrable object. When you add a callback or errback to a Deferrable on which #set_deferred_status has already been called, the callback/errback will be executed immediately. Your code doesn't have to worry about the ordering, and there are no timing issues, as you would expect with a threaded approach.
136
+
137
+ For more information on Deferrables and their typical usage patterns, look in the EM unit tests. There are also quite a few sugarings (including EM::Deferrable#future) that make typical Deferrable usages syntactically easier to work with.
138
+
data/EPOLL CHANGED
@@ -40,7 +40,6 @@ want your process to drop the superuser privileges after you increase your proce
40
40
 
41
41
  Call the method EventMachine#epoll anytime before you call EventMachine#run, and your program will
42
42
  automatically use epoll, if available. It's safe to call EventMachine#epoll on any platform because
43
-
44
43
  it compiles to a no-op on platforms that don't support epoll.
45
44
 
46
45
  require 'rubygems'
data/KEYBOARD ADDED
@@ -0,0 +1,38 @@
1
+ EventMachine (EM) can respond to keyboard events. This gives your event-driven programs the ability to respond to input from local users.
2
+
3
+ Programming EM to handle keyboard input in Ruby is simplicity itself. Just use EventMachine#open_keyboard, and supply the name of a Ruby module or class that will receive the input:
4
+
5
+ require 'rubygems'
6
+ require 'eventmachine'
7
+
8
+ module MyKeyboardHandler
9
+ def receive_data keystrokes
10
+ puts "I received the following data from the keyboard: #{keystrokes}"
11
+ end
12
+ end
13
+
14
+ EM.run {
15
+ EM.open_keyboard(MyKeyboardHandler)
16
+ }
17
+
18
+
19
+ If you want EM to send line-buffered keyboard input to your program, just include the LineText2 protocol module in your handler class or module:
20
+
21
+
22
+
23
+ require 'rubygems'
24
+ require 'eventmachine'
25
+
26
+ module MyKeyboardHandler
27
+ include EM::Protocols::LineText2
28
+ def receive_line data
29
+ puts "I received the following line from the keyboard: #{data}"
30
+ end
31
+ end
32
+
33
+ EM.run {
34
+ EM.open_keyboard(MyKeyboardHandler)
35
+ }
36
+
37
+ As we said, simplicity itself. You can call EventMachine#open_keyboard at any time while the EM reactor loop is running. In other words, the method invocation may appear anywhere in an EventMachine#run block, or in any code invoked in the #run block.
38
+
@@ -0,0 +1,72 @@
1
+ $Id: LIGHTWEIGHT_CONCURRENCY 541 2007-09-17 15:08:36Z blackhedd $
2
+
3
+ EventMachine (EM) adds two different formalisms for lightweight concurrency to the Ruby programmer's toolbox: spawned processes and deferrables. This note will show you how to use them.
4
+
5
+
6
+ === What is Lightweight Concurrency?
7
+
8
+ We use the term "Lightweight Concurrency" (LC) to refer to concurrency mechanisms that are lighter than Ruby threads. By "lighter," we mean: less resource-intensive in one or more dimensions, usually including memory and CPU usage. In general, you turn to LC in the hope of improving the performance and scalability of your programs.
9
+
10
+ In addition to the two EventMachine mechanisms we will discuss here, Ruby has at least one other LC construct: Fibers, which are currently under development in Ruby 1.9.
11
+
12
+ The technical feature that makes all of these LC mechanisms different from standard Ruby threads is that they are not scheduled automatically.
13
+
14
+ When you create and run Ruby threads, you can assume (within certain constraints) that your threads will all be scheduled fairly by Ruby's runtime. Ruby itself is responsible for giving each of your threads its own share of the total runtime.
15
+
16
+ But with LC, your program is responsible for causing different execution paths to run. In effect, your program has to act as a "thread scheduler." Scheduled entities in LC run to completion and are never preempted. The runtime system has far less work to do since it has no need to interrupt threads or to schedule them fairly. This is what makes LC lighter and faster.
17
+
18
+ You'll learn exactly how LC scheduling works in practice as we work through specific examples.
19
+
20
+
21
+ === EventMachine Lightweight Concurrency
22
+
23
+ Recall that EM provides a reactor loop that must be running in order for your programs to perform event-driven logic. An EM program typically has a structure like this:
24
+
25
+ require 'eventmachine'
26
+
27
+ # your initializations
28
+
29
+ EM.run {
30
+ # perform event-driven I/O here, including network clients,
31
+ # servers, timers, and thread-pool operations.
32
+ }
33
+
34
+ # your cleanup
35
+ # end of the program
36
+
37
+
38
+ EventMachine#run executes the reactor loop, which causes your code to be called as events of interest to your program occur. The block you pass to EventMachine#run is executed right after the reactor loop starts, and is the right place to start socket acceptors, etc.
39
+
40
+ Because the reactor loop runs constantly in an EM program (until it is stopped by a call to EventMachine#stop), it has the ability to schedule blocks of code for asynchronous execution. Unlike a pre-emptive thread scheduler, it's NOT able to interrupt code blocks while they execute. But the scheduling capability it does have is enough to enable lightweight concurrency.
41
+
42
+
43
+ For information on Spawned Processes, see the separate document SPAWNED_PROCESSES.
44
+
45
+ For information on Deferrables, see the separate document DEFERRABLES.
46
+
47
+
48
+ === [SIDEBAR]: I Heard That EventMachine Doesn't Work With Ruby Threads.
49
+
50
+ This is incorrect. EM is fully interoperable with all versions of Ruby threads, and has been since its earliest releases.
51
+
52
+ It's very true that EM encourages an "evented" (non-threaded) programming style. The specific benefits of event-driven programming are far better performance and scalabiity for well-written programs, and far easier debugging.
53
+
54
+ The benefit of using threads for similar applications is a possibly more intuitive programming model, as well as the fact that threads are already familiar to most programmers. Also, bugs in threaded programs often fail to show up until programs go into production. These factors create the illusion that threaded programs are easier to write.
55
+
56
+ However, some operations that occur frequently in professional-caliber applications simply can't be done without threads. (The classic example is making calls to database client-libraries that block on network I/O until they complete.)
57
+
58
+ EventMachine not only allows the use of Ruby threads in these cases, but it even provides a built-in thread-pool object to make them easier to work with.
59
+
60
+ You may have heard a persistent criticism that evented I/O is fundamentally incompatible with Ruby threads. It is true that some well-publicized attempts to incorporate event-handling libraries into Ruby were not successful. But EventMachine was designed from the ground up with Ruby compatibility in mind, so EM never suffered from the problems that defeated the earlier attempts.
61
+
62
+
63
+ === [SIDEBAR]: I Heard That EventMachine Doesn't Work Very Well On Windows.
64
+
65
+ This too is incorrect. EventMachine is an extension written in C++ and Java, and therefore it requires compilation. Many Windows computers (and some Unix computers, especially in production environments) don't have a build stack. Attempting to install EventMachine on a machine without a compiler usually produces a confusing error.
66
+
67
+ In addition, Ruby has a much-debated issue with Windows compiler versions. Ruby on Windows works best with Visual Studio 6, a compiler version that is long out-of-print, no longer supported by Microsoft, and difficult to obtain. (This problem is not specific to EventMachine.)
68
+
69
+ Shortly after EventMachine was first released, the compiler issues led to criticism that EM was incompatible with Windows. Since that time, every EventMachine release has been supplied in a precompiled binary form for Windows users, that does not require you to compile the code yourself. EM binary Gems for Windows are compiled using Visual Studio 6.
70
+
71
+ EventMachine does supply some advanced features (such as Linux EPOLL support, reduced-privilege operation, UNIX-domain sockets, etc.) that have no meaningful implementation on Windows. Apart from these special cases, all EM functionality (including lightweight concurrency) works perfectly well on Windows.
72
+
data/SMTP ADDED
@@ -0,0 +1,9 @@
1
+ $Id: SMTP 539 2007-09-17 14:42:52Z blackhedd $
2
+
3
+
4
+ [DOCUMENT UNDER CONSTRUCTION]
5
+
6
+ This note details the usage of EventMachine's built-in support for SMTP. EM supports both client and server connections, which will be described in separate sections.
7
+
8
+
9
+
data/SPAWNED_PROCESSES ADDED
@@ -0,0 +1,93 @@
1
+ $Id: SPAWNED_PROCESSES 538 2007-09-17 14:38:56Z blackhedd $
2
+
3
+ [DOCUMENT UNDER CONSTRUCTION]
4
+
5
+ EventMachine (EM) adds two different formalisms for lightweight concurrency to the Ruby programmer's toolbox: spawned processes and deferrables. This note will show you how to use spawned processes. For more information, see the separate document LIGHTWEIGHT_CONCURRENCY.
6
+
7
+
8
+ === What are Spawned Processes?
9
+
10
+ Spawned Processes in EventMachine are inspired directly by the "processes" found in the Erlang programming language. EM deliberately borrows much (but not all) of Erlang's terminology. However, EM's spawned processes differ from Erlang's in ways that reflect not only Ruby style, but also the fact that Ruby is not a functional language like Erlang.
11
+
12
+ Let's proceed with a complete, working code sample that we will analyze line by line. Here's an EM implementation of the "ping-pong" program that also appears in the Erlang tutorial:
13
+
14
+
15
+ require 'eventmachine'
16
+
17
+ EM.run {
18
+ pong = EM.spawn {|x, ping|
19
+ puts "Pong received #{x}"
20
+ ping.notify( x-1 )
21
+ }
22
+
23
+ ping = EM.spawn {|x|
24
+ if x > 0
25
+ puts "Pinging #{x}"
26
+ pong.notify x, self
27
+ else
28
+ EM.stop
29
+ end
30
+ }
31
+
32
+ ping.notify 3
33
+ }
34
+
35
+ If you run this program, you'll see the following output:
36
+
37
+ Pinging 3
38
+ Pong received 3
39
+ Pinging 2
40
+ Pong received 2
41
+ Pinging 1
42
+ Pong received 1
43
+
44
+ Let's take it step by step.
45
+
46
+ EventMachine#spawn works very much like the built-in function spawn in Erlang. It returns a reference to a Ruby object of class EventMachine::SpawnedProcess, which is actually a schedulable entity. In Erlang, the value returned from spawn is called a "process identifier" or "pid." But we'll refer to the Ruby object returned from EM#spawn simply as a "spawned process."
47
+
48
+ You pass a Ruby block with zero or more parameters to EventMachine#spawn. Like all Ruby blocks, this one is a closure, so it can refer to variables defined in the local context when you call EM#spawn.
49
+
50
+ However, the code block passed to EM#spawn does NOT execute immediately by default. Rather, it will execute only when the Spawned Object is "notified." In Erlang, this process is called "message passing," and is done with the operator !, but in Ruby it's done simply by calling the #notify method of a spawned-process object. The parameters you pass to #notify must match those defined in the block that was originally passed to EM#spawn.
51
+
52
+ When you call the #notify method of a spawned-process object, EM's reactor core will execute the code block originally passed to EM#spawn, at some point in the future. (#notify itself merely adds a notification to the object's message queue and ALWAYS returns immediately.)
53
+
54
+ When a SpawnedProcess object executes a notification, it does so in the context of the SpawnedProcess object itself. The notified code block can see local context from the point at which EM#spawn was called. However, the value of "self" inside the notified code block is a reference to the SpawnedProcesss object itself.
55
+
56
+ An EM spawned process is nothing more than a Ruby object with a message queue attached to it. You can have any number of spawned processes in your program without compromising scalability. You can notify a spawned process any number of times, and each notification will cause a "message" to be placed in the queue of the spawned process. Spawned processes with non-empty message queues are scheduled for execution automatically by the EM reactor. Spawned processes with no visible references are garbage-collected like any other Ruby object.
57
+
58
+ Back to our code sample:
59
+
60
+ pong = EM.spawn {|x, ping|
61
+ puts "Pong received #{x}"
62
+ ping.notify( x-1 )
63
+ }
64
+
65
+ This simply creates a spawned process and assigns it to the local variable pong. You can see that the spawned code block takes a numeric parameter and a reference to another spawned process. When pong is notified, it expects to receive arguments corresponding to these two parameters. It simply prints out the number it receives as the first argument. Then it notifies the spawned process referenced by the second argument, passing it the first argument minus 1.
66
+
67
+ And then the block ends, which is crucial because otherwise nothing else can run. (Remember that in LC, scheduled entities run to completion and are never preempted.)
68
+
69
+ On to the next bit of the code sample:
70
+
71
+ ping = EM.spawn {|x|
72
+ if x > 0
73
+ puts "Pinging #{x}"
74
+ pong.notify x, self
75
+ else
76
+ EM.stop
77
+ end
78
+ }
79
+
80
+ Here, we're spawning a process that takes a single (numeric) parameter. If the parameter is greater than zero, the block writes it to the console. It then notifies the spawned process referenced by the pong local variable, passing as arguments its number argument, and a reference to itself. The latter reference, as you saw above, is used by pong to send a return notification.
81
+
82
+ If the ping process receives a zero value, it will stop the reactor loop and end the program.
83
+
84
+ Now we've created a pair of spawned processes, but nothing else has happened. If we stop now, the program will spin in the EM reactor loop, doing nothing at all. Our spawned processes will never be scheduled for execution.
85
+
86
+ But look at the next line in the code sample:
87
+
88
+ ping.notify 3
89
+
90
+ This line gets the ping-pong ball rolling. We call ping's #notify method, passing the argument 3. This causes a message to be sent to the ping spawned process. The message contains the single argument, and it causes the EM reactor to schedule the ping process. And this in turn results in the execution of the Ruby code block passed to EM#spawn when ping was created. Everything else proceeds as a result of the messages that are subsequently passed to each other by the spawned processes.
91
+
92
+ [TODO, present the outbound network i/o use case, and clarify that spawned processes are interleaved with normal i/o operations and don't interfere with them at all. Also, blame Erlang for the confusing term "process"]
93
+
data/ext/cmain.cpp CHANGED
@@ -1,6 +1,6 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: cmain.cpp 476 2007-07-27 03:32:51Z blackhedd $
3
+ $Id: cmain.cpp 502 2007-08-24 09:29:53Z blackhedd $
4
4
 
5
5
  File: cmain.cpp
6
6
  Date: 06Apr06
@@ -134,6 +134,17 @@ extern "C" const char *evma_open_datagram_socket (const char *address, int port)
134
134
  return EventMachine->OpenDatagramSocket (address, port);
135
135
  }
136
136
 
137
+ /******************
138
+ evma_open_keyboard
139
+ ******************/
140
+
141
+ extern "C" const char *evma_open_keyboard()
142
+ {
143
+ if (!EventMachine)
144
+ throw std::runtime_error ("not initialized");
145
+ return EventMachine->OpenKeyboard();
146
+ }
147
+
137
148
 
138
149
 
139
150
  /****************************
@@ -207,6 +218,19 @@ extern "C" void evma_start_tls (const char *binding)
207
218
  ed->StartTls();
208
219
  }
209
220
 
221
+ /******************
222
+ evma_set_tls_parms
223
+ ******************/
224
+
225
+ extern "C" void evma_set_tls_parms (const char *binding, const char *privatekey_filename, const char *certchain_filename)
226
+ {
227
+ if (!EventMachine)
228
+ throw std::runtime_error ("not initialized");
229
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
230
+ if (ed)
231
+ ed->SetTlsParms (privatekey_filename, certchain_filename);
232
+ }
233
+
210
234
 
211
235
  /*****************
212
236
  evma_get_peername
data/ext/cplusplus.cpp CHANGED
@@ -1,6 +1,6 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: cplusplus.cpp 474 2007-07-27 03:12:30Z blackhedd $
3
+ $Id: cplusplus.cpp 505 2007-08-26 20:44:57Z blackhedd $
4
4
 
5
5
  File: cplusplus.cpp
6
6
  Date: 27Jul07
@@ -45,10 +45,10 @@ void EM::Run (void (*start_func)())
45
45
  EM::AddTimer
46
46
  ************/
47
47
 
48
- void EM::AddTimer (int seconds, void (*func)())
48
+ void EM::AddTimer (int milliseconds, void (*func)())
49
49
  {
50
50
  if (func) {
51
- const char *sig = evma_install_oneshot_timer (seconds);
51
+ const char *sig = evma_install_oneshot_timer (milliseconds);
52
52
  Timers.insert (make_pair (sig, func));
53
53
  }
54
54
  }
@@ -140,9 +140,6 @@ void EM::Callback (const char *sig, int ev, const char *data, int length)
140
140
 
141
141
  switch (ev) {
142
142
  case EM_TIMER_FIRED:
143
- //(new A())->Start ("192.168.0.8", 9700);
144
- //cerr << evma_create_tcp_server ("192.168.0.8", 9700) << " SERVER\n";
145
- //(new M())->Connect ("www.bayshorenetworks.com", 80);
146
143
  f = Timers [data];
147
144
  if (f)
148
145
  (*f)();
data/ext/ed.cpp CHANGED
@@ -1,6 +1,6 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: ed.cpp 447 2007-07-20 16:42:06Z blackhedd $
3
+ $Id: ed.cpp 497 2007-08-15 13:57:49Z blackhedd $
4
4
 
5
5
  File: ed.cpp
6
6
  Date: 06Apr06
@@ -623,7 +623,7 @@ void ConnectionDescriptor::StartTls()
623
623
  if (SslBox)
624
624
  throw std::runtime_error ("SSL/TLS already running on connection");
625
625
 
626
- SslBox = new SslBox_t (bIsServer);
626
+ SslBox = new SslBox_t (bIsServer, PrivateKeyFilename, CertChainFilename);
627
627
  _DispatchCiphertext();
628
628
  #endif
629
629
 
@@ -633,6 +633,27 @@ void ConnectionDescriptor::StartTls()
633
633
  }
634
634
 
635
635
 
636
+ /*********************************
637
+ ConnectionDescriptor::SetTlsParms
638
+ *********************************/
639
+
640
+ void ConnectionDescriptor::SetTlsParms (const char *privkey_filename, const char *certchain_filename)
641
+ {
642
+ #ifdef WITH_SSL
643
+ if (SslBox)
644
+ throw std::runtime_error ("call SetTlsParms before calling StartTls");
645
+ if (privkey_filename && *privkey_filename)
646
+ PrivateKeyFilename = privkey_filename;
647
+ if (certchain_filename && *certchain_filename)
648
+ CertChainFilename = certchain_filename;
649
+ #endif
650
+
651
+ #ifdef WITHOUT_SSL
652
+ throw std::runtime_error ("Encryption not available on this event-machine");
653
+ #endif
654
+ }
655
+
656
+
636
657
 
637
658
  /*****************************************
638
659
  ConnectionDescriptor::_DispatchCiphertext