示例#1
0
文件: Nio4r.java 项目: tpetchel/nio4r
    @JRubyMethod
    public IRubyObject initialize(
        ThreadContext context,
        IRubyObject selectable,
        IRubyObject interests,
        IRubyObject selector) {
      this.io = RubyIO.convertToIO(context, selectable);
      this.interests = interests;
      this.selector = selector;

      this.value = context.nil;
      this.closed = context.getRuntime().getFalse();

      return context.nil;
    }
示例#2
0
文件: Nio4r.java 项目: tpetchel/nio4r
    @JRubyMethod
    public IRubyObject register(ThreadContext context, IRubyObject io, IRubyObject interests) {
      Ruby runtime = context.getRuntime();
      Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();

      if (!this.selector.isOpen()) {
        throw context.getRuntime().newIOError("selector is closed");
      }

      if (!(rawChannel instanceof SelectableChannel)) {
        throw runtime.newArgumentError("not a selectable IO object");
      }

      SelectableChannel channel = (SelectableChannel) rawChannel;

      try {
        channel.configureBlocking(false);
      } catch (IOException ie) {
        throw runtime.newIOError(ie.getLocalizedMessage());
      }

      int interestOps = Nio4r.symbolToInterestOps(runtime, channel, interests);
      SelectionKey key;

      key = this.cancelledKeys.remove(channel);

      if (key != null) {
        key.interestOps(interestOps);
      } else {
        try {
          key = channel.register(this.selector, interestOps);
        } catch (java.lang.IllegalArgumentException ia) {
          throw runtime.newArgumentError("mode not supported for this object: " + interests);
        } catch (java.nio.channels.ClosedChannelException cce) {
          throw context.runtime.newIOError(cce.getLocalizedMessage());
        }
      }

      RubyClass monitorClass = runtime.getModule("NIO").getClass("Monitor");
      Monitor monitor = (Monitor) monitorClass.newInstance(context, io, interests, this, null);
      monitor.setSelectionKey(key);

      return monitor;
    }
示例#3
0
文件: Nio4r.java 项目: tpetchel/nio4r
    @JRubyMethod(name = "registered?")
    public IRubyObject isRegistered(ThreadContext context, IRubyObject io) {
      Ruby runtime = context.getRuntime();
      Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();

      if (!(rawChannel instanceof SelectableChannel)) {
        throw runtime.newArgumentError("not a selectable IO object");
      }

      SelectableChannel channel = (SelectableChannel) rawChannel;
      SelectionKey key = channel.keyFor(this.selector);

      if (key == null) return context.nil;

      if (((Monitor) key.attachment()).isClosed(context) == runtime.getTrue()) {
        return runtime.getFalse();
      } else {
        return runtime.getTrue();
      }
    }
示例#4
0
文件: Nio4r.java 项目: tpetchel/nio4r
    @JRubyMethod
    public IRubyObject deregister(ThreadContext context, IRubyObject io) {
      Ruby runtime = context.getRuntime();
      Channel rawChannel = RubyIO.convertToIO(context, io).getChannel();

      if (!(rawChannel instanceof SelectableChannel)) {
        throw runtime.newArgumentError("not a selectable IO object");
      }

      SelectableChannel channel = (SelectableChannel) rawChannel;
      SelectionKey key = channel.keyFor(this.selector);

      if (key == null) return context.nil;

      Monitor monitor = (Monitor) key.attachment();
      monitor.close(context, runtime.getFalse());
      cancelledKeys.put(channel, key);

      return monitor;
    }