/*
* Copyright (C) 2014 Graphene
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.quna.rsc.io.socket;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.ParseException;
import java.time.Instant;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.quna.rsc.ActionListener;
import org.quna.rsc.data.User;
import org.quna.rsc.data.message.MessageType;
import org.quna.rsc.data.message.Message;
import org.quna.rsc.io.ReadWriter;
import org.quna.rsc.io.StringByteBufferConverter;
/**
*
* @author owner
*/
public class RSCServerSocket extends RSCBaseServerSocket{
String serverName, welcomeMsg;
final Map <MessageType, ActionListener> listenerMap;
List <String> list = new ArrayList <String> ();
boolean isPrivate = false;
private static final Logger logger = Logger.getLogger (RSCServerSocket.class.getName());
private ResourceBundle languageResource;
public RSCServerSocket (int port) {
super (port, new ReadWriter()) ;
welcomeMsg = "" ;
serverName = "" ;
listenerMap = new HashMap <MessageType, ActionListener> ();
try {
languageResource = ResourceBundle.getBundle ("RSCLanguageResource",
Locale.getDefault ()) ;
} catch (MissingResourceException ex) {
logger.log (Level.WARNING, "The Following Language Is Not Supported: {0}."
+ " Sorry For The Inconvinience.", Locale.getDefault ().getLanguage ());
languageResource = ResourceBundle.getBundle ("RSCLanguageResource",
Locale.US) ;
}
}
public RSCServerSocket (String name, String welcomeMessage, int port, boolean isPrivate) {
this (port) ;
serverName = name;
this.welcomeMsg = welcomeMessage;
this.isPrivate = isPrivate;
}
@Override
void accept (ServerSocketChannel server, Selector selector) throws IOException {
SocketChannel channel = server.accept ();
channel.configureBlocking (false);
int validOps = channel.validOps ();
System.out.println (validOps);
if (! checkEligibility (channel)) {
channel.finishConnect ();
selector.close ();
return ;
}
if (channel.isConnectionPending()){
System.out.println("Client와의 연결 설정을 마무리 중입니다~");
channel.finishConnect();
}
if (welcomeMsg == null || welcomeMsg.isEmpty ()) {
welcomeMsg = languageResource.getString ("defWelcome").replace
("%roomName", serverName) ;
}
Message welcome = new Message (welcomeMsg, Date.from (Instant.now ()),
"root", MessageType.INFO);
rw.write (channel, StringByteBufferConverter.ByteBufferFromString (welcome.toString ()));
channel.register (selector, SelectionKey.OP_READ);
userList.add (new User (channel, false));
}
@Override
void process (Selector selector, ByteBuffer data) {
try {
String string = StringByteBufferConverter.decodeToString (data);
System.out.println (string);
Message message = new Message (string);
ActionListener listener = listenerMap.get (message.getMessageType ());
if (listener != null) {
listener.process (message);
}
} catch (ParseException ex) {
logger.log (Level.SEVERE, null, ex);
}
}
/**
* Checks if the client is eligible.
* @param channel
* @return eligibility of the client.
* @throws IOException
*/
public boolean checkEligibility (SocketChannel channel) throws IOException {
return ! isPrivate ^ list.contains (channel.getRemoteAddress ().toString ());
}
public void addListener (MessageType type, ActionListener listener) {
listenerMap.put (type, listener);
}
public ActionListener getListener (MessageType type) {
return listenerMap.get (type) ;
}
public ActionListener removeListener (MessageType type) {
return listenerMap.remove (type);
}
public void ban (SocketChannel channel) throws IOException {
channel.close ();
list.add (channel.getRemoteAddress ().toString ());
userList.remove (new User(channel, false));
}
}