Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kstn.client;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.net.Socket;
- import java.util.ArrayList;
- import java.util.Timer;
- import java.util.TimerTask;
- import org.jdom.JDOMException;
- import kstn.common.Communicate;
- import kstn.common.ProtocolResultFromServer;
- import kstn.common.TagFormatException;
- import kstn.common.Tags;
- public class OnlineStatusKeepingHandler extends Thread{
- private Socket socket;
- private int countDown;
- private String myUserName;
- private GUIMain mainForm;
- DataOutputStream out;
- DataInputStream in;
- ArrayList<ClientContact> contact;
- public OnlineStatusKeepingHandler(Socket jsocket, String userName, GUIMain main) {
- // TODO Auto-generated constructor stub
- socket = jsocket;
- countDown = 15;
- myUserName = userName;
- mainForm = main;
- try {
- out = new DataOutputStream(socket.getOutputStream());
- in = new DataInputStream(socket.getInputStream());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void run() {
- // TODO: Each 15s send online status to server
- Timer timer = new Timer();
- timer.scheduleAtFixedRate(new TimerTask() {
- @Override
- public void run() {
- // TODO Auto-generated method stub
- countDown--;
- if(countDown == 0){
- countDown = 15;
- try {
- out.writeUTF(Tags.SESSION_STT_S + Tags.PEER_NAME_S + myUserName
- + Tags.PEER_NAME_E + Tags.STATUS_S + "ALIVE" + Tags.STATUS_E
- + Tags.SESSION_STT_E);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // TODO: read for result a list to update for main form
- String result;
- try {
- result = in.readUTF();
- ProtocolResultFromServer pro = Communicate.getProtocolResultFromServer(result);
- contact = pro.getClientContacts();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (JDOMException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (TagFormatException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- mainForm.updateList(contact);
- }
- }
- }, 0, 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment