KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.InetAddress JavaDoc;
31 import java.net.InetSocketAddress JavaDoc;
32 import java.net.UnknownHostException JavaDoc;
33
34 import org.jruby.Ruby;
35 import org.jruby.RubyClass;
36 import org.jruby.exceptions.RaiseException;
37 import org.jruby.runtime.CallbackFactory;
38 import org.jruby.runtime.ObjectAllocator;
39 import org.jruby.runtime.builtin.IRubyObject;
40
41 /**
42  * @author <a HREF="mailto:ola.bini@ki.se">Ola Bini</a>
43  */

44 public class RubyIPSocket extends RubyBasicSocket {
45     static void createIPSocket(Ruby runtime) {
46         RubyClass rb_cIPSocket = runtime.defineClass("IPSocket", runtime.getClass("BasicSocket"), IPSOCKET_ALLOCATOR);
47         CallbackFactory cfact = runtime.callbackFactory(RubyIPSocket.class);
48
49         rb_cIPSocket.defineFastMethod("addr", cfact.getFastMethod("addr"));
50         rb_cIPSocket.defineFastMethod("peeraddr", cfact.getFastMethod("peeraddr"));
51         rb_cIPSocket.getMetaClass().defineFastMethod("getaddress", cfact.getFastSingletonMethod("getaddress", IRubyObject.class));
52
53         runtime.getObject().setConstant("IPsocket",rb_cIPSocket);
54     }
55     
56     private static ObjectAllocator IPSOCKET_ALLOCATOR = new ObjectAllocator() {
57         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
58             return new RubyIPSocket(runtime, klass);
59         }
60     };
61
62     public RubyIPSocket(Ruby runtime, RubyClass type) {
63         super(runtime, type);
64     }
65
66     protected static RuntimeException JavaDoc sockerr(IRubyObject recv, String JavaDoc msg) {
67         return new RaiseException(recv.getRuntime(), recv.getRuntime().getClass("SocketError"), null, true);
68     }
69
70     private IRubyObject addrFor(InetSocketAddress JavaDoc addr) {
71         IRubyObject[] ret = new IRubyObject[4];
72         Ruby r = getRuntime();
73         ret[0] = r.newString("AF_INET");
74         ret[1] = r.newFixnum(addr.getPort());
75         if(r.isDoNotReverseLookupEnabled()) {
76             ret[2] = r.newString(addr.getAddress().getHostAddress());
77         } else {
78             ret[2] = r.newString(addr.getHostName());
79         }
80         ret[3] = r.newString(addr.getAddress().getHostAddress());
81         return r.newArrayNoCopy(ret);
82     }
83
84     public IRubyObject addr() {
85         return addrFor(getLocalSocket());
86     }
87
88     public IRubyObject peeraddr() {
89         return addrFor(getRemoteSocket());
90     }
91
92     public static IRubyObject getaddress(IRubyObject recv, IRubyObject hostname) {
93         try {
94             return recv.getRuntime().newString(InetAddress.getByName(hostname.convertToString().toString()).getHostAddress());
95         } catch(UnknownHostException JavaDoc e) {
96             throw sockerr(recv, "getaddress: name or service not known");
97         }
98     }
99 }// RubyIPSocket
100
Popular Tags