package org.lantern.udtrelay;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

import org.apache.commons.io.IOUtils;
import org.lantern.LanternUtils;
import org.lastbamboo.common.offer.answer.OfferAnswer;
import org.lastbamboo.common.offer.answer.OfferAnswerListener;
import org.littleshoot.util.FiveTuple;
import org.littleshoot.util.RelayingSocketHandler;
import org.littleshoot.util.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * This class listens for incoming NAT-traversed five tuples (endpoint pairs
 * plus protocol) 
 */
public class UdtRelayServerFiveTupleListener 
    implements OfferAnswerListener<FiveTuple>{
    
    private final Logger log = LoggerFactory.getLogger(getClass());
    
    @Override
    public void onOfferAnswerFailed(final OfferAnswer offerAnswer) {
        // We don't really care about this on the server side.
        log.debug("Offer/answer failed");
    }

    @Override
    public void onUdpSocket(final FiveTuple sock) {
        log.info("Received inbound P2P UDT connection from: {}", sock);
        final InetSocketAddress local = sock.getLocal();
        final UdtRelayProxy proxy = 
            new UdtRelayProxy(local, 
                LanternUtils.PLAINTEXT_LOCALHOST_PROXY_PORT);
        
        try {
            proxy.run();
        } catch (final Exception e) {
            log.warn("Exception running UDT proxy?", e);
        }
    }

    @Override
    public void onTcpSocket(final Socket socket) {
        log.warn("Received TCP socket on server", ThreadUtils.dumpStack());
        final String msg = 
            "Unexpectedly received TCP socket for UDT relay server";
        log.error(msg);
        final InetSocketAddress local = 
            new InetSocketAddress("127.0.0.1", 
                LanternUtils.PLAINTEXT_LOCALHOST_PROXY_PORT);
        final RelayingSocketHandler sh = new RelayingSocketHandler(local);
        try {
            sh.onSocket("not-used", socket);
        } catch (final IOException e) {
            // Should already be closed at this point, but just make sure.
            IOUtils.closeQuietly(socket);
        }
        //throw new RuntimeException(msg);
    }
}

@@ -28,36 +28,8 @@
 y;%0A%0A
-import java.io.IOException;%0A
 impo
@@ -88,46 +88,8 @@
 t;%0A%0A
-import org.apache.commons.io.IOUtils;%0A
 impo
@@ -278,59 +278,8 @@
 le;%0A
-import org.littleshoot.util.RelayingSocketHandler;%0A
 impo
@@ -1404,84 +1404,8 @@
 ) %7B%0A
-        log.warn(%22Received TCP socket on server%22, ThreadUtils.dumpStack());%0A
     
@@ -1494,16 +1494,20 @@
 y server
+: %7B%7D
 %22;%0A     
@@ -1526,499 +1526,59 @@
 (msg
-);%0A        final InetSocketAddress local = %0A            new InetSocketAddress(%22127.0.0.1%22, %0A                LanternUtils.PLAINTEXT_LOCALHOST_PROXY_PORT);%0A        final RelayingSocketHandler sh = new RelayingSocketHandler(local);%0A        try %7B%0A            sh.onSocket(%22not-used%22, socket);%0A        %7D catch (final IOException e) %7B%0A            // Should already be closed at this point, but just make sure.%0A            IOUtils.closeQuietly(socket);%0A        %7D%0A        //throw new RuntimeException
+, ThreadUtils.dumpStack());%0A        throw new Error
 (msg

Revert to disallowing TCP over UDT listener