Created
November 24, 2015 01:13
-
-
Save mrbrdo/e5bcd63e0688422fb69b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@JRubyMethod(meta = true) | |
public static IRubyObject for_fd(ThreadContext context, IRubyObject _klass, IRubyObject _fileno) { | |
Ruby runtime = context.runtime; | |
int fileno = (int)_fileno.convertToInteger().getLongValue(); | |
RubyClass klass = (RubyClass)_klass; | |
final RubyClass UNIXSocket = runtime.getClass("UNIXSocket"); | |
RubyUNIXSocket unixSocket = (RubyUNIXSocket)(Helpers.invoke(context, UNIXSocket, "allocate")); | |
try { | |
// begin hack | |
Constructor<UnixSocketChannel>[] ca = (Constructor<UnixSocketChannel>[])UnixSocketChannel.class.getDeclaredConstructors(); | |
Constructor<UnixSocketChannel> constructor = ca[2]; | |
for (int i=0; i<ca.length; i++) { | |
Class[] argTypes = ca[i].getParameterTypes(); | |
if (argTypes.length == 2 && argTypes[1].getName() == "int") { | |
constructor = ca[i]; | |
} | |
} | |
Object[] args = new Object[] { fileno, (int)(SelectionKey.OP_READ | SelectionKey.OP_WRITE) }; | |
//Constructor<UnixSocketChannel> constructor = UnixSocketChannel.class.getDeclaredConstructor(args.getClass()); | |
constructor.setAccessible(true); | |
UnixSocketChannel channel = constructor.newInstance(args); | |
//UnixSocketChannel channel = new UnixSocketChannel(fileno, (int)(SelectionKey.OP_READ | SelectionKey.OP_WRITE)); | |
unixSocket.init_sock(runtime, channel); | |
return unixSocket; | |
} catch (InstantiationException e) { | |
System.out.println("InstantiationException"); | |
return runtime.getNil(); | |
} catch (IllegalAccessException e) { | |
System.out.println("IllegalAccessException"); | |
return runtime.getNil(); | |
} catch (InvocationTargetException e) { | |
System.out.println("InvocationTargetException"); | |
return runtime.getNil(); | |
} catch (java.lang.SecurityException e) { | |
return runtime.getNil(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment