<commit_before>package org.menacheri.communication;

import java.net.SocketAddress;

import org.jboss.netty.channel.socket.DatagramChannel;
import org.menacheri.app.ISession;
import org.menacheri.communication.IDeliveryGuaranty.DeliveryGuaranty;
import org.menacheri.communication.IMessageSender.IFast;
import org.menacheri.event.Events;


/**
 * This class is used to send messages to a remote UDP client or server. An
 * instance of this class will be created when the {@link Events#CONNECT_UDP}
 * event is sent to a {@link ISession}
 * 
 * @author <NAME>
 * 
 */
public class NettyUDPMessageSender implements IFast
{
	private final SocketAddress remoteAddress;
	private final DatagramChannel channel;
	private static final IDeliveryGuaranty DELIVERY_GUARANTY = DeliveryGuaranty.FAST;

	public NettyUDPMessageSender(SocketAddress remoteAddress,
			DatagramChannel channel)
	{
		this.remoteAddress = remoteAddress;
		this.channel = channel;
	}

	@Override
	public Object sendMessage(Object message)
	{
		return channel.write(message, remoteAddress);
	}

	@Override
	public IDeliveryGuaranty getDeliveryGuaranty()
	{
		return DELIVERY_GUARANTY;
	}

	public SocketAddress getRemoteAddress()
	{
		return remoteAddress;
	}

	public DatagramChannel getChannel()
	{
		return channel;
	}

	@Override
	public String toString()
	{
		String channelId = "UDP Channel with id: ";
		if (null != channel)
		{
			channelId += channel.getId();
		}
		else
		{
			channelId += "0";
		}
		String sender = "Netty " + channelId + " RemoteAddress: "
				+ remoteAddress;
		return sender;
	}
}<commit_msg>Added close method. Also added dependency on session registry service in order to remove session when message sender is closed.<commit_after>package org.menacheri.communication;

import java.net.SocketAddress;

import org.jboss.netty.channel.socket.DatagramChannel;
import org.menacheri.app.ISession;
import org.menacheri.communication.IDeliveryGuaranty.DeliveryGuaranty;
import org.menacheri.communication.IMessageSender.IFast;
import org.menacheri.event.Events;
import org.menacheri.event.impl.NetworkEvent;
import org.menacheri.handlers.netty.UDPUpstreamHandler;
import org.menacheri.service.ISessionRegistryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class is used to send messages to a remote UDP client or server. An
 * instance of this class will be created by the {@link UDPUpstreamHandler} when
 * a {@link Events#CONNECT} event is received from client. The created instance
 * of this class is then sent as payload of a {@link NetworkEvent} to the
 * {@link ISession}.
 * 
 * 
 * @author <NAME>
 * 
 */
public class NettyUDPMessageSender implements IFast
{
	private static final Logger LOG = LoggerFactory
			.getLogger(NettyUDPMessageSender.class);
	private final SocketAddress remoteAddress;
	private final DatagramChannel channel;
	private final ISessionRegistryService sessionRegistryService;

	private static final IDeliveryGuaranty DELIVERY_GUARANTY = DeliveryGuaranty.FAST;

	public NettyUDPMessageSender(SocketAddress remoteAddress,
			DatagramChannel channel,
			ISessionRegistryService sessionRegistryService)
	{
		this.remoteAddress = remoteAddress;
		this.channel = channel;
		this.sessionRegistryService = sessionRegistryService;
	}

	@Override
	public Object sendMessage(Object message)
	{
		return channel.write(message, remoteAddress);
	}

	@Override
	public IDeliveryGuaranty getDeliveryGuaranty()
	{
		return DELIVERY_GUARANTY;
	}

	@Override
	public void close()
	{
		ISession session = sessionRegistryService.getSession(remoteAddress);
		if (sessionRegistryService.removeSession(remoteAddress))
		{
			LOG.info("Successfully removed session: {}", session);
		}
		else
		{
			LOG.trace("No udp session found for address: {}", remoteAddress);
		}

	}

	public SocketAddress getRemoteAddress()
	{
		return remoteAddress;
	}

	public DatagramChannel getChannel()
	{
		return channel;
	}

	@Override
	public String toString()
	{
		String channelId = "UDP Channel with id: ";
		if (null != channel)
		{
			channelId += channel.getId();
		}
		else
		{
			channelId += "0";
		}
		String sender = "Netty " + channelId + " RemoteAddress: "
				+ remoteAddress;
		return sender;
	}

	public ISessionRegistryService getSessionRegistryService()
	{
		return sessionRegistryService;
	}
}
{'fraction_non_alphanumeric': 0.08470261513538532, 'fraction_numerical': 0.0009257116408238834, 'mean_word_length': 4.778074866310161, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5220224', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1304, 'n_words': 345}