Package de.dwerth.gntpserver.gntp

Source Code of de.dwerth.gntpserver.gntp.InputThread

package de.dwerth.gntpserver.gntp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import de.dwerth.gntpserver.gntp.messages.GNTPMessage;
import de.dwerth.gntpserver.helpers.MessageHelper;

public class InputThread extends Thread {

  private Socket socket;

  public InputThread(Socket s) {
    socket = s;
  }

  @Override
  public void run() {
    OutputStream os = null;
    InputStream is = null;
    try {
      os = socket.getOutputStream();
      is = socket.getInputStream();
      byte[] rawData = MessageHelper.readInput(is);
      GNTPMessage message = MessageFactory.parseRawData(rawData);

      try {
        while (MessageHelper.processMessage(message, is, os)) {
          rawData = MessageHelper.readInput(is);
          message = MessageFactory.parseRawData(rawData);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      System.out.println("closing connection");
      try {
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      try {
        os.close();
      } catch (IOException e) {
        e.printStackTrace();
      }

      try {
        socket.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
}
TOP

Related Classes of de.dwerth.gntpserver.gntp.InputThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.