// Path: src/com/seller/trade/services/ServiceBroker.java
// public final class ServiceBroker {
// 
//     protected final Logger LOG;
//     private final Configuration configuration;
//     private final DHTService dhtService;
//     private final StoreService storeService;
//     private final TemplateService templateService;
// 
//     private final String serverIp;
// 
//     public ServiceBroker(Configuration configuration) {
//         System.out.println("Starting ServiceBroker...");
//         this.configuration = configuration;
//         LOG = Lumberjack.getLogger(this);
//         serverIp = configuration.getString(ConfigurationKeys.ST_SERVER_IP);
//         int tcpPort = configuration.getInt(ConfigurationKeys.ST_SERVER_PORT);
// 
//         dhtService = new DHTServiceImpl(configuration.getBoolean(ConfigurationKeys.ST_USE_LAN_MAPPINGS));
//         if (!configuration.getBoolean(ConfigurationKeys.ST_IS_LOBBY_SERVER)) {
//             System.out.println("Announcing myself, not lobby.");
//             dhtService.announceNode(tcpPort);
//         } else {
//             System.out.println("Not announcing myself, I'm a lobby server....");
//         }
// 
//         storeService = new StoreService(configuration, dhtService);
//         storeService.announceProducts();
// 
//         templateService = new TemplateService(configuration);
//         System.out.println("ServiceBroker started.");
//     }
// 
//     public Configuration getConfiguration() {
//         return configuration;
//     }
// 
//     public DHTService getDhtService() { return dhtService; }
// 
//     public StoreService getStoreService() { return storeService; }
// 
//     public TemplateService getTemplateService() { return templateService; }
// }
// 
// Path: src/com/seller/trade/services/dht/DHTNode.java
// public class DHTNode {
//     public final String ipAddress;
//     public final int port;
// 
//     public DHTNode(String ipAddress, int port) {
//         this.ipAddress = ipAddress;
//         this.port = port;
//     }
// 
//     public final String getIPAddress() {
//         return ipAddress;
//     }
// 
//     /** Returns the http port under which the peer is being announced.
//      * All further HTTP interaction between servers should be done through this
//      * port*/
//     public final int getHttpPort() {
//         return port;
//     }
// }
// 
// Path: src/com/seller/trade/services/dht/DHTService.java
// public interface DHTService {
// 
//     public List<DHTNode> getNodes();
// 
//     public List<DHTNode> getNodes(String keyword);
// 
//     public void announceNode(int httpPort);
// 
//     public void announceKeywords(List<String> keywords, int httpPort);
// }


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import com.seller.trade.services.ServiceBroker;
import com.seller.trade.services.dht.DHTNode;
import com.seller.trade.services.dht.DHTService;
/**
 * The MIT License
 ===============

 Copyright (C) 2015 SellerTrade Developers

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject
 to the following conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.seller.trade.servlets;



/**
 * TODO: Add HTTP redirection to peer.
 * Created by gubatron on 1/10/15.
 * Returns a list of known peers to connect to.
 * It will cache the last known list as fetching peers from the DHT
 * can take time.
 */
public class LobbyServlet extends STAbstractServlet {

    private final DHTService dhtService;

    public LobbyServlet(String command, ServiceBroker broker) {
        super(command, broker);
        dhtService = broker.getDhtService();
    }

    @Override
    protected void handleUncached(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String output;
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

// Path: src/com/seller/trade/services/ServiceBroker.java
// public final class ServiceBroker {
// 
//     protected final Logger LOG;
//     private final Configuration configuration;
//     private final DHTService dhtService;
//     private final StoreService storeService;
//     private final TemplateService templateService;
// 
//     private final String serverIp;
// 
//     public ServiceBroker(Configuration configuration) {
//         System.out.println("Starting ServiceBroker...");
//         this.configuration = configuration;
//         LOG = Lumberjack.getLogger(this);
//         serverIp = configuration.getString(ConfigurationKeys.ST_SERVER_IP);
//         int tcpPort = configuration.getInt(ConfigurationKeys.ST_SERVER_PORT);
// 
//         dhtService = new DHTServiceImpl(configuration.getBoolean(ConfigurationKeys.ST_USE_LAN_MAPPINGS));
//         if (!configuration.getBoolean(ConfigurationKeys.ST_IS_LOBBY_SERVER)) {
//             System.out.println("Announcing myself, not lobby.");
//             dhtService.announceNode(tcpPort);
//         } else {
//             System.out.println("Not announcing myself, I'm a lobby server....");
//         }
// 
//         storeService = new StoreService(configuration, dhtService);
//         storeService.announceProducts();
// 
//         templateService = new TemplateService(configuration);
//         System.out.println("ServiceBroker started.");
//     }
// 
//     public Configuration getConfiguration() {
//         return configuration;
//     }
// 
//     public DHTService getDhtService() { return dhtService; }
// 
//     public StoreService getStoreService() { return storeService; }
// 
//     public TemplateService getTemplateService() { return templateService; }
// }
// 
// Path: src/com/seller/trade/services/dht/DHTNode.java
// public class DHTNode {
//     public final String ipAddress;
//     public final int port;
// 
//     public DHTNode(String ipAddress, int port) {
//         this.ipAddress = ipAddress;
//         this.port = port;
//     }
// 
//     public final String getIPAddress() {
//         return ipAddress;
//     }
// 
//     /** Returns the http port under which the peer is being announced.
//      * All further HTTP interaction between servers should be done through this
//      * port*/
//     public final int getHttpPort() {
//         return port;
//     }
// }
// 
// Path: src/com/seller/trade/services/dht/DHTService.java
// public interface DHTService {
// 
//     public List<DHTNode> getNodes();
// 
//     public List<DHTNode> getNodes(String keyword);
// 
//     public void announceNode(int httpPort);
// 
//     public void announceKeywords(List<String> keywords, int httpPort);
// }


// Path: src/com/seller/trade/servlets/LobbyServlet.java
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import com.seller.trade.services.ServiceBroker;
import com.seller.trade.services.dht.DHTNode;
import com.seller.trade.services.dht.DHTService;

/**
 * The MIT License
 ===============

 Copyright (C) 2015 SellerTrade Developers

 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject
 to the following conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.seller.trade.servlets;



/**
 * TODO: Add HTTP redirection to peer.
 * Created by gubatron on 1/10/15.
 * Returns a list of known peers to connect to.
 * It will cache the last known list as fetching peers from the DHT
 * can take time.
 */
public class LobbyServlet extends STAbstractServlet {

    private final DHTService dhtService;

    public LobbyServlet(String command, ServiceBroker broker) {
        super(command, broker);
        dhtService = broker.getDhtService();
    }

    @Override
    protected void handleUncached(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String output;
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);

        final List<DHTNode> nodes = null;// dhtService.getNodes();