tests: fix model_visitor tests since mdoc fix
[nit.git] / lib / libevent.nit
index e5858e3..36de40f 100644 (file)
 module libevent is pkgconfig("libevent")
 
 in "C header" `{
-       #include <sys/stat.h>
-       #include <sys/types.h>
-       #include <fcntl.h>
-       #include <errno.h>
-       #include <sys/socket.h>
-
        #include <event2/listener.h>
        #include <event2/bufferevent.h>
        #include <event2/buffer.h>
 `}
 
 in "C" `{
+       #include <sys/stat.h>
+       #include <sys/types.h>
+       #include <fcntl.h>
+       #include <errno.h>
+       #include <string.h>
+
+       #include <sys/socket.h>
+       #include <arpa/inet.h>
+       #include <netinet/in.h>
+       #include <netinet/ip.h>
+
+// Protect callbacks for compatibility with light FFI
+#ifdef Connection_decr_ref
        // Callback forwarded to 'Connection.write_callback'
        static void c_write_cb(struct bufferevent *bev, Connection ctx) {
                Connection_write_callback(ctx);
@@ -42,45 +49,24 @@ in "C" `{
        // Callback forwarded to 'Connection.read_callback_native'
        static void c_read_cb(struct bufferevent *bev, Connection ctx)
        {
-               // TODO move to Nit code
-               struct evbuffer *input = bufferevent_get_input(bev);
-               size_t len = evbuffer_get_length(input);
-               char* cstr = malloc(len);
-               evbuffer_remove(input, cstr, len);
-               Connection_read_callback_native(ctx, cstr, len);
+               Connection_read_callback_native(ctx, bev);
        }
 
        // Callback forwarded to 'Connection.event_callback'
        static void c_event_cb(struct bufferevent *bev, short events, Connection ctx)
        {
-               Connection_event_callback(ctx, events);
-
-               // TODO move to Nit code
-               if (events & BEV_EVENT_ERROR)
-                       perror("Error from bufferevent");
-               if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
-                       bufferevent_free(bev);
-                       Connection_decr_ref(ctx);
-               }
+               int release = Connection_event_callback(ctx, events);
+               if (release) Connection_decr_ref(ctx);
        }
 
-       // Callback fowarded to 'ConnectionFactory.spawn_connection'
-       static void accept_conn_cb(struct evconnlistener *listener, evutil_socket_t fd,
-               struct sockaddr *address, int socklen, ConnectionFactory ctx)
+       // Callback forwarded to 'ConnectionFactory.accept_connection'
+       static void accept_connection_cb(struct evconnlistener *listener, evutil_socket_t fd,
+               struct sockaddr *addrin, int socklen, ConnectionFactory ctx)
        {
-               // TODO move to Nit code
-               struct event_base *base = evconnlistener_get_base(listener);
-               struct bufferevent *bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
-
-               Connection nit_con = ConnectionFactory_spawn_connection(ctx, bev);
-               Connection_incr_ref(nit_con);
-
-               bufferevent_setcb(bev,
-                       (bufferevent_data_cb)c_read_cb,
-                       (bufferevent_data_cb)c_write_cb,
-                       (bufferevent_event_cb)c_event_cb, nit_con);
-               bufferevent_enable(bev, EV_READ|EV_WRITE);
+               ConnectionFactory_accept_connection(ctx, listener, fd, addrin, socklen);
        }
+#endif
+
 `}
 
 # Structure to hold information and state for a Libevent dispatch loop.
@@ -141,9 +127,14 @@ class Connection
                if close_requested then close
        end
 
-       private fun read_callback_native(cstr: NativeString, len: Int)
+       private fun read_callback_native(bev: NativeBufferEvent)
        do
-               read_callback(cstr.to_s_with_length(len))
+               var evbuffer = bev.input_buffer
+               var len = evbuffer.length
+               var buf = new NativeString(len)
+               evbuffer.remove(buf, len)
+               var str = buf.to_s_with_length(len)
+               read_callback str
        end
 
        # Callback method when data is available to read
@@ -152,8 +143,19 @@ class Connection
                if close_requested then close
        end
 
-       # Callback method on events
-       fun event_callback(events: Int) do end
+       # Callback method on events: EOF, user-defined timeout and unrecoverable errors
+       #
+       # Returns `true` if the native handles to `self` can be released.
+       fun event_callback(events: Int): Bool
+       do
+               if events & bev_event_error != 0 or events & bev_event_eof != 0 then
+                       if events & bev_event_error != 0 then print_error "Error from bufferevent"
+                       close
+                       return true
+               end
+
+               return false
+       end
 
        # Write a string to the connection
        redef fun write(str)
@@ -353,13 +355,17 @@ extern class ConnectionListener `{ struct evconnlistener * `}
 
                struct hostent *hostent = gethostbyname(address);
 
+               if (!hostent) {
+                       return NULL;
+               }
+
                memset(&sin, 0, sizeof(sin));
                sin.sin_family = hostent->h_addrtype;
                sin.sin_port = htons(port);
                memcpy( &(sin.sin_addr.s_addr), (const void*)hostent->h_addr, hostent->h_length );
 
                listener = evconnlistener_new_bind(base,
-                       (evconnlistener_cb)accept_conn_cb, factory,
+                       (evconnlistener_cb)accept_connection_cb, factory,
                        LEV_OPT_CLOSE_ON_FREE | LEV_OPT_REUSEABLE, -1,
                        (struct sockaddr*)&sin, sizeof(sin));
 
@@ -392,10 +398,31 @@ class ConnectionFactory
        # The `NativeEventBase` for the dispatch loop of this factory
        var event_base: NativeEventBase
 
-       # On new connection, create the handler `Connection` object
-       fun spawn_connection(nat_buf_ev: NativeBufferEvent): Connection
+       # Accept a connection on `listener`
+       #
+       # By default, it creates a new NativeBufferEvent and calls `spawn_connection`.
+       fun accept_connection(listener: ConnectionListener, fd: Int, addrin: Pointer, socklen: Int)
        do
-               return new Connection(nat_buf_ev)
+               var base = listener.base
+               var bev = new NativeBufferEvent.socket(base, fd, bev_opt_close_on_free)
+
+               # Human representation of remote client address
+               var addr_len = 46 # Longest possible IPv6 address + null byte
+               var addr_buf = new NativeString(addr_len)
+               addr_buf = addrin_to_address(addrin, addr_buf, addr_len)
+               var addr = if addr_buf.address_is_null then
+                               "Unknown address"
+                       else addr_buf.to_s
+
+               var conn = spawn_connection(bev, addr)
+               bev.enable ev_read|ev_write
+               bev.setcb conn
+       end
+
+       # Create a new `Connection` object for `buffer_event`
+       fun spawn_connection(buffer_event: NativeBufferEvent, address: String): Connection
+       do
+               return new Connection(buffer_event)
        end
 
        # Listen on `address`:`port` for new connection, which will callback `spawn_connection`
@@ -407,4 +434,19 @@ class ConnectionFactory
                end
                return listener
        end
+
+       # Put string representation of source `address` into `buf`
+       private fun addrin_to_address(address: Pointer, buf: NativeString, buf_len: Int): NativeString `{
+               struct sockaddr *addrin = (struct sockaddr*)address;
+
+               if (addrin->sa_family == AF_INET) {
+                       struct in_addr *src = &((struct sockaddr_in*)addrin)->sin_addr;
+                       return (char *)inet_ntop(addrin->sa_family, src, buf, buf_len);
+               }
+               else if (addrin->sa_family == AF_INET6) {
+                       struct in6_addr *src = &((struct sockaddr_in6*)addrin)->sin6_addr;
+                       return (char *)inet_ntop(addrin->sa_family, src, buf, buf_len);
+               }
+               return NULL;
+       `}
 end