001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016-2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.io.events;
020
021import java.nio.channels.SelectableChannel;
022import org.jgrapes.core.Channel;
023import org.jgrapes.core.CompletionEvent;
024import org.jgrapes.core.Event;
025import org.jgrapes.io.NioHandler;
026
027/**
028 * Signal a registration request to a registry.
029 */
030public class NioRegistration extends Event<NioRegistration.Registration> {
031
032    private final NioHandler handler;
033    private final SelectableChannel ioChannel;
034    private final int ops;
035
036    /**
037     * Passed during registration. allows the requester to update
038     * the mask operations that it is interested in.
039     */
040    public abstract static class Registration {
041
042        /**
043         * Update the mask for interesting operations.
044         *
045         * @param ops the operations.
046         */
047        public abstract void updateInterested(int ops);
048    }
049
050    /**
051     * The completion event for a {@link NioRegistration} event.
052     */
053    public static class Completed
054            extends CompletionEvent<NioRegistration> {
055
056        /**
057         * Instantiates a new event.
058         *
059         * @param monitoredEvent the monitored event
060         * @param channels the channels
061         */
062        public Completed(NioRegistration monitoredEvent, Channel... channels) {
063            super(monitoredEvent, channels);
064        }
065    }
066
067    /**
068     * Creates a new registration event for the given handler, using the given
069     * NIO channel and handling the given operations. The completed event
070     * for this event is to be sent to the given channel. 
071     * 
072     * @param handler the handler
073     * @param ioChannel the NIO channel
074     * @param ops the supported operations
075     * @param completedTarget where to send the completed event to
076     */
077    public NioRegistration(NioHandler handler, SelectableChannel ioChannel,
078            int ops, Channel completedTarget) {
079        new Completed(this, completedTarget);
080        this.handler = handler;
081        this.ioChannel = ioChannel;
082        this.ops = ops;
083    }
084
085    /**
086     * @return the handler
087     */
088    public NioHandler handler() {
089        return handler;
090    }
091
092    /**
093     * @return the channel
094     */
095    public SelectableChannel ioChannel() {
096        return ioChannel;
097    }
098
099    /**
100     * @return the ops
101     */
102    public int ops() {
103        return ops;
104    }
105
106}