Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2007 Ola Bini <ola@ologix.com> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.socket;
 
 import org.jruby.Ruby;
 

Author(s):
Ola Bini
 
 @JRubyClass(name="Socket", parent="BasicSocket", include="Socket::Constants")
 public class RubyServerSocket extends RubySocket {
     static void createServerSocket(Ruby runtime) {
         RubyClass rb_cSocket = runtime.defineClass("ServerSocket"runtime.getClass("Socket"), );
 
         rb_cSocket.defineAnnotatedMethods(RubyServerSocket.class);
     }
 
     private static ObjectAllocator SERVER_SOCKET_ALLOCATOR = new ObjectAllocator() {
         public IRubyObject allocate(Ruby runtimeRubyClass klass) {
             return new RubyServerSocket(runtimeklass);
         }
     };
 
     public RubyServerSocket(Ruby runtimeRubyClass type) {
         super(runtimetype);
     }
 
     @JRubyMethod(name = "listen")
     public IRubyObject listen(ThreadContext contextIRubyObject backlog) {
         context.runtime.getWarnings().warnOnce(
                 ..,
                 "pass backlog to #bind instead of #listen (http://wiki.jruby.org/ServerSocket)");
 
         return context.runtime.newFixnum(0);
     }
 
     @JRubyMethod(notImplemented = true)
     public IRubyObject connect_nonblock(ThreadContext contextIRubyObject arg) {
         throw SocketUtils.sockerr(context.runtime"server socket cannot connect");
     }
 
     @JRubyMethod(notImplemented = true)
     public IRubyObject connect(ThreadContext contextIRubyObject arg) {
         throw SocketUtils.sockerr(context.runtime"server socket cannot connect");
     }
 
     @JRubyMethod()
     public IRubyObject bind(ThreadContext contextIRubyObject addr) {
         InetSocketAddress iaddr = Sockaddr.addressFromSockaddr_in(contextaddr);
 
        doBind(contextgetChannel(), iaddr, 0);
        return RubyFixnum.zero(context.runtime);
    }
    @JRubyMethod()
    public IRubyObject bind(ThreadContext contextIRubyObject addrIRubyObject backlog) {
        InetSocketAddress iaddr = Sockaddr.addressFromSockaddr_in(contextaddr);
        doBind(contextgetChannel(), iaddr, RubyFixnum.fix2int(backlog));
        return RubyFixnum.zero(context.runtime);
    }
    @JRubyMethod()
    public IRubyObject accept(ThreadContext context) {
        return doAccept(contextgetChannel());
    }
    @JRubyMethod()
    public IRubyObject accept_nonblock(ThreadContext context) {
        return doAcceptNonblock(contextgetChannel());
    }
    protected ChannelDescriptor initChannel(Ruby runtime) {
        Channel channel;
        try {
            if( == .) {
                channel = ServerSocketChannel.open();
            } else {
                throw runtime.newArgumentError("unsupported server socket type `" +  + "'");
            }
            ModeFlags modeFlags = newModeFlags(runtime.);
            return new ChannelDescriptor(channelmodeFlags);
        } catch(IOException e) {
            throw SocketUtils.sockerr(runtime"initialize: " + e.toString());
        }
    }
    private RubyArray doAcceptNonblock(ThreadContext contextChannel channel) {
        try {
            if (channel instanceof SelectableChannel) {
                SelectableChannel selectable = (SelectableChannel)channel;
                synchronized (selectable.blockingLock()) {
                    boolean oldBlocking = selectable.isBlocking();
                    try {
                        selectable.configureBlocking(false);
                        RubySocket socket = doAccept(contextchannel);
                        SocketChannel socketChannel = (SocketChannel)socket.getChannel();
                        InetSocketAddress addr = (InetSocketAddress)socketChannel.socket().getLocalSocketAddress();
                        return context.runtime.newArray(
                                socket,
                                Sockaddr.packSockaddrFromAddress(contextaddr));
                    } finally {
                        selectable.configureBlocking(oldBlocking);
                    }
                }
            } else {
                throw getRuntime().newErrnoENOPROTOOPTError();
            }
        } catch(IOException e) {
            throw SocketUtils.sockerr(context.runtimee.getLocalizedMessage());
        }
    }
    private RubySocket doAccept(ThreadContext contextChannel channel) {
        Ruby runtime = context.runtime;
        try {
            if (channel instanceof ServerSocketChannel) {
                ServerSocketChannel serverChannel = (ServerSocketChannel)getChannel();
                SocketChannel socket = serverChannel.accept();
                if (socket == null) {
                    // This appears to be undocumented in JDK; null as a sentinel value
                    // for a nonblocking accept with nothing available. We raise for Ruby.
                    // indicates that no connection is available in non-blocking mode
                    throw runtime.newErrnoEAGAINReadableError("accept(2) would block");
                }
                RubySocket rubySocket = new RubySocket(runtimeruntime.getClass("Socket"));
                rubySocket.initFromServer(runtimethissocket);
                return rubySocket;
            } else {
                throw runtime.newErrnoENOPROTOOPTError();
            }
        } catch (IllegalBlockingModeException ibme) {
            // indicates that no connection is available in non-blocking mode
            throw runtime.newErrnoEAGAINReadableError("accept(2) would block");
        } catch(IOException e) {
            throw SocketUtils.sockerr(runtimee.getLocalizedMessage());
        }
    }
    private void doBind(ThreadContext contextChannel channelInetSocketAddress iaddrint backlog) {
        Ruby runtime = context.runtime;
        try {
            if (channel instanceof ServerSocketChannel) {
                ServerSocket socket = ((ServerSocketChannel)channel).socket();
                socket.bind(iaddrbacklog);
            } else {
                throw runtime.newErrnoENOPROTOOPTError();
            }
        } catch(UnknownHostException e) {
            throw SocketUtils.sockerr(runtime"bind(2): unknown host");
        } catch(SocketException e) {
            handleSocketException(runtime"bind"e);
        } catch(IOException e) {
            throw SocketUtils.sockerr(runtime"bind(2): name or service not known");
        } catch (IllegalArgumentException iae) {
            throw SocketUtils.sockerr(runtimeiae.getMessage());
        }
    }
}// RubySocket
New to GrepCode? Check out our FAQ X