RubyUNIXServer.java

  1. /***** BEGIN LICENSE BLOCK *****
  2.  * Version: EPL 1.0/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Eclipse Public
  5.  * License Version 1.0 (the "License"); you may not use this file
  6.  * except in compliance with the License. You may obtain a copy of
  7.  * the License at http://www.eclipse.org/legal/epl-v10.html
  8.  *
  9.  * Software distributed under the License is distributed on an "AS
  10.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11.  * implied. See the License for the specific language governing
  12.  * rights and limitations under the License.
  13.  *
  14.  * Copyright (C) 2008 Ola Bini <ola.bini@gmail.com>
  15.  *
  16.  * Alternatively, the contents of this file may be used under the terms of
  17.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  18.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  19.  * in which case the provisions of the GPL or the LGPL are applicable instead
  20.  * of those above. If you wish to allow use of your version of this file only
  21.  * under the terms of either the GPL or the LGPL, and not to allow others to
  22.  * use your version of this file under the terms of the EPL, indicate your
  23.  * decision by deleting the provisions above and replace them with the notice
  24.  * and other provisions required by the GPL or the LGPL. If you do not delete
  25.  * the provisions above, a recipient may use your version of this file under
  26.  * the terms of any one of the EPL, the GPL or the LGPL.
  27.  ***** END LICENSE BLOCK *****/
  28. package org.jruby.ext.socket;


  29. import jnr.unixsocket.UnixServerSocketChannel;
  30. import jnr.unixsocket.UnixSocketChannel;
  31. import org.jruby.Ruby;
  32. import org.jruby.RubyClass;
  33. import org.jruby.anno.JRubyClass;
  34. import org.jruby.anno.JRubyMethod;
  35. import org.jruby.runtime.Helpers;
  36. import org.jruby.runtime.ObjectAllocator;
  37. import org.jruby.runtime.ThreadContext;
  38. import org.jruby.runtime.Visibility;
  39. import org.jruby.runtime.builtin.IRubyObject;

  40. import java.io.IOException;
  41. import java.nio.channels.SelectableChannel;
  42. import java.nio.channels.SelectionKey;

  43. @JRubyClass(name="UNIXServer", parent="UNIXSocket")
  44. public class RubyUNIXServer extends RubyUNIXSocket {
  45.     private static ObjectAllocator UNIXSERVER_ALLOCATOR = new ObjectAllocator() {
  46.         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
  47.             return new RubyUNIXServer(runtime, klass);
  48.         }
  49.     };

  50.     static void createUNIXServer(Ruby runtime) {
  51.         RubyClass rb_cUNIXServer = runtime.defineClass("UNIXServer", runtime.getClass("UNIXSocket"), UNIXSERVER_ALLOCATOR);

  52.         runtime.getObject().setConstant("UNIXserver", rb_cUNIXServer);
  53.        
  54.         rb_cUNIXServer.defineAnnotatedMethods(RubyUNIXServer.class);
  55.     }

  56.     public RubyUNIXServer(Ruby runtime, RubyClass type) {
  57.         super(runtime, type);
  58.     }

  59.     @JRubyMethod(visibility = Visibility.PRIVATE)
  60.     public IRubyObject initialize(ThreadContext context, IRubyObject path) {
  61.         init_unixsock(context.runtime, path, true);

  62.         return this;
  63.     }

  64.     @JRubyMethod
  65.     public IRubyObject accept(ThreadContext context) {
  66.         Ruby runtime = context.runtime;

  67.         try {

  68.             while (true) { // select loop to allow interrupting
  69.                 boolean ready = context.getThread().select(this, SelectionKey.OP_ACCEPT);

  70.                 if (!ready) {
  71.                     // we were woken up without being selected...poll for thread events and go back to sleep
  72.                     context.pollThreadEvents();

  73.                 } else {
  74.                     UnixSocketChannel socketChannel = asUnixServer().accept();

  75.                     RubyUNIXSocket sock = (RubyUNIXSocket)(Helpers.invoke(context, runtime.getClass("UNIXSocket"), "allocate"));

  76.                     sock.init_sock(context.runtime, socketChannel, "");

  77.                     return sock;
  78.                 }
  79.             }

  80.         } catch (IOException ioe) {
  81.             throw context.runtime.newIOErrorFromException(ioe);
  82.         }
  83.     }

  84.     @JRubyMethod
  85.     public IRubyObject accept_nonblock(ThreadContext context) {
  86.         Ruby runtime = context.runtime;

  87.         SelectableChannel selectable = (SelectableChannel)getChannel();

  88.         synchronized (selectable.blockingLock()) {
  89.             boolean oldBlocking = selectable.isBlocking();

  90.             try {
  91.                 selectable.configureBlocking(false);

  92.                 try {
  93.                     UnixSocketChannel socketChannel = ((UnixServerSocketChannel) selectable).accept();

  94.                     RubyUNIXSocket sock = (RubyUNIXSocket)(Helpers.invoke(context, runtime.getClass("UNIXSocket"), "allocate"));

  95.                     sock.init_sock(context.runtime, socketChannel, "");

  96.                     return sock;

  97.                 } finally {
  98.                     selectable.configureBlocking(oldBlocking);
  99.                 }

  100.             } catch (IOException ioe) {
  101.                 if (ioe.getMessage().equals("accept failed: Resource temporarily unavailable")) {
  102.                     throw runtime.newErrnoEAGAINReadableError("accept");
  103.                 }

  104.                 throw context.runtime.newIOErrorFromException(ioe);
  105.             }
  106.         }
  107.     }

  108.     @JRubyMethod
  109.     public IRubyObject listen(ThreadContext context, IRubyObject log) {
  110.         // TODO listen backlog
  111.         return context.runtime.newFixnum(0);
  112.     }

  113.     @JRubyMethod
  114.     public IRubyObject sysaccept(ThreadContext context) {
  115.         return accept(context);
  116.     }

  117.     @JRubyMethod
  118.     public IRubyObject path(ThreadContext context) {
  119.         return context.runtime.newString(openFile.getPath());
  120.     }

  121.     @JRubyMethod
  122.     public IRubyObject addr(ThreadContext context) {
  123.         Ruby runtime = context.runtime;

  124.         return runtime.newArray(
  125.                 runtime.newString("AF_UNIX"),
  126.                 runtime.newString(openFile.getPath()));
  127.     }

  128.     @JRubyMethod
  129.     public IRubyObject peeraddr(ThreadContext context) {
  130.         throw context.runtime.newErrnoENOTCONNError();
  131.     }

  132.     private UnixServerSocketChannel asUnixServer() {
  133.         return (UnixServerSocketChannel)getChannel();
  134.     }
  135. }// RubyUNIXServer