Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package btserver;
- import btUtils.MiscFunc;
- import java.nio.*;
- import java.io.*;
- import java.util.*;
- import java.sql.*;
- import org.xsocket.stream.*;
- import org.apache.log4j.Logger;
- public class BTServer implements IConnectHandler, IDataHandler, IDisconnectHandler
- {
- static Logger logger = Logger.getLogger(BTServer.class.getName());
- private UserManager userMgr = null;
- public BTServer()
- {
- logger.debug("[Init start ...]");
- userMgr = UserManager.getInstance();
- logger.debug("[Init success]");
- }
- public boolean onData(INonBlockingConnection connection) throws IOException
- {
- NetUser cur_gmr = userMgr.findByConnection(connection);
- if(cur_gmr == null)
- {
- cur_gmr = new NetUser(connection, false);
- logger.debug("[New connection: IP = " + connection.getRemoteAddress().getHostAddress() + "; Address = " +
- connection.getRemoteAddress().getHostName() + ";]");
- }
- ByteBuffer[] bbuf = connection.readAvailable();
- int index = 0;
- byte[] buf = null;
- for(int i=0; i < bbuf.length; i++)
- {
- buf = new byte[bbuf[i].capacity()];
- for(int k=0; k < buf.length; k++)
- buf[k] = bbuf[i].get();
- if(cur_gmr.last_msg == null)
- cur_gmr.last_msg = new NetMessage();
- while( (index = cur_gmr.last_msg.createFromBuffer(buf,index)) > 0)
- {
- if(cur_gmr.last_msg.complete)
- ProcessMsg(cur_gmr);
- if(cur_gmr.last_msg == null)
- cur_gmr.last_msg = new NetMessage();
- }
- if(cur_gmr.last_msg != null)
- {
- ProcessMsg(cur_gmr);
- }
- buf = null;
- }
- return true;
- }
- private void ProcessMsg(NetUser usr)
- {
- NetMessage nm = null;
- Random rnd = null;
- List<Object> params = null;
- boolean status = false;
- logger.debug("[" + usr.last_msg.type + " from " + usr.userName + "(" + usr.ID + ")]");
- usr.bytes_send += usr.last_msg.body.length;
- switch(usr.last_msg.type)
- {
- case MSGC_LOGIN:
- status = userMgr.clientLogin(usr, usr.last_msg.params);
- break;
- case MSGC_PING:
- int userID = (Integer)usr.last_msg.params.get(0);
- String key = (String)usr.last_msg.params.get(1);
- usr.lastPingTime = MiscFunc.getSeconds();
- if(userID != usr.ID && !key.equals(usr.currentKey))
- {
- logger.info("Client info is wrong!; Removing client from user list");
- userMgr.removeClient(usr);
- try
- {
- usr.connection.close();
- }
- catch (IOException ex)
- {
- ex.printStackTrace();
- }
- }
- break;
- default:
- break;
- }
- usr.last_msg.params.clear();
- usr.last_msg = null;
- nm = null;
- }
- public boolean onConnect(INonBlockingConnection connection) throws IOException
- {
- return true;
- }
- public boolean onDisconnect(INonBlockingConnection connection) throws IOException
- {
- NetUser player = userMgr.findByConnection(connection);
- if(player != null)
- {
- logger.info("[Client " + player.userName + "(" + player.ID + ") disconnected]");
- userMgr.removeClient(player);
- }
- else
- {
- logger.warn("[Unknown client disconnected]");
- }
- return true;
- }
- }
- Клиент :
- package Main;
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- public class BTMidlet extends MIDlet implements CommandListener{
- Display display = null;
- BTApp game = null;
- Form form = null;
- TextField textField = null;
- int fieldType;
- public BTMidlet(){
- super();
- display = Display.getDisplay(this);
- game = new BTApp(this);
- display.setCurrent(game);
- game.start();
- }
- public void startApp() {
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- notifyDestroyed();
- }
- public void quit(){
- game.stop();
- notifyDestroyed();
- }
- public void commandAction(Command command, Displayable displayable)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement