1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Minimal usage example of libevent
16 module libevent_example
is example
20 # Factory creating instances of `EchoConnection` to handle new connections
22 super ConnectionFactory
24 redef fun spawn_connection
(buf
, address
)
26 return new EchoConnection(buf
)
30 # Connection echoing data received from clients back at them
34 redef fun read_callback
(content
)
36 print
"Received: {content}"
41 # Skip the actual execution when testing
42 if "NIT_TESTING".environ
== "true" then exit
0
44 # Setup libevent system
45 var event_base
= new NativeEventBase
46 var factory
= new MyFactory(event_base
)
48 # Open a TCP socket for listening
49 factory
.bind_tcp
("localhost", 8888)
51 # Open a UNIX domain socket for listening
52 factory
.bind_unix
("/tmp/my.sck")