Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kstn.server;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.StringReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Timer;
- import java.util.TimerTask;
- import kstn.common.TagFormatException;
- import kstn.common.Tags;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- public class InputStreamHandler_ass extends Thread {
- private ClientListManager list;
- private DataInputStream reader;
- private DataOutputStream writer;
- private String ip;
- private int time_count = 60;
- private String name;
- private ClientSocketHandler parent;
- private SAXBuilder builder;
- public InputStreamHandler_ass(InputStream is, OutputStream os,
- ClientListManager list_pt, String t_ip, ClientSocketHandler pa) {
- reader = new DataInputStream(is);
- writer = new DataOutputStream(os);
- list = list_pt;
- ip = t_ip;
- parent = pa;
- builder = new SAXBuilder();
- }
- public void run() {
- final Timer timer = new Timer();
- timer.scheduleAtFixedRate(new TimerTask() {
- public void run() {
- time_count--;
- System.out.print(name + " : " + time_count);
- if (time_count < 0) {
- list.deleteClientInfo(name);
- timer.cancel();
- parent.closeSocket();
- }
- }
- }, 0, 1000);
- try {
- String c = reader.readUTF();
- while (true) {
- System.out.println(c);
- getRequestClient(c);
- c = reader.readUTF();
- }
- } catch (Exception ioe) {
- ioe.printStackTrace();
- }
- }
- private void getRequestClient(String request) throws JDOMException,
- IOException, TagFormatException {
- Document mDocument = builder.build(new StringReader(request));
- Element rootNode = mDocument.getRootElement();
- String protType = rootNode.getName();
- if (protType.equals(Tags.SESSION)) {
- name = rootNode.getChildText(Tags.PEER_NAME);
- int port = Integer.parseInt(rootNode.getChildText(Tags.PORT));
- if (list.check(name)) {
- list.add(name, ip, port);
- time_count = 60;
- String temp = list.createListInfo(name);
- writer.writeUTF(temp);
- writer.flush();
- System.out.println("Send: " + temp);
- } else {
- writer.writeUTF("<SESSION_DENY />");
- writer.flush();
- }
- } else if (protType.equals(Tags.SESSION_KEEP_ALIVE)) {
- String name_s = rootNode.getChildText(Tags.PEER_NAME);
- String status = rootNode.getChildText(Tags.STATUS);
- if (status.equals("ALIVE")) {
- ClientInfo temp_ci = list.find(name_s);
- if (temp_ci != null) {
- time_count = 60;
- String temp = list.createListInfo(name);
- writer.writeUTF(temp);
- writer.flush();
- }
- } else if (status.equals(Tags.WILL_BE_KILL)) {
- list.deleteClientInfo(name_s);
- }
- } else {
- TagFormatException tfe = new TagFormatException(
- "wrong tag format: " + protType);
- throw tfe;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment