tudzic

Untitled

Oct 29th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. package kstn.client;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.Socket;
  7. import java.util.ArrayList;
  8. import java.util.Timer;
  9. import java.util.TimerTask;
  10.  
  11. import org.jdom.JDOMException;
  12.  
  13. import kstn.common.Communicate;
  14. import kstn.common.ProtocolResultFromServer;
  15. import kstn.common.TagFormatException;
  16. import kstn.common.Tags;
  17.  
  18. public class OnlineStatusKeepingHandler extends Thread{
  19. private Socket socket;
  20. private int countDown;
  21. private String myUserName;
  22. private GUIMain mainForm;
  23.  
  24. DataOutputStream out;
  25. DataInputStream in;
  26.  
  27. ArrayList<ClientContact> contact;
  28.  
  29. public OnlineStatusKeepingHandler(Socket jsocket, String userName, GUIMain main) {
  30. // TODO Auto-generated constructor stub
  31. socket = jsocket;
  32. countDown = 15;
  33. myUserName = userName;
  34. mainForm = main;
  35.  
  36. try {
  37. out = new DataOutputStream(socket.getOutputStream());
  38. in = new DataInputStream(socket.getInputStream());
  39. } catch (IOException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43. }
  44. @Override
  45. public void run() {
  46. // TODO: Each 15s send online status to server
  47. Timer timer = new Timer();
  48. timer.scheduleAtFixedRate(new TimerTask() {
  49.  
  50. @Override
  51. public void run() {
  52. // TODO Auto-generated method stub
  53. countDown--;
  54. if(countDown == 0){
  55. countDown = 15;
  56. try {
  57. out.writeUTF(Tags.SESSION_STT_S + Tags.PEER_NAME_S + myUserName
  58. + Tags.PEER_NAME_E + Tags.STATUS_S + "ALIVE" + Tags.STATUS_E
  59. + Tags.SESSION_STT_E);
  60. } catch (IOException e) {
  61. // TODO Auto-generated catch block
  62. e.printStackTrace();
  63. }
  64.  
  65. // TODO: read for result a list to update for main form
  66. String result;
  67. try {
  68. result = in.readUTF();
  69. ProtocolResultFromServer pro = Communicate.getProtocolResultFromServer(result);
  70. contact = pro.getClientContacts();
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. } catch (JDOMException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. } catch (TagFormatException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }
  81.  
  82. mainForm.updateList(contact);
  83. }
  84. }
  85. }, 0, 1000);
  86.  
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment