Advertisement
Guest User

Untitled

a guest
May 20th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. package btserver;
  2.  
  3. import btUtils.MiscFunc;
  4.  
  5. import java.nio.*;
  6.  
  7. import java.io.*;
  8.  
  9. import java.util.*;
  10.  
  11. import java.sql.*;
  12.  
  13. import org.xsocket.stream.*;
  14.  
  15. import org.apache.log4j.Logger;
  16.  
  17. public class BTServer implements IConnectHandler, IDataHandler, IDisconnectHandler
  18.  
  19. {
  20.  
  21. static Logger logger = Logger.getLogger(BTServer.class.getName());
  22.  
  23. private UserManager userMgr = null;
  24.  
  25. public BTServer()
  26.  
  27. {
  28.  
  29. logger.debug("[Init start ...]");
  30.  
  31. userMgr = UserManager.getInstance();
  32.  
  33. logger.debug("[Init success]");
  34.  
  35. }
  36.  
  37. public boolean onData(INonBlockingConnection connection) throws IOException
  38.  
  39. {
  40.  
  41. NetUser cur_gmr = userMgr.findByConnection(connection);
  42.  
  43. if(cur_gmr == null)
  44.  
  45. {
  46.  
  47. cur_gmr = new NetUser(connection, false);
  48.  
  49. logger.debug("[New connection: IP = " + connection.getRemoteAddress().getHostAddress() + "; Address = " +
  50.  
  51. connection.getRemoteAddress().getHostName() + ";]");
  52.  
  53. }
  54.  
  55. ByteBuffer[] bbuf = connection.readAvailable();
  56.  
  57. int index = 0;
  58.  
  59. byte[] buf = null;
  60.  
  61. for(int i=0; i < bbuf.length; i++)
  62.  
  63. {
  64.  
  65. buf = new byte[bbuf[i].capacity()];
  66.  
  67. for(int k=0; k < buf.length; k++)
  68.  
  69. buf[k] = bbuf[i].get();
  70.  
  71. if(cur_gmr.last_msg == null)
  72.  
  73. cur_gmr.last_msg = new NetMessage();
  74.  
  75. while( (index = cur_gmr.last_msg.createFromBuffer(buf,index)) > 0)
  76.  
  77. {
  78.  
  79. if(cur_gmr.last_msg.complete)
  80.  
  81. ProcessMsg(cur_gmr);
  82.  
  83. if(cur_gmr.last_msg == null)
  84.  
  85. cur_gmr.last_msg = new NetMessage();
  86.  
  87. }
  88.  
  89. if(cur_gmr.last_msg != null)
  90.  
  91. {
  92.  
  93. ProcessMsg(cur_gmr);
  94.  
  95. }
  96.  
  97. buf = null;
  98.  
  99. }
  100.  
  101. return true;
  102.  
  103. }
  104.  
  105. private void ProcessMsg(NetUser usr)
  106.  
  107. {
  108.  
  109. NetMessage nm = null;
  110.  
  111. Random rnd = null;
  112.  
  113. List<Object> params = null;
  114.  
  115. boolean status = false;
  116.  
  117. logger.debug("[" + usr.last_msg.type + " from " + usr.userName + "(" + usr.ID + ")]");
  118.  
  119. usr.bytes_send += usr.last_msg.body.length;
  120.  
  121. switch(usr.last_msg.type)
  122.  
  123. {
  124.  
  125. case MSGC_LOGIN:
  126.  
  127. status = userMgr.clientLogin(usr, usr.last_msg.params);
  128.  
  129. break;
  130.  
  131. case MSGC_PING:
  132.  
  133. int userID = (Integer)usr.last_msg.params.get(0);
  134.  
  135. String key = (String)usr.last_msg.params.get(1);
  136.  
  137. usr.lastPingTime = MiscFunc.getSeconds();
  138.  
  139. if(userID != usr.ID && !key.equals(usr.currentKey))
  140.  
  141. {
  142.  
  143. logger.info("Client info is wrong!; Removing client from user list");
  144.  
  145. userMgr.removeClient(usr);
  146.  
  147. try
  148.  
  149. {
  150.  
  151. usr.connection.close();
  152.  
  153. }
  154.  
  155. catch (IOException ex)
  156.  
  157. {
  158.  
  159. ex.printStackTrace();
  160.  
  161. }
  162.  
  163. }
  164.  
  165. break;
  166.  
  167. default:
  168.  
  169. break;
  170.  
  171. }
  172.  
  173. usr.last_msg.params.clear();
  174.  
  175. usr.last_msg = null;
  176.  
  177. nm = null;
  178.  
  179. }
  180.  
  181. public boolean onConnect(INonBlockingConnection connection) throws IOException
  182.  
  183. {
  184.  
  185. return true;
  186.  
  187. }
  188.  
  189. public boolean onDisconnect(INonBlockingConnection connection) throws IOException
  190.  
  191. {
  192.  
  193. NetUser player = userMgr.findByConnection(connection);
  194.  
  195. if(player != null)
  196.  
  197. {
  198.  
  199. logger.info("[Client " + player.userName + "(" + player.ID + ") disconnected]");
  200.  
  201. userMgr.removeClient(player);
  202.  
  203. }
  204.  
  205. else
  206.  
  207. {
  208.  
  209. logger.warn("[Unknown client disconnected]");
  210.  
  211. }
  212.  
  213. return true;
  214.  
  215. }
  216.  
  217. }
  218.  
  219. Клиент :
  220.  
  221. package Main;
  222.  
  223. import javax.microedition.midlet.*;
  224.  
  225. import javax.microedition.lcdui.*;
  226.  
  227. public class BTMidlet extends MIDlet implements CommandListener{
  228.  
  229. Display display = null;
  230.  
  231. BTApp game = null;
  232.  
  233. Form form = null;
  234.  
  235. TextField textField = null;
  236.  
  237. int fieldType;
  238.  
  239. public BTMidlet(){
  240.  
  241. super();
  242.  
  243. display = Display.getDisplay(this);
  244.  
  245. game = new BTApp(this);
  246.  
  247. display.setCurrent(game);
  248.  
  249. game.start();
  250.  
  251. }
  252.  
  253. public void startApp() {
  254.  
  255. }
  256.  
  257. public void pauseApp() {
  258.  
  259. }
  260.  
  261. public void destroyApp(boolean unconditional) {
  262.  
  263. notifyDestroyed();
  264.  
  265. }
  266.  
  267. public void quit(){
  268.  
  269. game.stop();
  270.  
  271. notifyDestroyed();
  272.  
  273. }
  274.  
  275. public void commandAction(Command command, Displayable displayable)
  276.  
  277. {
  278.  
  279. }
  280.  
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement