tudzic

Untitled

Oct 29th, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. package kstn.server;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.StringReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.Timer;
  10. import java.util.TimerTask;
  11.  
  12. import kstn.common.TagFormatException;
  13. import kstn.common.Tags;
  14.  
  15. import org.jdom.Document;
  16. import org.jdom.Element;
  17. import org.jdom.JDOMException;
  18. import org.jdom.input.SAXBuilder;
  19.  
  20. public class InputStreamHandler_ass extends Thread {
  21. private ClientListManager list;
  22. private DataInputStream reader;
  23. private DataOutputStream writer;
  24. private String ip;
  25. private int time_count = 60;
  26. private String name;
  27. private ClientSocketHandler parent;
  28. private SAXBuilder builder;
  29.  
  30. public InputStreamHandler_ass(InputStream is, OutputStream os,
  31. ClientListManager list_pt, String t_ip, ClientSocketHandler pa) {
  32. reader = new DataInputStream(is);
  33. writer = new DataOutputStream(os);
  34. list = list_pt;
  35. ip = t_ip;
  36. parent = pa;
  37. builder = new SAXBuilder();
  38. }
  39.  
  40. public void run() {
  41. final Timer timer = new Timer();
  42. timer.scheduleAtFixedRate(new TimerTask() {
  43. public void run() {
  44. time_count--;
  45. System.out.print(name + " : " + time_count);
  46. if (time_count < 0) {
  47. list.deleteClientInfo(name);
  48. timer.cancel();
  49. parent.closeSocket();
  50. }
  51. }
  52. }, 0, 1000);
  53. try {
  54. String c = reader.readUTF();
  55. while (true) {
  56. System.out.println(c);
  57. getRequestClient(c);
  58. c = reader.readUTF();
  59. }
  60. } catch (Exception ioe) {
  61. ioe.printStackTrace();
  62. }
  63. }
  64.  
  65. private void getRequestClient(String request) throws JDOMException,
  66. IOException, TagFormatException {
  67. Document mDocument = builder.build(new StringReader(request));
  68. Element rootNode = mDocument.getRootElement();
  69. String protType = rootNode.getName();
  70. if (protType.equals(Tags.SESSION)) {
  71. name = rootNode.getChildText(Tags.PEER_NAME);
  72. int port = Integer.parseInt(rootNode.getChildText(Tags.PORT));
  73.  
  74. if (list.check(name)) {
  75. list.add(name, ip, port);
  76. time_count = 60;
  77. String temp = list.createListInfo(name);
  78. writer.writeUTF(temp);
  79. writer.flush();
  80. System.out.println("Send: " + temp);
  81. } else {
  82. writer.writeUTF("<SESSION_DENY />");
  83. writer.flush();
  84. }
  85. } else if (protType.equals(Tags.SESSION_KEEP_ALIVE)) {
  86. String name_s = rootNode.getChildText(Tags.PEER_NAME);
  87. String status = rootNode.getChildText(Tags.STATUS);
  88. if (status.equals("ALIVE")) {
  89. ClientInfo temp_ci = list.find(name_s);
  90. if (temp_ci != null) {
  91. time_count = 60;
  92. String temp = list.createListInfo(name);
  93. writer.writeUTF(temp);
  94. writer.flush();
  95. }
  96. } else if (status.equals(Tags.WILL_BE_KILL)) {
  97. list.deleteClientInfo(name_s);
  98. }
  99. } else {
  100. TagFormatException tfe = new TagFormatException(
  101. "wrong tag format: " + protType);
  102. throw tfe;
  103. }
  104.  
  105. }
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment