// Path: src/main/java/com/simsilica/net/ChatSession.java
// public interface ChatSession {
// 
//     /**
//      *  Sends a new message to the chat.
//      */
//     @Asynchronous
//     public void sendMessage( String message );
// 
//     /**
//      *  Returns the list of players currently in the chat.
//      */
//     public List<String> getPlayerNames();        
// }
// 
// Path: src/main/java/com/simsilica/net/ChatSessionListener.java
// public interface ChatSessionListener {
//     
//     /**
//      *  Called when a new player has joined the chat.
//      */
//     @Asynchronous   
//     public void playerJoined( int clientId, String playerName );
//  
//     /**
//      *  Called when a player has sent a message to the chat.
//      */
//     @Asynchronous   
//     public void newMessage( int clientId, String playerName, String message );
//     
//     /**
//      *  Called when an existing player has left the chat.
//      */   
//     @Asynchronous   
//     public void playerLeft( int clientId, String playerName );
//     
// }


import java.util.*;
import java.util.concurrent.*;
import org.slf4j.*;
import com.jme3.network.HostedConnection;
import com.jme3.network.MessageConnection;
import com.jme3.network.Server;
import com.jme3.network.service.AbstractHostedConnectionService;
import com.jme3.network.service.HostedServiceManager;
import com.jme3.network.service.rmi.RmiHostedService;
import com.jme3.network.service.rmi.RmiRegistry;
import com.simsilica.net.ChatSession;
import com.simsilica.net.ChatSessionListener;

/*
 * $Id$
 * 
 * Copyright (c) 2016, Simsilica, LLC
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in 
 *    the documentation and/or other materials provided with the 
 *    distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its 
 *    contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.simsilica.net.server;





/**
 *  HostedService providing a chat server for connected players.  Some
 *  time during player connection setup, the game must start hosting
 *  and provide the player name in order for the client to participate.
 *
 *  @author    Paul Speed
 */
public class ChatHostedService extends AbstractHostedConnectionService {

    static Logger log = LoggerFactory.getLogger(ChatHostedService.class);

    private static final String ATTRIBUTE_SESSION = ChatHostedService.class.getName();

    private RmiHostedService rmiService;
    private int channel;

    private List<ChatSessionImpl> players = new CopyOnWriteArrayList<>();
 
    /**
     *  Creates a new chat service that will use the default reliable channel
     *  for reliable communication.
     */
    public ChatHostedService() {
        this(MessageConnection.CHANNEL_DEFAULT_RELIABLE);
    }
    
    /**
     *  Creates a new chat service that will use the specified channel
     *  for reliable communication.
     */
    public ChatHostedService( int channel ) {
        this.channel = channel;
        setAutoHost(false);
    }
 
    protected ChatSessionImpl getChatSession( HostedConnection conn ) {
        return conn.getAttribute(ATTRIBUTE_SESSION);   
    }
 
    @Override
    protected void onInitialize( HostedServiceManager s ) {
        
        // Grab the RMI service so we can easily use it later        
        this.rmiService = getService(RmiHostedService.class);
        if( rmiService == null ) {
            throw new RuntimeException("ChatHostedService requires an RMI service.");
        }
    }
    
    /**
     *  Starts hosting the chat services on the specified connection using
     *  a specified player name.  This causes the player to 'enter' the chat
     *  room and will then be able to send/receive messages.
     */
    public void startHostingOnConnection( HostedConnection conn, String playerName ) {
        log.debug("startHostingOnConnection(" + conn + ")");
    
        ChatSessionImpl session = new ChatSessionImpl(conn, playerName);
        conn.setAttribute(ATTRIBUTE_SESSION, session);
        
        // Expose the session as an RMI resource to the client
        RmiRegistry rmi = rmiService.getRmiRegistry(conn);

// Path: src/main/java/com/simsilica/net/ChatSession.java
// public interface ChatSession {
// 
//     /**
//      *  Sends a new message to the chat.
//      */
//     @Asynchronous
//     public void sendMessage( String message );
// 
//     /**
//      *  Returns the list of players currently in the chat.
//      */
//     public List<String> getPlayerNames();        
// }
// 
// Path: src/main/java/com/simsilica/net/ChatSessionListener.java
// public interface ChatSessionListener {
//     
//     /**
//      *  Called when a new player has joined the chat.
//      */
//     @Asynchronous   
//     public void playerJoined( int clientId, String playerName );
//  
//     /**
//      *  Called when a player has sent a message to the chat.
//      */
//     @Asynchronous   
//     public void newMessage( int clientId, String playerName, String message );
//     
//     /**
//      *  Called when an existing player has left the chat.
//      */   
//     @Asynchronous   
//     public void playerLeft( int clientId, String playerName );
//     
// }


// Path: src/main/java/com/simsilica/net/server/ChatHostedService.java
import java.util.*;
import java.util.concurrent.*;
import org.slf4j.*;
import com.jme3.network.HostedConnection;
import com.jme3.network.MessageConnection;
import com.jme3.network.Server;
import com.jme3.network.service.AbstractHostedConnectionService;
import com.jme3.network.service.HostedServiceManager;
import com.jme3.network.service.rmi.RmiHostedService;
import com.jme3.network.service.rmi.RmiRegistry;
import com.simsilica.net.ChatSession;
import com.simsilica.net.ChatSessionListener;

/*
 * $Id$
 * 
 * Copyright (c) 2016, Simsilica, LLC
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in 
 *    the documentation and/or other materials provided with the 
 *    distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its 
 *    contributors may be used to endorse or promote products derived 
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.simsilica.net.server;





/**
 *  HostedService providing a chat server for connected players.  Some
 *  time during player connection setup, the game must start hosting
 *  and provide the player name in order for the client to participate.
 *
 *  @author    Paul Speed
 */
public class ChatHostedService extends AbstractHostedConnectionService {

    static Logger log = LoggerFactory.getLogger(ChatHostedService.class);

    private static final String ATTRIBUTE_SESSION = ChatHostedService.class.getName();

    private RmiHostedService rmiService;
    private int channel;

    private List<ChatSessionImpl> players = new CopyOnWriteArrayList<>();
 
    /**
     *  Creates a new chat service that will use the default reliable channel
     *  for reliable communication.
     */
    public ChatHostedService() {
        this(MessageConnection.CHANNEL_DEFAULT_RELIABLE);
    }
    
    /**
     *  Creates a new chat service that will use the specified channel
     *  for reliable communication.
     */
    public ChatHostedService( int channel ) {
        this.channel = channel;
        setAutoHost(false);
    }
 
    protected ChatSessionImpl getChatSession( HostedConnection conn ) {
        return conn.getAttribute(ATTRIBUTE_SESSION);   
    }
 
    @Override
    protected void onInitialize( HostedServiceManager s ) {
        
        // Grab the RMI service so we can easily use it later        
        this.rmiService = getService(RmiHostedService.class);
        if( rmiService == null ) {
            throw new RuntimeException("ChatHostedService requires an RMI service.");
        }
    }
    
    /**
     *  Starts hosting the chat services on the specified connection using
     *  a specified player name.  This causes the player to 'enter' the chat
     *  room and will then be able to send/receive messages.
     */
    public void startHostingOnConnection( HostedConnection conn, String playerName ) {
        log.debug("startHostingOnConnection(" + conn + ")");
    
        ChatSessionImpl session = new ChatSessionImpl(conn, playerName);
        conn.setAttribute(ATTRIBUTE_SESSION, session);
        
        // Expose the session as an RMI resource to the client
        RmiRegistry rmi = rmiService.getRmiRegistry(conn);

        rmi.share((byte)channel, session, ChatSession.class);
