KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > ext > socket > RubyBasicSocket


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common 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/cpl-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) 2007 Ola Bini <ola@ologix.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 CPL, 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 CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.ext.socket;
29
30 import java.io.EOFException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.nio.channels.Channel JavaDoc;
33 import java.nio.channels.SocketChannel JavaDoc;
34 import java.nio.channels.ServerSocketChannel JavaDoc;
35 import java.net.SocketAddress JavaDoc;
36 import java.net.InetSocketAddress JavaDoc;
37 import org.jruby.Ruby;
38 import org.jruby.RubyClass;
39 import org.jruby.RubyNumeric;
40 import org.jruby.RubyIO;
41 import org.jruby.RubyString;
42 import org.jruby.runtime.CallbackFactory;
43 import org.jruby.runtime.ObjectAllocator;
44 import org.jruby.runtime.builtin.IRubyObject;
45 import org.jruby.util.IOHandlerNio;
46 import org.jruby.util.IOHandler;
47
48 /**
49  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
50  */

51 public class RubyBasicSocket extends RubyIO {
52     private static ObjectAllocator BASICSOCKET_ALLOCATOR = new ObjectAllocator() {
53         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
54             return new RubyBasicSocket(runtime, klass);
55         }
56     };
57
58     static void createBasicSocket(Ruby runtime) {
59         RubyClass rb_cBasicSocket = runtime.defineClass("BasicSocket", runtime.getClass("IO"), BASICSOCKET_ALLOCATOR);
60
61         CallbackFactory cfact = runtime.callbackFactory(RubyBasicSocket.class);
62         rb_cBasicSocket.defineFastMethod("send", cfact.getFastOptMethod("write_send"));
63         rb_cBasicSocket.defineFastMethod("recv", cfact.getFastOptMethod("recv"));
64         rb_cBasicSocket.defineFastMethod("shutdown", cfact.getFastOptMethod("shutdown"));
65         rb_cBasicSocket.defineFastMethod("__getsockname", cfact.getFastMethod("getsockname"));
66         rb_cBasicSocket.defineFastMethod("__getpeername", cfact.getFastMethod("getpeername"));
67         rb_cBasicSocket.defineFastMethod("getsockname", cfact.getFastMethod("getsockname"));
68         rb_cBasicSocket.defineFastMethod("getpeername", cfact.getFastMethod("getpeername"));
69         rb_cBasicSocket.getMetaClass().defineFastMethod("do_not_reverse_lookup", cfact.getFastSingletonMethod("do_not_reverse_lookup"));
70         rb_cBasicSocket.getMetaClass().defineFastMethod("do_not_reverse_lookup=", cfact.getFastSingletonMethod("set_do_not_reverse_lookup", IRubyObject.class));
71     }
72
73     protected Channel JavaDoc socketChannel;
74
75     public RubyBasicSocket(Ruby runtime, RubyClass type) {
76         super(runtime, type);
77     }
78
79     protected void setChannel(Channel JavaDoc c) {
80         this.socketChannel = c;
81         try {
82             handler = new IOHandlerNio(getRuntime(), socketChannel);
83             handler.setIsSync(true);
84         } catch (IOException JavaDoc e) {
85             throw getRuntime().newIOError(e.getMessage());
86         }
87         registerIOHandler(handler);
88         modes = handler.getModes();
89     }
90
91     public IRubyObject write_send(IRubyObject[] args) {
92         return syswrite(args[0]);
93     }
94     
95     public IRubyObject recv(IRubyObject[] args) {
96         try {
97             return RubyString.newString(getRuntime(), ((IOHandlerNio) handler).recv(RubyNumeric.fix2int(args[0])));
98         } catch (IOHandler.BadDescriptorException e) {
99             throw getRuntime().newErrnoEBADFError();
100         } catch (EOFException JavaDoc e) {
101             // recv returns nil on EOF
102
return getRuntime().getNil();
103         } catch (IOException JavaDoc e) {
104             // All errors to sysread should be SystemCallErrors, but on a closed stream
105
// Ruby returns an IOError. Java throws same exception for all errors so
106
// we resort to this hack...
107
if ("Socket not open".equals(e.getMessage())) {
108                 throw getRuntime().newIOError(e.getMessage());
109             }
110             throw getRuntime().newSystemCallError(e.getMessage());
111         }
112     }
113
114     protected InetSocketAddress JavaDoc getLocalSocket() {
115         if (socketChannel instanceof SocketChannel JavaDoc) {
116             return (InetSocketAddress JavaDoc)((SocketChannel JavaDoc)socketChannel).socket().getLocalSocketAddress();
117         } else if (socketChannel instanceof ServerSocketChannel JavaDoc) {
118             return (InetSocketAddress JavaDoc)((ServerSocketChannel JavaDoc) socketChannel).socket().getLocalSocketAddress();
119         } else {
120             return null;
121         }
122     }
123
124     protected InetSocketAddress JavaDoc getRemoteSocket() {
125         if(socketChannel instanceof SocketChannel JavaDoc) {
126             return (InetSocketAddress JavaDoc)((SocketChannel JavaDoc)socketChannel).socket().getRemoteSocketAddress();
127         } else {
128             return null;
129         }
130     }
131
132     public IRubyObject getsockname() {
133         SocketAddress JavaDoc sock = getLocalSocket();
134         if(null == sock) {
135             throw getRuntime().newIOError("Not Supported");
136         }
137         return getRuntime().newString(sock.toString());
138     }
139
140     public IRubyObject getpeername() {
141         SocketAddress JavaDoc sock = getRemoteSocket();
142         if(null == sock) {
143             throw getRuntime().newIOError("Not Supported");
144         }
145         return getRuntime().newString(sock.toString());
146     }
147
148     public IRubyObject shutdown(IRubyObject[] args) {
149         if (getRuntime().getSafeLevel() >= 4 && tainted().isFalse()) {
150             throw getRuntime().newSecurityError("Insecure: can't shutdown socket");
151         }
152         
153         int how = 2;
154         if (args.length > 0) {
155             how = RubyNumeric.fix2int(args[0]);
156         }
157         if (how < 0 || 2 < how) {
158             throw getRuntime().newArgumentError("`how' should be either 0, 1, 2");
159         }
160         if (how != 2) {
161             throw getRuntime().newNotImplementedError("Shutdown currently only works with how=2");
162         }
163         return close();
164     }
165
166     public static IRubyObject do_not_reverse_lookup(IRubyObject recv) {
167         return recv.getRuntime().isDoNotReverseLookupEnabled() ? recv.getRuntime().getTrue() : recv.getRuntime().getFalse();
168     }
169     
170     public static IRubyObject set_do_not_reverse_lookup(IRubyObject recv, IRubyObject flag) {
171         recv.getRuntime().setDoNotReverseLookupEnabled(flag.isTrue());
172         return recv.getRuntime().isDoNotReverseLookupEnabled() ? recv.getRuntime().getTrue() : recv.getRuntime().getFalse();
173     }
174 }// RubyBasicSocket
175
Popular Tags