KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
31
32 import java.net.ConnectException JavaDoc;
33 import java.net.InetAddress JavaDoc;
34 import java.net.InetSocketAddress JavaDoc;
35 import java.net.UnknownHostException JavaDoc;
36
37 import java.nio.channels.SocketChannel JavaDoc;
38
39 import org.jruby.Ruby;
40 import org.jruby.RubyClass;
41 import org.jruby.RubyNumeric;
42 import org.jruby.runtime.CallbackFactory;
43 import org.jruby.runtime.Block;
44 import org.jruby.runtime.ObjectAllocator;
45 import org.jruby.runtime.builtin.IRubyObject;
46
47 /**
48  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
49  */

50 public class RubyTCPSocket extends RubyIPSocket {
51     static void createTCPSocket(Ruby runtime) {
52         RubyClass rb_cTCPSocket = runtime.defineClass("TCPSocket", runtime.getClass("IPSocket"), TCPSOCKET_ALLOCATOR);
53         CallbackFactory cfact = runtime.callbackFactory(RubyTCPSocket.class);
54
55         rb_cTCPSocket.includeModule(runtime.getClass("Socket").getConstant("Constants"));
56
57         rb_cTCPSocket.defineFastMethod("initialize", cfact.getFastMethod("initialize",IRubyObject.class, IRubyObject.class));
58         rb_cTCPSocket.defineFastMethod("setsockopt", cfact.getFastOptMethod("setsockopt"));
59         rb_cTCPSocket.getMetaClass().defineFastMethod("gethostbyname", cfact.getFastSingletonMethod("gethostbyname", IRubyObject.class));
60         rb_cTCPSocket.getMetaClass().defineMethod("open", cfact.getOptSingletonMethod("open"));
61
62         runtime.getObject().setConstant("TCPsocket",rb_cTCPSocket);
63     }
64
65     private static ObjectAllocator TCPSOCKET_ALLOCATOR = new ObjectAllocator() {
66         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
67             return new RubyTCPSocket(runtime, klass);
68         }
69     };
70
71     public RubyTCPSocket(Ruby runtime, RubyClass type) {
72         super(runtime, type);
73     }
74
75     public IRubyObject initialize(IRubyObject arg1, IRubyObject port) {
76         try {
77             InetSocketAddress JavaDoc addr = new InetSocketAddress JavaDoc(InetAddress.getByName(arg1.convertToString().toString()),RubyNumeric.fix2int(port));
78             SocketChannel JavaDoc channel = SocketChannel.open(addr);
79             channel.finishConnect();
80             setChannel(channel);
81         } catch(ConnectException JavaDoc e) {
82             throw getRuntime().newErrnoECONNREFUSEDError();
83         } catch(UnknownHostException JavaDoc e) {
84             throw sockerr(this, "initialize: name or service not known");
85         } catch(IOException JavaDoc e) {
86             throw sockerr(this, "initialize: name or service not known");
87         }
88         return this;
89     }
90
91     public IRubyObject setsockopt(IRubyObject[] args) {
92         // Stubbed out
93
return getRuntime().getNil();
94     }
95
96     public static IRubyObject open(IRubyObject recv, IRubyObject[] args, Block block) {
97         RubyTCPSocket sock = (RubyTCPSocket)recv.callMethod(recv.getRuntime().getCurrentContext(),"new",args);
98         if (!block.isGiven()) return sock;
99
100         try {
101             return recv.getRuntime().getCurrentContext().yield(sock, block);
102         } finally {
103             if (sock.isOpen()) sock.close();
104         }
105     }
106
107     public static IRubyObject gethostbyname(IRubyObject recv, IRubyObject hostname) {
108         try {
109             IRubyObject[] ret = new IRubyObject[4];
110             Ruby r = recv.getRuntime();
111             InetAddress JavaDoc addr = InetAddress.getByName(hostname.convertToString().toString());
112             ret[0] = r.newString(addr.getCanonicalHostName());
113             ret[1] = r.newArray();
114             ret[2] = r.newFixnum(2); //AF_INET
115
ret[3] = r.newString(addr.getHostAddress());
116             return r.newArrayNoCopy(ret);
117         } catch(UnknownHostException JavaDoc e) {
118             throw sockerr(recv, "gethostbyname: name or service not known");
119         }
120     }
121 }// RubyTCPSocket
122
Popular Tags