| 176 | | # EventMachine::run initializes and runs an event loop. |
|---|
| 177 | | # This method only returns if user-callback code calls stop_event_loop. |
|---|
| 178 | | # Use the supplied block to define your clients and servers. |
|---|
| 179 | | # The block is called by EventMachine::run immediately after initializing |
|---|
| 180 | | # its internal event loop but <i>before</i> running the loop. |
|---|
| 181 | | # Therefore this block is the right place to call start_server if you |
|---|
| 182 | | # want to accept connections from remote clients. |
|---|
| 183 | | # |
|---|
| 184 | | # For programs that are structured as servers, it's usually appropriate |
|---|
| 185 | | # to start an event loop by calling EventMachine::run, and let it |
|---|
| 186 | | # run forever. It's also possible to use EventMachine::run to make a single |
|---|
| 187 | | # client-connection to a remote server, process the data flow from that |
|---|
| 188 | | # single connection, and then call stop_event_loop to force EventMachine::run |
|---|
| 189 | | # to return. Your program will then continue from the point immediately |
|---|
| 190 | | # following the call to EventMachine::run. |
|---|
| 191 | | # |
|---|
| 192 | | # You can of course do both client and servers simultaneously in the same program. |
|---|
| 193 | | # One of the strengths of the event-driven programming model is that the |
|---|
| 194 | | # handling of network events on many different connections will be interleaved, |
|---|
| 195 | | # and scheduled according to the actual events themselves. This maximizes |
|---|
| 196 | | # efficiency. |
|---|
| 197 | | # |
|---|
| 198 | | # === Server usage example |
|---|
| 199 | | # |
|---|
| 200 | | # See the text at the top of this file for an example of an echo server. |
|---|
| 201 | | # |
|---|
| 202 | | # === Client usage example |
|---|
| 203 | | # |
|---|
| 204 | | # See the description of stop_event_loop for an extremely simple client example. |
|---|
| 205 | | # |
|---|
| 206 | | #-- |
|---|
| 207 | | # Obsoleted the use_threads mechanism. |
|---|
| 208 | | # 25Nov06: Added the begin/ensure block. We need to be sure that release_machine |
|---|
| 209 | | # gets called even if an exception gets thrown within any of the user code |
|---|
| 210 | | # that the event loop runs. The best way to see this is to run a unit |
|---|
| 211 | | # test with two functions, each of which calls EventMachine#run and each of |
|---|
| 212 | | # which throws something inside of #run. Without the ensure, the second test |
|---|
| 213 | | # will start without release_machine being called and will immediately throw |
|---|
| 214 | | # a C++ runtime error. |
|---|
| 215 | | # |
|---|
| 216 | | def EventMachine::run &block |
|---|
| 217 | | @conns = {} |
|---|
| 218 | | @acceptors = {} |
|---|
| 219 | | @timers = {} |
|---|
| 220 | | begin |
|---|
| 221 | | @reactor_running = true |
|---|
| 222 | | initialize_event_machine |
|---|
| 223 | | block and add_timer 0, block |
|---|
| 224 | | run_machine |
|---|
| 225 | | rescue |
|---|
| 226 | | STDERR.puts "#{$!}" |
|---|
| 227 | | ensure |
|---|
| 228 | | release_machine |
|---|
| 229 | | @reactor_running = false |
|---|
| 230 | | end |
|---|
| 231 | | end |
|---|
| | 176 | # EventMachine::run initializes and runs an event loop. |
|---|
| | 177 | # This method only returns if user-callback code calls stop_event_loop. |
|---|
| | 178 | # Use the supplied block to define your clients and servers. |
|---|
| | 179 | # The block is called by EventMachine::run immediately after initializing |
|---|
| | 180 | # its internal event loop but <i>before</i> running the loop. |
|---|
| | 181 | # Therefore this block is the right place to call start_server if you |
|---|
| | 182 | # want to accept connections from remote clients. |
|---|
| | 183 | # |
|---|
| | 184 | # For programs that are structured as servers, it's usually appropriate |
|---|
| | 185 | # to start an event loop by calling EventMachine::run, and let it |
|---|
| | 186 | # run forever. It's also possible to use EventMachine::run to make a single |
|---|
| | 187 | # client-connection to a remote server, process the data flow from that |
|---|
| | 188 | # single connection, and then call stop_event_loop to force EventMachine::run |
|---|
| | 189 | # to return. Your program will then continue from the point immediately |
|---|
| | 190 | # following the call to EventMachine::run. |
|---|
| | 191 | # |
|---|
| | 192 | # You can of course do both client and servers simultaneously in the same program. |
|---|
| | 193 | # One of the strengths of the event-driven programming model is that the |
|---|
| | 194 | # handling of network events on many different connections will be interleaved, |
|---|
| | 195 | # and scheduled according to the actual events themselves. This maximizes |
|---|
| | 196 | # efficiency. |
|---|
| | 197 | # |
|---|
| | 198 | # === Server usage example |
|---|
| | 199 | # |
|---|
| | 200 | # See the text at the top of this file for an example of an echo server. |
|---|
| | 201 | # |
|---|
| | 202 | # === Client usage example |
|---|
| | 203 | # |
|---|
| | 204 | # See the description of stop_event_loop for an extremely simple client example. |
|---|
| | 205 | # |
|---|
| | 206 | #-- |
|---|
| | 207 | # Obsoleted the use_threads mechanism. |
|---|
| | 208 | # 25Nov06: Added the begin/ensure block. We need to be sure that release_machine |
|---|
| | 209 | # gets called even if an exception gets thrown within any of the user code |
|---|
| | 210 | # that the event loop runs. The best way to see this is to run a unit |
|---|
| | 211 | # test with two functions, each of which calls EventMachine#run and each of |
|---|
| | 212 | # which throws something inside of #run. Without the ensure, the second test |
|---|
| | 213 | # will start without release_machine being called and will immediately throw |
|---|
| | 214 | # a C++ runtime error. |
|---|
| | 215 | # |
|---|
| | 216 | # Provisionally added a brain-dead rescue block to signal errors raised by user-written |
|---|
| | 217 | # code called in event handlers. Otherwise very confusing errors result. |
|---|
| | 218 | # |
|---|
| | 219 | def EventMachine::run &block |
|---|
| | 220 | @conns = {} |
|---|
| | 221 | @acceptors = {} |
|---|
| | 222 | @timers = {} |
|---|
| | 223 | begin |
|---|
| | 224 | @reactor_running = true |
|---|
| | 225 | initialize_event_machine |
|---|
| | 226 | block and add_timer 0, block |
|---|
| | 227 | run_machine |
|---|
| | 228 | rescue |
|---|
| | 229 | STDERR.puts "#{$!}" |
|---|
| | 230 | ensure |
|---|
| | 231 | release_machine |
|---|
| | 232 | @reactor_running = false |
|---|
| | 233 | end |
|---|
| | 234 | end |
|---|