Java Code Examples for java.nio.channels.DatagramChannel#isConnected()

The following examples show how to use java.nio.channels.DatagramChannel#isConnected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UdpConnection.java    From kryonet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public InetSocketAddress readFromAddress () throws IOException {
	DatagramChannel datagramChannel = this.datagramChannel;
	if (datagramChannel == null) throw new SocketException("Connection is closed.");
	lastCommunicationTime = System.currentTimeMillis();
	if(!datagramChannel.isConnected())
		return (InetSocketAddress)datagramChannel.receive(readBuffer); // always null on Android >= 5.0
	datagramChannel.read(readBuffer);
	return connectedAddress;
}
 
Example 2
Source File: GelfUDPSender.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isConnected(DatagramChannel channel) {
    return channel.isConnected();
}
 
Example 3
Source File: AbstractDatagramChannelBinding.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void internalReceiveCommand(String itemName, Command command) {
    P provider = findFirstMatchingBindingProvider(itemName);

    if (provider == null) {
        logger.warn("cannot find matching binding provider [itemName={}, command={}]", itemName, command);
        return;
    }

    if (command != null) {
        List<Command> commands = provider.getQualifiedCommands(itemName, command);

        for (Command someCommand : commands) {
            Channel theChannel = null;
            if (useAddressMask && (provider.getHost(itemName, someCommand).equals("*")
                    || provider.getPortAsString(itemName, someCommand).equals("*"))) {
                theChannel = channels.get(itemName, someCommand, provider.getDirection(itemName, someCommand),
                        provider.getHost(itemName, someCommand), provider.getPortAsString(itemName, someCommand));
            } else {
                theChannel = channels.get(itemName, someCommand, provider.getDirection(itemName, someCommand),
                        new InetSocketAddress(provider.getHost(itemName, someCommand),
                                provider.getPort(itemName, someCommand)));
            }
            DatagramChannel theDatagramChannel = null;
            if (theChannel != null) {
                theDatagramChannel = theChannel.channel;
            }
            if (theDatagramChannel != null) {

                boolean result = internalReceiveChanneledCommand(itemName, someCommand, theChannel,
                        command.toString());

                if (!theDatagramChannel.isConnected() && theDatagramChannel != listenerChannel) {

                    logger.warn(
                            "The channel for {} has a connection problem. Data will queued to the new channel when it is successfully set up.",
                            theChannel.remote);

                    Scheduler scheduler = null;
                    try {
                        scheduler = StdSchedulerFactory.getDefaultScheduler();
                    } catch (SchedulerException e1) {
                        logger.warn("An exception occurred while getting the Quartz scheduler: {}",
                                e1.getMessage());
                    }

                    JobDataMap map = new JobDataMap();
                    map.put("Channel", theChannel);
                    map.put("Binding", this);

                    JobDetail job = newJob(ReconnectJob.class)
                            .withIdentity(Integer.toHexString(hashCode()) + "-Reconnect-"
                                    + Long.toString(System.currentTimeMillis()), this.toString())
                            .usingJobData(map).build();

                    Trigger trigger = newTrigger()
                            .withIdentity(Integer.toHexString(hashCode()) + "-Reconnect-"
                                    + Long.toString(System.currentTimeMillis()), this.toString())
                            .startNow().build();

                    try {
                        if (job != null && trigger != null) {
                            if (!theChannel.isReconnecting) {
                                theChannel.isReconnecting = true;
                                scheduler.scheduleJob(job, trigger);
                            }
                        }
                    } catch (SchedulerException e) {
                        logger.warn("An exception occurred while scheduling a job with the Quartz Scheduler {}",
                                e.getMessage());
                    }
                }

                if (result) {
                    List<Class<? extends State>> stateTypeList = provider.getAcceptedDataTypes(itemName,
                            someCommand);
                    State newState = createStateFromString(stateTypeList, command.toString());

                    if (newState != null) {
                        eventPublisher.postUpdate(itemName, newState);
                    }
                }
            } else {
                logger.warn("There is no channel that services [itemName={}, command={}]", itemName, command);
            }
        }
    }
}