반응형
팀프로젝트를 미리 준비하면서 만들어 본 통신 클래스...;
복잡했던 스레드 이런걸 단방에 해결해 주도록 만들었습니다.
1:n의 통신이 가능하게 하였으며... 중요 클래스를 건드리지 않고
이벤트를 작성하는 형식으로 만들어 보았습니다.
아직 부족한게 많고 허접할거 같지만...
일단 처음으로 통신과정 중에 왔다 갔다할 객체를 메세지라고 하여 만들었습니다.
그리고 그 메세지 클래스를 상속받아서 구체적인 메세지들이 구현이 됩니다.
--- Message.Java ---
package yhg.comm.message;
import java.io.Serializable;
/**
* 통신 중에 주고 받는 메세지
*
* @author Yoon HyunGook
* @since 2010-03-08
*/
public class Message implements Serializable {
private static final long serialVersionUID = 5795268628773097426L;
private int number;
private int type;
/**
* 미리 정해진 메세지 타입
* 필요시에 추가 하면됨
*/
public final static int type_MSGTable = 1;
public final static int type_MSGBoolean = 2;
public final static int type_MSGChat = 3;
public final static int type_MSGEnterClient = 4;
public final static int type_MSGLeaveClient = 5;
/**
* 생성자
*
* @author Yoon HyunGook
* @since 2010-03-08
*/
public Message(){
type = 0;
}
/**
* 메세지 타입 설정
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @param 메세지 타입
*/
protected void setType(int type){
this.type = type;
}
/**
* 메세지 타입 얻기
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @return 메세지 타입
*/
public int getType(){
return type;
}
/**
* 클라이언트 번호 얻기
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @return 클라이언트 번호
*/
public int getNumber(){
return number;
}
/**
* 클라이언트 번호 설정
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @param 클라이언트 번호
*/
public void setNumber(int num){
this.number = num;
}
/**
* 객체 출력
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @return String
*/
public String toString(){
return Integer.toString(getNumber());
}
}
--- MSGChat.java ---
package yhg.comm.message;
/**
* 채팅 메세지
*
* @author Yoon HyunGook
* @since 2010-03-08
*/
public class MSGChat extends Message{
private static final long serialVersionUID = 6997142338486570285L;
private String message;
/**
* 생성자
*
* @author Yoon HyunGook
* @since 2010-03-08
*/
public MSGChat(){
setType(Message.type_MSGChat);
}
/**
* 전송 내용 설정
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @param 전송 내용
*/
public void setMessage(String str){
message = new String(str);
}
/**
* 전송 내용 얻기
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @return 전송 내용
*/
public String getMessage(){
return message;
}
/**
* 객체 출력
*
* @author Yoon HyunGook
* @since 2010-03-08
*
* @return String
*/
public String toString(){
return getMessage();
}
}
메세지 타입은 새로운 메세지가 생길때 마다 수동적으로 추가 해줘야 합니다.
type : 메세지의 종류를 구분
number : 클라이언트의 번호 (필요시만 사용)
반응형
'Project' 카테고리의 다른 글
| [Java Communication] 버그 수정 (0) | 2010.03.10 |
|---|---|
| [Java Communication] Reference (2) | 2010.03.10 |
| [Java Communication] Server & Client (0) | 2010.03.09 |
| [UnitTest] SameTest (0) | 2009.03.18 |
| PHP Unit Test :: sameTest (0) | 2008.03.22 |