/**
* <strong>启动RServer连接</strong>
* <p>
*
* </p>
*
* @author: 汉娱网络.技术部.yangxf
* @date: 2008-3-3
* @version: 1.1
**/
package com.handjoys.socket;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.net.Socket;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import com.handjoys.conf.ConfigReader;
import com.handjoys.conf.ConfigParam;
import com.handjoys.logger.FileLogger;
import com.handjoys.Broadcaster;
import com.handjoys.GameServer;
public class RServerFactory {
private static Map<String, GSession> rserver=new HashMap<String, GSession>();
private static GameServer gs_instance;
private RServerFactory() {
}
public static boolean init(GameServer gs) {
gs_instance=gs;
FileLogger.info("\n\n--- [ RServer Starting ] ---\n");
Map rserverM=(HashMap)ConfigReader.getParam(ConfigParam.RSERVER);
String hostIp=(String)ConfigReader.getParam(ConfigParam.SERVERIP);
int hostPort=((Integer)(ConfigReader.getParam(ConfigParam.SERVERPORT))).intValue();
String hostName=(String)ConfigReader.getParam(ConfigParam.SERVERNAME);
int index=0;
for(Iterator ir=rserverM.keySet().iterator(); ir.hasNext(); ) {
String name=(String)ir.next();
String address=(String)rserverM.get(name);
String s[]=address.split(":");
String ip=s[0];
int port=Integer.parseInt(s[1]);
// 如果是本地地址, 则忽略
if(ip.equals(hostIp) && (port == hostPort))
continue;
index++;
try {
InetSocketAddress isa=new InetSocketAddress(ip, port);
SocketChannel sc=SocketChannel.open(isa);
// 发送session验证报文
String sessionID=hostName + "#" + index + "#" + hostIp + ":" + hostPort;
GSession gSession=new GSession(sessionID, sc);
String msg="<msg t='sys' s='" + sessionID + "'><body action='50000.20000' r='1'></body></msg>";
gs_instance.initRServer(msg, gSession);
rserver.put(name, gSession);
}catch(Exception e) {
//e.printStackTrace();
FileLogger.info("RServer " + address + " has been connect failed......");
continue;
}
FileLogger.info("RServer " + address + " has been connect success......");
}
return true;
}
public static GSession getRServer(String serverName) {
return (GSession)rserver.get(serverName);
}
public static boolean addRServer(String serverName, GSession session) {
Map rserverM=(HashMap)ConfigReader.getParam(ConfigParam.RSERVER);
if(rserverM.get(serverName) == null)
return false;
rserver.put(serverName, session);
return true;
}
public static boolean removeRServer(SocketChannel sc) {
for(Iterator ir=rserver.keySet().iterator(); ir.hasNext(); ) {
String name=(String)ir.next();
GSession gSession=(GSession)rserver.get(name);
if(sc == gSession.channel) {
ir.remove();
}
}
return true;
}
}