/******************************************************************************* * Copyright (c) 2013 * * This File is part of NGECore2. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see . * * Using NGEngine to work with NGECore2 is making a combined work based on NGEngine. * Therefore all terms and conditions of the GNU Lesser General Public License cover the combination. ******************************************************************************/ package services; import java.nio.ByteOrder; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Map; import main.NGECore; import org.apache.mina.core.buffer.IoBuffer; import org.apache.mina.core.session.IoSession; import protocol.swg.AccountFeatureBits; import protocol.swg.ClientIdMsg; import protocol.swg.ClientPermissionsMessage; import protocol.swg.ConnectionServerLagResponse; import protocol.swg.GalaxyLoopTimesResponse; import protocol.swg.GameServerLagResponse; import protocol.swg.HeartBeatMessage; import engine.clients.Client; import engine.resources.database.DatabaseConnection; import engine.resources.service.INetworkDispatch; import engine.resources.service.INetworkRemoteEvent; import resources.common.*; @SuppressWarnings("unused") public class ConnectionService implements INetworkDispatch { private NGECore core; private DatabaseConnection databaseConnection; private DatabaseConnection databaseConnection2; public ConnectionService(NGECore core) { this.core = core; this.databaseConnection = core.getDatabase1(); this.databaseConnection2 = core.getDatabase2(); } @Override public void insertOpcodes(Map swgOpcodes, Map objControllerOpcodes) { swgOpcodes.put(Opcodes.ClientIdMsg, new INetworkRemoteEvent() { @Override public void handlePacket(IoSession session, IoBuffer data) throws Exception { System.out.println("xx"); data = data.order(ByteOrder.LITTLE_ENDIAN); ClientIdMsg clientIdMsg = new ClientIdMsg(); data.position(0); clientIdMsg.deserialize(data); Client client = core.getClient((Integer) session.getAttribute("connectionId")); if(client == null) { System.out.println("NULL Client"); return; } client.setSession(session); ResultSet resultSet; PreparedStatement preparedStatement; try { preparedStatement = databaseConnection.preparedStatement("SELECT * FROM sessions WHERE key=?"); preparedStatement.setBytes(1, clientIdMsg.getSessionKey()); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { client.setAccountId(resultSet.getLong("accountId")); client.setSessionKey(clientIdMsg.getSessionKey()); client.setGM(core.loginService.checkForGmPermission((int) resultSet.getLong("accountId"))); AccountFeatureBits accountFeatureBits = new AccountFeatureBits(); ClientPermissionsMessage clientPermissionsMessage = new ClientPermissionsMessage(); session.write(new HeartBeatMessage().serialize()); session.write(accountFeatureBits.serialize()); session.write(clientPermissionsMessage.serialize()); preparedStatement.close(); System.out.println("zyx"); } else { System.out.println("Cant get login session"); } } catch (SQLException e) { e.printStackTrace(); } } }); swgOpcodes.put(Opcodes.RequestGalaxyLoopTimes, new INetworkRemoteEvent() { @Override public void handlePacket(IoSession session, IoBuffer data) throws Exception { GalaxyLoopTimesResponse response = new GalaxyLoopTimesResponse(0); session.write(response.serialize()); } }); swgOpcodes.put(Opcodes.LagRequest, new INetworkRemoteEvent() { @Override public void handlePacket(IoSession session, IoBuffer data) throws Exception { ConnectionServerLagResponse connectionReponse = new ConnectionServerLagResponse(); session.write(connectionReponse.serialize()); GameServerLagResponse gameReponse = new GameServerLagResponse(); session.write(gameReponse.serialize()); } }); } @Override public void shutdown() { // TODO Auto-generated method stub } }