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;
022
023import org.jgrapes.core.Channel;
024import org.jgrapes.core.CompletionEvent;
025import org.jgrapes.core.Event;
026import org.jgrapes.io.NioHandler;
027
028/**
029 * Signal a registration request to a registry.
030 */
031public class NioRegistration extends Event<NioRegistration.Registration> {
032
033    private final NioHandler handler;
034    private final SelectableChannel ioChannel;
035    private final int ops;
036
037    /**
038     * Passed during registration. allows the requester to update
039     * the mask operations that it is interested in.
040     */
041    public abstract static class Registration {
042
043        /**
044         * Update the mask for interesting operations.
045         *
046         * @param ops the operations.
047         */
048        public abstract void updateInterested(int ops);
049    }
050
051    /**
052     * The completion event for a {@link NioRegistration} event.
053     */
054    public static class Completed
055            extends CompletionEvent<NioRegistration> {
056
057        /**
058         * Instantiates a new event.
059         *
060         * @param monitoredEvent the monitored event
061         * @param channels the channels
062         */
063        public Completed(NioRegistration monitoredEvent, Channel... channels) {
064            super(monitoredEvent, channels);
065        }
066    }
067
068    /**
069     * Creates a new registration event for the given handler, using the given
070     * NIO channel and handling the given operations. The completed event
071     * for this event is to be sent to the given channel. 
072     * 
073     * @param handler the handler
074     * @param ioChannel the NIO channel
075     * @param ops the supported operations
076     * @param completedTarget where to send the completed event to
077     */
078    public NioRegistration(NioHandler handler, SelectableChannel ioChannel,
079            int ops, Channel completedTarget) {
080        new Completed(this, completedTarget);
081        this.handler = handler;
082        this.ioChannel = ioChannel;
083        this.ops = ops;
084    }
085
086    /**
087     * @return the handler
088     */
089    public NioHandler handler() {
090        return handler;
091    }
092
093    /**
094     * @return the channel
095     */
096    public SelectableChannel ioChannel() {
097        return ioChannel;
098    }
099
100    /**
101     * @return the ops
102     */
103    public int ops() {
104        return ops;
105    }
106
107}