KickJava   Java API By Example, From Geeks To Geeks.

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


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.BindException 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.ServerSocketChannel JavaDoc;
38
39 import org.jruby.Ruby;
40 import org.jruby.RubyClass;
41 import org.jruby.RubyFixnum;
42 import org.jruby.RubyNumeric;
43 import org.jruby.runtime.CallbackFactory;
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 RubyTCPServer extends RubyTCPSocket {
51     static void createTCPServer(Ruby runtime) {
52         RubyClass rb_cTCPServer = runtime.defineClass("TCPServer", runtime.getClass("TCPSocket"), TCPSERVER_ALLOCATOR);
53         CallbackFactory cfact = runtime.callbackFactory(RubyTCPServer.class);
54
55         rb_cTCPServer.defineFastMethod("initialize", cfact.getFastMethod("initialize",IRubyObject.class, IRubyObject.class));
56         rb_cTCPServer.defineFastMethod("peeraddr", cfact.getFastOptMethod("peeraddr"));
57         rb_cTCPServer.defineFastMethod("getpeername", cfact.getFastOptMethod("getpeername"));
58         rb_cTCPServer.defineFastMethod("accept", cfact.getFastMethod("accept"));
59         rb_cTCPServer.defineFastMethod("close", cfact.getFastMethod("close"));
60         rb_cTCPServer.defineFastMethod("listen", cfact.getFastMethod("listen",IRubyObject.class));
61         rb_cTCPServer.getMetaClass().defineFastMethod("open", cfact.getFastOptSingletonMethod("open"));
62         
63         runtime.getObject().setConstant("TCPserver",rb_cTCPServer);
64     }
65
66     private static ObjectAllocator TCPSERVER_ALLOCATOR = new ObjectAllocator() {
67         public IRubyObject allocate(Ruby runtime, RubyClass klass) {
68             return new RubyTCPServer(runtime, klass);
69         }
70     };
71
72     public RubyTCPServer(Ruby runtime, RubyClass type) {
73         super(runtime, type);
74     }
75
76     private ServerSocketChannel JavaDoc ssc;
77     private InetSocketAddress JavaDoc socket_address;
78
79     public IRubyObject initialize(IRubyObject hostname, IRubyObject port) {
80         if(hostname.isNil()) {
81             hostname = getRuntime().newString("0.0.0.0");
82         }
83         String JavaDoc shost = hostname.convertToString().toString();
84         try {
85             InetAddress JavaDoc addr = InetAddress.getByName(shost);
86             ssc = ServerSocketChannel.open();
87             socket_address = new InetSocketAddress JavaDoc(addr, RubyNumeric.fix2int(port));
88             ssc.socket().bind(socket_address);
89             setChannel(ssc);
90         } catch(UnknownHostException JavaDoc e) {
91             throw sockerr(this, "initialize: name or service not known");
92         } catch(BindException JavaDoc e) {
93             // e.printStackTrace();
94
throw getRuntime().newErrnoEADDRINUSEError();
95         } catch(IOException JavaDoc e) {
96             throw sockerr(this, "initialize: name or service not known");
97         }
98
99         return this;
100     }
101
102     public IRubyObject accept() {
103         RubyTCPSocket socket = new RubyTCPSocket(getRuntime(),getRuntime().getClass("TCPSocket"));
104         try {
105             socket.setChannel(ssc.accept());
106         } catch(IOException JavaDoc e) {
107             throw sockerr(this, "problem when accepting");
108         }
109         return socket;
110     }
111
112     public IRubyObject close() {
113         try {
114             ssc.close();
115         } catch(IOException JavaDoc e) {
116             throw sockerr(this, "problem when closing");
117         }
118         return getRuntime().getNil();
119     }
120
121     public IRubyObject listen(IRubyObject backlog) {
122         return RubyFixnum.zero(getRuntime());
123     }
124
125     public IRubyObject peeraddr(IRubyObject[] args) {
126         throw getRuntime().newNotImplementedError("not supported");
127     }
128
129     public IRubyObject getpeername(IRubyObject[] args) {
130         throw getRuntime().newNotImplementedError("not supported");
131     }
132
133     public static IRubyObject open(IRubyObject recv, IRubyObject[] args) {
134         return recv.callMethod(recv.getRuntime().getCurrentContext(),"new",args);
135     }
136 }// RubyTCPServer
137
Popular Tags