<commit_before>package ibis.ipl.impl.net.udp;

import ibis.ipl.impl.net.__;
import ibis.ipl.impl.net.NetAllocator;
import ibis.ipl.impl.net.NetDriver;
import ibis.ipl.impl.net.NetInput;
import ibis.ipl.impl.net.NetReceiveBuffer;
import ibis.ipl.impl.net.NetSendPortIdentifier;

import ibis.ipl.IbisException;
import ibis.ipl.IbisIOException;
import ibis.ipl.StaticProperties;

import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.SocketException;

/* Only for java >= 1.4 
 * import java.net.SocketTimeoutException;
 */
import java.io.InterruptedIOException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.util.Hashtable;


/**
 * The UDP input implementation.
 *
 * <BR><B>Note</B>: this first implementation allocate one receive socket per
 * input. It could be interesting to experiment with an implementation
 * using one socket per poller instead.
 */
public class UdpInput extends NetInput {

	/**
	 * The polling timeout in milliseconds.
	 *
	 * <BR><B>Note</B>: this will be replaced by a property setting in the future.
	 */
	private final int             pollTimeout    = 1; // milliseconds

	/**
	 * The reception timeout in milliseconds.
	 *
	 * <BR><B>Note</B>: this will be replaced by a property setting in the future.
	 */
	private final int             receiveTimeout = 1000; // milliseconds

	/**
	 * The UDP socket.
	 */
	private DatagramSocket 	      socket 	     = null;

	/**
	 * The UDP message wrapper.
	 */
	private DatagramPacket 	      packet 	     = null;

	/**
	 * The UDP driver instance.
	 */
	private Driver         	      driver 	     = null;

	/**
	 * The local socket IP address.
	 */
	private InetAddress    	      laddr  	     = null;

	/**
	 * The local socket IP port.
	 */
	private int            	      lport  	     =    0;

	/**
	 * The local MTU.
	 */
	private int            	      lmtu   	     =    0;

	/**
	 * The remote socket IP address.
	 */
	private InetAddress    	      raddr  	     = null;

	/**
	 * The remote socket IP port.
	 */
	private int            	      rport  	     =    0;

	/**
	 * The remote MTU.
	 */
	private int            	      rmtu   	     =    0;

	/**
	 * The current reception byte array.
	 */
	private byte []               data           = null;

	/**
	 * The current reception buffer.
	 */
	private NetReceiveBuffer      buffer 	     = null;

	/**
	 * The peer {@link ibis.ipl.impl.net.NetSendPort NetSendPort}
	 * local number.
	 */
	private Integer               rpn    	     = null;

	/**
	 * The buffer block allocator.
	 */
	private NetAllocator          allocator      = null;

	/**
	 * The current socket timeout.
	 */
	private int                   socketTimeout  =    0;

	/**
	 * Constructor.
	 *
	 * @param sp the properties of the input's 
	 * {@link ibis.ipl.impl.net.NetSendPort NetSendPort}.
	 * @param driver the TCP driver instance.
	 * @param input the controlling input.
	 */
	UdpInput(StaticProperties sp,
		 NetDriver        driver,
		 NetInput         input)
		throws IbisIOException {
		super(sp, driver, input);
	}

	/*
	 * Sets up an incoming UDP connection.
	 *
	 * <BR><B>Note</B>: this function also negociate the mtu.
	 * <BR><B>Note</B>: the current UDP mtu is arbitrarily fixed at 32kB.
	 *
	 * @param rpn {@inheritDoc}
	 * @param is {@inheritDoc}
	 * @param os {@inheritDoc}
	 */
	public void setupConnection(Integer            rpn,
				    ObjectInputStream  is,
				    ObjectOutputStream os)
		throws IbisIOException {
		this.rpn = rpn;
		 
		try {
			socket = new DatagramSocket(0, InetAddress.getLocalHost());
			lmtu = Math.min(socket.getReceiveBufferSize(), 32768);
			laddr = socket.getLocalAddress();
			lport = socket.getLocalPort();
		} catch (SocketException e) {
			throw new IbisIOException(e);
		} catch (IOException e) {
			throw new IbisIOException(e);
		}

		Hashtable lInfo = new Hashtable();
		lInfo.put("udp_address", laddr);
		lInfo.put("udp_port", 	 new Integer(lport));
		lInfo.put("udp_mtu",  	 new Integer(lmtu));
		sendInfoTable(os, lInfo);

		Hashtable rInfo = receiveInfoTable(is);
		raddr =  (InetAddress)rInfo.get("udp_address");
		rport = ((Integer)    rInfo.get("udp_port")  ).intValue();
		rmtu  = ((Integer)    rInfo.get("udp_mtu")   ).intValue();

		mtu       = Math.min(lmtu, rmtu);
		allocator = new NetAllocator(mtu);
		packet    = new DatagramPacket(new byte[mtu], mtu);

		setReceiveTimeout(receiveTimeout);
	}

	/**
	 * {@inheritDoc}
	 *
	 * <BR><B>Note</B>: This UDP polling implementation uses a timed out
	 * {@link DatagramSocket#receive(DatagramPacket)}. As the minimum timeout value is one
	 * millisecond, an unsuccessful polling operation is rather expensive.
	 *
	 * @return {@inheritDoc}
	 */
	public Integer poll() throws IbisIOException {
		activeNum = null;

		if (rpn == null) {
			return null;
		}
<commit_msg>- UDP runtime selection of timeout values<commit_after>package ibis.ipl.impl.net.udp;

import ibis.ipl.impl.net.__;
import ibis.ipl.impl.net.NetAllocator;
import ibis.ipl.impl.net.NetDriver;
import ibis.ipl.impl.net.NetInput;
import ibis.ipl.impl.net.NetReceiveBuffer;
import ibis.ipl.impl.net.NetSendPortIdentifier;

import ibis.ipl.IbisException;
import ibis.ipl.IbisIOException;
import ibis.ipl.StaticProperties;

import java.net.DatagramSocket;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.SocketException;

/* Only for java >= 1.4 
 * import java.net.SocketTimeoutException;
 */
import java.io.InterruptedIOException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.util.Hashtable;


/**
 * The UDP input implementation.
 *
 * <BR><B>Note</B>: this first implementation allocate one receive socket per
 * input. It could be interesting to experiment with an implementation
 * using one socket per poller instead.
 */
public class UdpInput extends NetInput {

	/**
	 * The default polling timeout in milliseconds.
	 *
	 * <BR><B>Note</B>: this will be replaced by a property setting in the future.
	 */
	private final int             defaultPollTimeout    = 1; // milliseconds

	/**
	 * The default reception timeout in milliseconds.
	 *
	 * <BR><B>Note</B>: this will be replaced by a property setting in the future.
	 */
	private final int             defaultReceiveTimeout = 1000; // milliseconds

	/**
	 * The polling timeout in milliseconds.
	 *
	 * <BR><B>Note</B>: this will be replaced by a property setting in the future.
	 */
	private int                   pollTimeout    = defaultPollTimeout; // milliseconds

	/**
	 * The reception timeout in milliseconds.
	 *
	 * <BR><B>Note</B>: this will be replaced by a property setting in the future.
	 */
	private int                   receiveTimeout = defaultReceiveTimeout; // milliseconds

	/**
	 * The UDP socket.
	 */
	private DatagramSocket 	      socket 	     = null;

	/**
	 * The UDP message wrapper.
	 */
	private DatagramPacket 	      packet 	     = null;

	/**
	 * The UDP driver instance.
	 */
	private Driver         	      driver 	     = null;

	/**
	 * The local socket IP address.
	 */
	private InetAddress    	      laddr  	     = null;

	/**
	 * The local socket IP port.
	 */
	private int            	      lport  	     =    0;

	/**
	 * The local MTU.
	 */
	private int            	      lmtu   	     =    0;

	/**
	 * The remote socket IP address.
	 */
	private InetAddress    	      raddr  	     = null;

	/**
	 * The remote socket IP port.
	 */
	private int            	      rport  	     =    0;

	/**
	 * The remote MTU.
	 */
	private int            	      rmtu   	     =    0;

	/**
	 * The current reception byte array.
	 */
	private byte []               data           = null;

	/**
	 * The current reception buffer.
	 */
	private NetReceiveBuffer      buffer 	     = null;

	/**
	 * The peer {@link ibis.ipl.impl.net.NetSendPort NetSendPort}
	 * local number.
	 */
	private Integer               rpn    	     = null;

	/**
	 * The buffer block allocator.
	 */
	private NetAllocator          allocator      = null;

	/**
	 * The current socket timeout.
	 */
	private int                   socketTimeout  =    0;

	/**
	 * Constructor.
	 *
	 * @param sp the properties of the input's 
	 * {@link ibis.ipl.impl.net.NetSendPort NetSendPort}.
	 * @param driver the TCP driver instance.
	 * @param input the controlling input.
	 */
	UdpInput(StaticProperties sp,
		 NetDriver        driver,
		 NetInput         input)
		throws IbisIOException {
		super(sp, driver, input);
	}

	/*
	 * Sets up an incoming UDP connection.
	 *
	 * <BR><B>Note</B>: this function also negociate the mtu.
	 * <BR><B>Note</B>: the current UDP mtu is arbitrarily fixed at 32kB.
	 *
	 * @param rpn {@inheritDoc}
	 * @param is {@inheritDoc}
	 * @param os {@inheritDoc}
	 */
	public void setupConnection(Integer            rpn,
				    ObjectInputStream  is,
				    ObjectOutputStream os)
		throws IbisIOException {
		this.rpn = rpn;
		 
		try {
			socket = new DatagramSocket(0, InetAddress.getLocalHost());
			lmtu = Math.min(socket.getReceiveBufferSize(), 32768);
			laddr = socket.getLocalAddress();
			lport = socket.getLocalPort();
		} catch (SocketException e) {
			throw new IbisIOException(e);
		} catch (IOException e) {
			throw new IbisIOException(e);
		}

		Hashtable lInfo = new Hashtable();
		lInfo.put("udp_address", laddr);
		lInfo.put("udp_port", 	 new Integer(lport));
		lInfo.put("udp_mtu",  	 new Integer(lmtu));
		sendInfoTable(os, lInfo);

		Hashtable rInfo = receiveInfoTable(is);
		raddr =  (InetAddress)rInfo.get("udp_address");
		rport = ((Integer)    rInfo.get("udp_port")  ).intValue();
		rmtu  = ((Integer)    rInfo.get("udp_mtu")   ).intValue();

		mtu       = Math.min(lmtu, rmtu);
		allocator = new NetAllocator(mtu);
		packet    = new DatagramPacket(new byte[mtu], mtu);

		String s = null;
		if ((s = getProperty("ReceiveTimeout")) != null) {
			receiveTimeout = Integer.valueOf(s).intValue();
		}
		if ((s = getProperty("PollingTimeout")) != null) {
			pollTimeout = Integer.valueOf(s).intValue();
		}

		setReceiveTimeout(receiveTimeout);
	}

	/**
	 * {@inheritDoc}
	 *
	 * <BR><B>Note</B>: This UDP polling implementation uses a timed out
	 * {@link DatagramSocket#receive(DatagramPacket)}. As the minimum timeout value is one
	 * millisecond, an unsuccessful polling operation is rather expensive.
	 *
	 * @return {@inheritDoc}
	 */
	public Integer poll() throws IbisIOException {
		activeNum = null;

		if (rpn == null) {
			return null;
		}

{'fraction_non_alphanumeric': 0.11500474833808168, 'fraction_numerical': 0.003798670465337132, 'mean_word_length': 2.552968960863698, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7641632', 'n_tokens_mistral': 3623, 'n_tokens_neox': 3192, 'n_words': 911}