// Path: app/src/main/java/com/batsw/anonimitychat/chat/listener/IncomingConnectionListenerManager.java
// public class IncomingConnectionListenerManager {
//     private List<IIncomingConnectionListener> mIncomingConnectionListenerList = null;
// 
//     public IncomingConnectionListenerManager() {
//         mIncomingConnectionListenerList = new ArrayList<>();
//     }
// 
//     public void triggerPartnerChatRequest(String partnerHostname) {
//         for (IIncomingConnectionListener torStatusListener : mIncomingConnectionListenerList) {
// 
//             torStatusListener.triggerIncomingPartnerConnectionEvent(partnerHostname);
// 
//         }
//     }
// 
//     public void addIncomingConnectionListener(IIncomingConnectionListener iIncomingConnectionListener) {
//         mIncomingConnectionListenerList.add(iIncomingConnectionListener);
//     }
// 
//     public void removeIncomingConnectionListener(ITorBundleStatusListener iTorBundleStatusListener) {
//         if (mIncomingConnectionListenerList.contains(iTorBundleStatusListener)) {
//             mIncomingConnectionListenerList.remove(iTorBundleStatusListener);
//         }
//     }
// }
// 
// Path: app/src/main/java/com/batsw/anonimitychat/chat/message/MessageReceivedListenerManager.java
// public class MessageReceivedListenerManager {
//     private static final String LOG = MessageReceivedListenerManager.class.getSimpleName();
// 
//     private HashMap<Long, IMessageReceivedListener> mMessageReceivedListenerMap = null;
// 
//     public MessageReceivedListenerManager() {
//         mMessageReceivedListenerMap = new HashMap<>();
//     }
// 
//     public void addTorBundleListener(IMessageReceivedListener messageReceivedListener, long sessionId) {
//         Log.i(LOG, "addTorBundleListener -> ENTER messageReceivedListener=" + messageReceivedListener + ", sessionId=" + sessionId);
// 
//         if (!mMessageReceivedListenerMap.containsKey(sessionId)) {
//             mMessageReceivedListenerMap.put(sessionId, messageReceivedListener);
//         }
// 
//         Log.i(LOG, "addTorBundleListener -> LEAVE");
//     }
// 
//     public void messageReceived(String message, long sessionId) {
//         Log.i(LOG, "messageReceived -> ENTER message=" + message + ", sessionId=" + sessionId);
//         final long messageTimestamp = System.currentTimeMillis();
// 
//         ChatMessage chatMessage = new ChatMessage(message, ChatMessageType.PARTNER, messageTimestamp);
// 
//         IMessageReceivedListener messageReceivedListener = mMessageReceivedListenerMap.get(sessionId);
//         if (messageReceivedListener != null) {
// 
//             DBChatMessageEntity chatMessageEntity = new DBChatMessageEntity();
//             chatMessageEntity.setSessionId(sessionId);
//             chatMessageEntity.setMessage(message);
//             chatMessageEntity.setChatMessageType(chatMessage.getChatMessageType());
//             chatMessageEntity.setTimestamp(messageTimestamp);
//             AppController.getInstanceParameterized(null).addMessageToChatHistory(chatMessageEntity);
// 
//             messageReceivedListener.showReceivedMessage(chatMessage);
//         }
//         Log.i(LOG, "messageReceived -> LEAVE");
//     }
// 
//     public void removeTorBundleListener(long sessionId) {
//         Log.i(LOG, "removeTorBundleListener -> ENTER sessionId=" + sessionId);
// 
//         if (mMessageReceivedListenerMap.containsKey(sessionId)) {
//             mMessageReceivedListenerMap.remove(sessionId);
//         }
// 
//         Log.i(LOG, "removeTorBundleListener -> LEAVE");
//     }
// }
// 
// Path: app/src/main/java/com/batsw/anonimitychat/tor/bundle/TorConstants.java
// public interface TorConstants {
//     // TOR Expert Bundle resources
//     public static final String FOLDER_NAME_FOR_RESOURCES_IN_ASSETS = "tor_bundle";
//     public static final String INTERNAL_RESOURCE_FOLDER_NAME = "/torBundle";
//     public static final String TOR_READY_STATUS_MESSAGE = "Bootstrapped 100%: Done";
//     public static final String TOR_HIDDEN_SERVICE_NAME = "hostname";
// 
//     public static final String TOR_ADDRESS_SUFFIX = ".onion";
// 
//     //Port related constants
//     public static final int TOR_BUNDLE_EXTERNAL_PORT = 80;
//     public static final int TOR_BUNDLE_INTERNAL_SOCKS_PORT = 11158;
//     public static final int TOR_BUNDLE_INTERNAL_HIDDEN_SERVICES_PORT = 44444;
// 
//     //TOR Bundle Status constants
//     public static final String TOR_BUNDLE_STOPPED = "disconnected";
//     public static final String TOR_BUNDLE_IS_STARTING = "connecting";
//     public static final String TOR_BUNDLE_STARTED = "connected";
// }


import android.os.StrictMode;
import android.util.Log;
import com.batsw.anonimitychat.chat.listener.IncomingConnectionListenerManager;
import com.batsw.anonimitychat.chat.message.MessageReceivedListenerManager;
import com.batsw.anonimitychat.tor.bundle.TorConstants;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
package com.batsw.anonimitychat.tor.connections;




/**
 * Created by tudor on 3/13/2017.
 */

public class TorConnectionReceiver {

    private static final String LOG = TorConnectionReceiver.class.getSimpleName();

    private ExecutorService mWaitingForPartnerThread = null;

// Path: app/src/main/java/com/batsw/anonimitychat/chat/listener/IncomingConnectionListenerManager.java
// public class IncomingConnectionListenerManager {
//     private List<IIncomingConnectionListener> mIncomingConnectionListenerList = null;
// 
//     public IncomingConnectionListenerManager() {
//         mIncomingConnectionListenerList = new ArrayList<>();
//     }
// 
//     public void triggerPartnerChatRequest(String partnerHostname) {
//         for (IIncomingConnectionListener torStatusListener : mIncomingConnectionListenerList) {
// 
//             torStatusListener.triggerIncomingPartnerConnectionEvent(partnerHostname);
// 
//         }
//     }
// 
//     public void addIncomingConnectionListener(IIncomingConnectionListener iIncomingConnectionListener) {
//         mIncomingConnectionListenerList.add(iIncomingConnectionListener);
//     }
// 
//     public void removeIncomingConnectionListener(ITorBundleStatusListener iTorBundleStatusListener) {
//         if (mIncomingConnectionListenerList.contains(iTorBundleStatusListener)) {
//             mIncomingConnectionListenerList.remove(iTorBundleStatusListener);
//         }
//     }
// }
// 
// Path: app/src/main/java/com/batsw/anonimitychat/chat/message/MessageReceivedListenerManager.java
// public class MessageReceivedListenerManager {
//     private static final String LOG = MessageReceivedListenerManager.class.getSimpleName();
// 
//     private HashMap<Long, IMessageReceivedListener> mMessageReceivedListenerMap = null;
// 
//     public MessageReceivedListenerManager() {
//         mMessageReceivedListenerMap = new HashMap<>();
//     }
// 
//     public void addTorBundleListener(IMessageReceivedListener messageReceivedListener, long sessionId) {
//         Log.i(LOG, "addTorBundleListener -> ENTER messageReceivedListener=" + messageReceivedListener + ", sessionId=" + sessionId);
// 
//         if (!mMessageReceivedListenerMap.containsKey(sessionId)) {
//             mMessageReceivedListenerMap.put(sessionId, messageReceivedListener);
//         }
// 
//         Log.i(LOG, "addTorBundleListener -> LEAVE");
//     }
// 
//     public void messageReceived(String message, long sessionId) {
//         Log.i(LOG, "messageReceived -> ENTER message=" + message + ", sessionId=" + sessionId);
//         final long messageTimestamp = System.currentTimeMillis();
// 
//         ChatMessage chatMessage = new ChatMessage(message, ChatMessageType.PARTNER, messageTimestamp);
// 
//         IMessageReceivedListener messageReceivedListener = mMessageReceivedListenerMap.get(sessionId);
//         if (messageReceivedListener != null) {
// 
//             DBChatMessageEntity chatMessageEntity = new DBChatMessageEntity();
//             chatMessageEntity.setSessionId(sessionId);
//             chatMessageEntity.setMessage(message);
//             chatMessageEntity.setChatMessageType(chatMessage.getChatMessageType());
//             chatMessageEntity.setTimestamp(messageTimestamp);
//             AppController.getInstanceParameterized(null).addMessageToChatHistory(chatMessageEntity);
// 
//             messageReceivedListener.showReceivedMessage(chatMessage);
//         }
//         Log.i(LOG, "messageReceived -> LEAVE");
//     }
// 
//     public void removeTorBundleListener(long sessionId) {
//         Log.i(LOG, "removeTorBundleListener -> ENTER sessionId=" + sessionId);
// 
//         if (mMessageReceivedListenerMap.containsKey(sessionId)) {
//             mMessageReceivedListenerMap.remove(sessionId);
//         }
// 
//         Log.i(LOG, "removeTorBundleListener -> LEAVE");
//     }
// }
// 
// Path: app/src/main/java/com/batsw/anonimitychat/tor/bundle/TorConstants.java
// public interface TorConstants {
//     // TOR Expert Bundle resources
//     public static final String FOLDER_NAME_FOR_RESOURCES_IN_ASSETS = "tor_bundle";
//     public static final String INTERNAL_RESOURCE_FOLDER_NAME = "/torBundle";
//     public static final String TOR_READY_STATUS_MESSAGE = "Bootstrapped 100%: Done";
//     public static final String TOR_HIDDEN_SERVICE_NAME = "hostname";
// 
//     public static final String TOR_ADDRESS_SUFFIX = ".onion";
// 
//     //Port related constants
//     public static final int TOR_BUNDLE_EXTERNAL_PORT = 80;
//     public static final int TOR_BUNDLE_INTERNAL_SOCKS_PORT = 11158;
//     public static final int TOR_BUNDLE_INTERNAL_HIDDEN_SERVICES_PORT = 44444;
// 
//     //TOR Bundle Status constants
//     public static final String TOR_BUNDLE_STOPPED = "disconnected";
//     public static final String TOR_BUNDLE_IS_STARTING = "connecting";
//     public static final String TOR_BUNDLE_STARTED = "connected";
// }


// Path: app/src/main/java/com/batsw/anonimitychat/tor/connections/TorConnectionReceiver.java
import android.os.StrictMode;
import android.util.Log;
import com.batsw.anonimitychat.chat.listener.IncomingConnectionListenerManager;
import com.batsw.anonimitychat.chat.message.MessageReceivedListenerManager;
import com.batsw.anonimitychat.tor.bundle.TorConstants;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

package com.batsw.anonimitychat.tor.connections;




/**
 * Created by tudor on 3/13/2017.
 */

public class TorConnectionReceiver {

    private static final String LOG = TorConnectionReceiver.class.getSimpleName();

    private ExecutorService mWaitingForPartnerThread = null;

    private IncomingConnectionListenerManager mIncomingConnectionListenerManager;