PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Android/MobiVuold/MobiVU_testudp/src/com/mobivu/NetworkEngine.java

https://github.com/AnthonyNystrom/MobiVU
Java | 374 lines | 204 code | 36 blank | 134 comment | 10 complexity | 649cb63d49a79b373ce916bb6c69a46c MD5 | raw file
  1. package com.mobivu;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10. import java.util.*;
  11. import java.net.DatagramPacket;
  12. import java.net.DatagramSocket;
  13. import java.net.InetAddress;
  14. import java.net.SocketException;
  15. import android.os.Looper;
  16. import android.os.SystemClock;
  17. import android.util.Log;
  18. public class NetworkEngine implements Runnable {
  19. private String _sIP;
  20. private Socket _sc;
  21. private OutputStream _out;
  22. private InputStream _insc;
  23. private Thread _th=null;
  24. private boolean _bLoop;
  25. private int _iEngineStatus;
  26. private byte []_byBufRX;
  27. private byte []_byBufRX2;
  28. private byte []_byBuf2Send;
  29. private int _iLen2Send;
  30. private int _cmd;
  31. private DatagramSocket _socket;
  32. private DatagramPacket _datagram;
  33. private InetAddress _addr;
  34. private int _icnttest=0;
  35. FileOutputStream _inputread, _inputread1;
  36. int _iSizeRx2;
  37. private static final int ENGINE_NET_STATUS_DISCONNECTED =0;
  38. private static final int ENGINE_NET_STATUS_CONNECTED =1;
  39. private static final int ENGINE_NET_STATUS_TX_CMD =2;
  40. private static final int ENGINE_NET_STATUS_RX_CMD =3;
  41. public static final int CMD_NONE = -1;
  42. public static final int CMD_LOGIN = 0;
  43. public static final int CMD_LOGOUT = 1;
  44. public static final int CMD_CALL = 2;
  45. public static final int CMD_FRM_VIDEO = 3;
  46. public static final int CMD_FRM_AUDIO = 4;
  47. public static final int CMD_USERS_LIST = 5;
  48. public static final int CMD_RING = 6;
  49. public static final int CMD_CLOSE_CONV = 7;
  50. public static final int CMD_RECORDER = 11;
  51. public static final int BAD_RESULT = 0;
  52. public static final int GOOD_RESULT = 1;
  53. private EventNetwork _eventNetwork;
  54. NetworkEngine(EventNetwork eventNetwork){
  55. _eventNetwork = eventNetwork;
  56. }
  57. void Init(String sIP){
  58. _sIP = sIP;
  59. _byBuf2Send = new byte[20000];
  60. _byBufRX = new byte[20000];
  61. _byBufRX2 = new byte[20000];
  62. _iSizeRx2 = 0;
  63. _iLen2Send = 0;
  64. _bLoop = true;
  65. _cmd = CMD_NONE;
  66. _iEngineStatus = ENGINE_NET_STATUS_DISCONNECTED;
  67. try {
  68. _socket = new DatagramSocket();
  69. _socket.setSoTimeout(2000);
  70. } catch (SocketException e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. }
  74. _datagram = new DatagramPacket(new byte[1],1);
  75. try {
  76. _addr = InetAddress.getByName(sIP);
  77. } catch (UnknownHostException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }
  81. // prepare file to write amr
  82. File fo = new File("/sdcard/input.dat");
  83. if (fo.exists())
  84. fo.delete();
  85. File fo1 = new File("/sdcard/input1.dat");
  86. if (fo1.exists())
  87. fo1.delete();
  88. try {
  89. _inputread = new FileOutputStream(fo);
  90. _inputread1= new FileOutputStream(fo1);
  91. } catch (FileNotFoundException e) {
  92. // TODO Auto-generated catch block
  93. e.printStackTrace();
  94. }
  95. _th = new Thread(this);
  96. _th.start();
  97. }
  98. public void Release(){
  99. try {
  100. _inputread .close();
  101. _inputread1.close();
  102. } catch (IOException e1) {
  103. // TODO Auto-generated catch block
  104. e1.printStackTrace();
  105. }
  106. _bLoop=false;
  107. try {
  108. Thread.sleep(500);
  109. } catch (InterruptedException e) {
  110. // TODO Auto-generated catch block
  111. e.printStackTrace();
  112. }
  113. _th.stop();
  114. _byBufRX = null;
  115. _byBuf2Send = null;
  116. }
  117. public void run() {
  118. Looper.prepare();
  119. int iRead, iSizePack;
  120. int iCmdRx;
  121. boolean bReadDataGood=false;
  122. while (_bLoop){
  123. switch (_iEngineStatus){
  124. case ENGINE_NET_STATUS_DISCONNECTED:
  125. if (!ConnectSocket())
  126. CloseSocket();
  127. break;
  128. case ENGINE_NET_STATUS_CONNECTED:
  129. break;
  130. case ENGINE_NET_STATUS_TX_CMD:
  131. try {
  132. WriteData(_byBuf2Send, 0, _iLen2Send);
  133. _iEngineStatus++;
  134. } catch (IOException e) {
  135. Log.i("NetworkEngine::ENGINE_NET_STATUS_TX_CMD", "error transmission");
  136. e.printStackTrace();
  137. CloseSocket();
  138. _iEngineStatus = ENGINE_NET_STATUS_DISCONNECTED;
  139. }
  140. break;
  141. case ENGINE_NET_STATUS_RX_CMD:
  142. iRead =0;
  143. try {
  144. /*iRead = ReadData(_byBufRX, 0, 3);
  145. iSizePack = _byBufRX[1] & 0x000000FF;
  146. iSizePack <<= 8;
  147. iSizePack |= (_byBufRX[0] & 0x000000FF);
  148. //Engine.AddLog("iRead:"+iRead+"iSizePack:"+iSizePack);
  149. if (iSizePack>0)
  150. { iRead = ReadData(_byBufRX, 3, iSizePack-1);
  151. //Engine.AddLog("iRead:"+iRead);
  152. //_eventNetwork.CmdResult(CMD_FRM_AUDIO, _byBufRX, iSizePack-1, GOOD_RESULT);
  153. }*/
  154. iRead = ReadData(_byBufRX, 0, _byBufRX.length);
  155. if (iRead>0)
  156. _eventNetwork.CmdResult(CMD_FRM_VIDEO, _byBufRX, iRead, GOOD_RESULT);
  157. } catch (IOException e) {
  158. // TODO Auto-generated catch block
  159. e.printStackTrace();
  160. }
  161. break;/*
  162. long l1 = SystemClock.uptimeMillis();
  163. iRead=0;
  164. try {
  165. // read header
  166. iRead = ReadData(_byBufRX, 0, 3);
  167. if (iRead>0)
  168. _icnttest++;
  169. if (iRead==3){
  170. //iSizePack = _byBufRX[1] & 0x0000001F;
  171. iSizePack = _byBufRX[1] & 0x000000FF;
  172. iSizePack <<= 8;
  173. iSizePack |= (_byBufRX[0] & 0x000000FF);
  174. iCmdRx =_byBufRX[2];
  175. if (iSizePack >_byBufRX.length)
  176. Engine.AddLog("Size Packet too big");
  177. else{
  178. _byBufRX[0]=(byte) 0xFF;
  179. iRead=ReadData(_byBufRX, 3, iSizePack-1);
  180. Engine.AddLog("iSizePack:"+iSizePack+ " Counter:"+_icnttest+"Read:"+iRead);
  181. bReadDataGood = true;
  182. switch (_cmd){
  183. case CMD_USERS_LIST:
  184. case CMD_LOGOUT:
  185. case CMD_LOGIN:
  186. if ((_cmd==iCmdRx || iCmdRx==CMD_CLOSE_CONV) && iRead==iSizePack-1){
  187. _eventNetwork.CmdResult(iCmdRx, _byBufRX, iSizePack, GOOD_RESULT);
  188. }
  189. else{
  190. Engine.AddLog("doesn't match cmd");
  191. _eventNetwork.CmdResult(_cmd, _byBufRX, iSizePack, BAD_RESULT);
  192. }
  193. _cmd = CMD_NONE;
  194. break;
  195. default:
  196. if (_cmd!=CMD_NONE && iCmdRx!=CMD_RING && iCmdRx!=CMD_FRM_VIDEO && iCmdRx!=CMD_FRM_AUDIO && iCmdRx!=CMD_CLOSE_CONV && iCmdRx!=CMD_CALL){
  197. Engine.AddLog("BAD RESULT1");
  198. _eventNetwork.CmdResult(_cmd, _byBufRX, 0, BAD_RESULT);
  199. _cmd=CMD_NONE;
  200. }
  201. else if ((iCmdRx==CMD_RING || iCmdRx==CMD_FRM_VIDEO || iCmdRx==CMD_FRM_AUDIO || iCmdRx==CMD_CLOSE_CONV || iCmdRx==CMD_CALL) && iRead==iSizePack-1){
  202. _eventNetwork.CmdResult(iCmdRx, _byBufRX, iSizePack-1, GOOD_RESULT);
  203. }
  204. else{
  205. Engine.AddLog("BAD RESULT2");
  206. _eventNetwork.CmdResult(_cmd, _byBufRX, 0, BAD_RESULT);
  207. _cmd=CMD_NONE;
  208. }
  209. }
  210. }
  211. }
  212. else if (iRead>0)
  213. {
  214. Engine.AddLog("DATA LESS OF 3:"+iRead);
  215. }
  216. } catch (IOException e) {
  217. e.printStackTrace();
  218. Engine.AddLog("exception to read socket");
  219. if (_cmd!=CMD_NONE){
  220. _eventNetwork.CmdResult(_cmd, _byBufRX, 0, BAD_RESULT);
  221. _cmd=CMD_NONE;
  222. }
  223. }
  224. if (!bReadDataGood && iRead>0){
  225. Engine.AddLog("!bReadDataGood && iRead>0");
  226. CloseSocket();
  227. _iEngineStatus = ENGINE_NET_STATUS_DISCONNECTED;
  228. }
  229. Engine.AddLog("Time ReadData:"+(SystemClock.uptimeMillis()-l1));
  230. break;*/
  231. }
  232. }
  233. }
  234. private void CloseSocket(){
  235. /*if (_out!=null){
  236. try{
  237. _out.close();
  238. _sc.close();
  239. _sc = null;
  240. _out = null;
  241. _insc = null;
  242. }
  243. catch(IOException ex){
  244. }
  245. _eventNetwork.Connected(false);
  246. }*/
  247. _socket.disconnect();
  248. }
  249. private boolean ConnectSocket(){
  250. /*boolean bret=false;
  251. try{
  252. _sc = new Socket(_sIP, 8082);
  253. _sc.setSoTimeout(5000);
  254. _out = _sc.getOutputStream();
  255. _insc = _sc.getInputStream();
  256. bret=true;
  257. _iEngineStatus = ENGINE_NET_STATUS_CONNECTED;
  258. _eventNetwork.Connected(true);
  259. }
  260. catch(IOException e){
  261. e.printStackTrace();
  262. _eventNetwork.Connected(false);
  263. }*/
  264. _socket.connect(_addr, 9050);
  265. boolean bret=_socket.isConnected();
  266. _eventNetwork.Connected(bret);
  267. _iEngineStatus = ENGINE_NET_STATUS_CONNECTED;
  268. return bret;
  269. }
  270. public void SendCommand(int iCmd, byte []byData, int iSizeData){
  271. if (iCmd==CMD_FRM_AUDIO || iCmd==CMD_FRM_VIDEO){
  272. if (byData!=null){
  273. System.arraycopy(byData, 0, _byBuf2Send, 3, iSizeData);
  274. SetCheckSum(_byBuf2Send, iSizeData+1, (byte)iCmd);
  275. _iLen2Send = iSizeData+3;
  276. } else{
  277. SetCheckSum(_byBuf2Send, 1, (byte)iCmd);
  278. _iLen2Send = 3;
  279. }
  280. try {
  281. WriteData(_byBuf2Send, 0, _iLen2Send);
  282. } catch (IOException e) {
  283. Log.i("SendCommand", "Failed");
  284. e.printStackTrace();
  285. }
  286. _cmd = iCmd;
  287. //_iEngineStatus = ENGINE_NET_STATUS_TX_CMD;
  288. _iEngineStatus = ENGINE_NET_STATUS_RX_CMD;
  289. }
  290. }
  291. private void SetCheckSum(byte []buf, int iLen, byte cmd){
  292. /*short iChkSum= (short) (((iLen & 0x1FFF) % 0x07) ^ 0x05);
  293. buf[0] = (byte) (iLen & 0x000000FF);
  294. buf[1] = (byte) ((iLen>>8) & 0x00000FF);
  295. buf[1] = (byte) ((iChkSum<<5) | buf[1]);
  296. buf[2] = cmd;*/
  297. buf[0] = (byte) (iLen & 0x000000FF);
  298. buf[1] = (byte) ((iLen>>8) & 0x00000FF);
  299. buf[2] = (byte)_icnttest;
  300. _icnttest++;
  301. }
  302. private int ReadData(byte []byBuff, int iIdx, int iSize) throws IOException{
  303. /*int iRet=0;
  304. if (_insc.available()>0)
  305. iRet=_insc.read(byBuff, iIdx, iSize);
  306. return iRet;*/
  307. /*_socket.receive(_datagram2Read);
  308. int iRet=_datagram2Read.getLength();
  309. Engine.AddLog("datagram2Read.getLength():"+_datagram2Read.getLength());*/
  310. DatagramPacket dm = new DatagramPacket(byBuff, iSize);
  311. _socket.receive(dm);
  312. int iRet = dm.getLength();
  313. if (iRet>0)
  314. _inputread.write(byBuff,2,1);
  315. return iRet;
  316. }
  317. private void WriteData(byte []byBuff, int iIdx, int iSize) throws IOException{
  318. //_out.write(byBuff, iIdx, iSize);
  319. //_out.flush();
  320. _datagram.setData(byBuff, iIdx, iSize);
  321. _datagram.setLength(iSize);
  322. _datagram.setAddress(_addr);
  323. _datagram.setPort(9050);
  324. try {
  325. _socket.send(_datagram);
  326. } catch (IOException e) {
  327. Log.i("WriteData", "Failed");
  328. e.printStackTrace();
  329. }
  330. }
  331. }