All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.objectos.udp.Channel Maven / Gradle / Ivy

There is a newer version: 0.1.8
Show newest version
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.udp;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @author [email protected] (Marcio Endo)
 */
class Channel {

  private final DatagramChannel channel;
  private final Selector selector;

  private final BoundTo boundTo;
  private final List packetActionList;

  private final ByteBuffer buffer = ByteBuffer.allocate(1024);

  private Channel(DatagramChannel channel, Selector selector, BoundTo boundTo, List packetActionList) {
    this.channel = channel;
    this.selector = selector;
    this.boundTo = boundTo;
    this.packetActionList = packetActionList;
  }

  public static Channel get(BoundTo boundTo, List packetActionList) throws UdpException {
    try {
      DatagramChannel channel = DatagramChannel.open();
      boundTo.bind(channel);
      channel.setOption(StandardSocketOptions.SO_BROADCAST, Boolean.TRUE);
      channel.configureBlocking(false);

      Selector selector = Selector.open();
      channel.register(selector, SelectionKey.OP_READ);

      return new Channel(channel, selector, boundTo, packetActionList);
    } catch (IOException e) {
      throw new UdpException(e);
    }
  }

  public void broadcast(Message message, int port) throws UdpException {
    SocketAddress target = boundTo.broadcast(port);
    send(message, target);
  }

  public void send(Message message, SocketAddress target) throws UdpException {
    try {
      if (channel.isOpen()) {
        Datagram datagram = message.datagram();
        ByteBuffer out = datagram.toByteBuffer();
        channel.send(out, target);
      }
    } catch (IOException e) {
      throw new UdpException(e);
    }
  }

  public void close() {
    try {
      channel.close();
    } catch (IOException e) {
      // what to do?
    }
  }

  public void receive() throws UdpException {
    try {
      receive0();
    } catch (IOException e) {
      close();
      throw new UdpException(e);
    }
  }

  private void receive0() throws IOException, UdpException {
    int select = selector.select();

    if (select == 0) {
      return;
    }

    Set selectedKeys = selector.selectedKeys();
    Iterator keyIterator = selectedKeys.iterator();
    while (keyIterator.hasNext()) {
      SelectionKey selectedKey = keyIterator.next();
      keyIterator.remove();

      if (!selectedKey.isValid()) {
        continue;
      }

      if (selectedKey.isReadable()) {
        buffer.clear();
        SocketAddress address = channel.receive(buffer);
        receive1(address);
      }
    }
  }

  private void receive1(SocketAddress address) throws UdpException {
    if (!(address instanceof InetSocketAddress)) {
      return;
    }

    InetSocketAddress theAddress = (InetSocketAddress) address;
    buffer.flip();

    Packet packet = new Packet(this, theAddress, buffer);
    for (PacketAction packetAction : packetActionList) {
      packetAction.onPacket(packet);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy