clientAgent와 CopyUtil을 참조.

여기서 Key Annotation 은 Mp3Client 객체의 매소드를 HashMap에 저장했다가 번호에 따라 불러오는데 쓰임.




package mp3example;


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.lang.reflect.Method;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;


public class Mp3Client {

private Scanner keyScanner;

private String ip;

private int port;

private String path;

private Map<String, Method> methodMap;

public Mp3Client(String ip, String path) throws Exception{

super();

this.ip = ip;

this.port = 5555;

this.keyScanner = new Scanner(System.in);

this.path = path;

this.methodMap = new HashMap<String, Method>();

Class clz = this.getClass();

Method[] arr = clz.getDeclaredMethods();

for (Method method : arr) {

KeyAnno anno = method.getAnnotation(KeyAnno.class);

if(anno == null){

continue;

}

methodMap.put(anno.value(), method);

}

}

public void execute()throws Exception{

System.out.println("작업을 시작할까요?");

getUsage();

while(true){

methodMap.get(keyScanner.nextLine()).invoke(this, null);

}

}

@KeyAnno("1")

public void getUsage()throws Exception{

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("1");

}

@Override

protected void receiveMsg() throws Exception {

System.out.println(din.readUTF());

}

}.doExecute();

}

@KeyAnno("2")

public void getList()throws Exception{

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("2");

}

@Override

protected void receiveMsg() throws Exception {

System.out.println(din.readUTF());

}

}.doExecute();

}

@KeyAnno("3")

public void download()throws Exception{

System.out.println("다운로드할 파일명을 입력하세요");

final String fileName = keyScanner.nextLine();

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("3");

dos.writeUTF(fileName);

}

@Override

protected void receiveMsg() throws Exception {

String saveFileName = din.readUTF();

FileOutputStream fos = new FileOutputStream(path+saveFileName);

CopyUtil.copy(din, fos, false, true);

System.out.println("전송이 완료되었습니다.");

}

}.doExecute();

}

@KeyAnno("4")

public void upload()throws Exception{

System.out.println("업로드할 파일명을 입력하세요");

final String fileName = keyScanner.nextLine();

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("4");

dos.writeUTF(fileName);

FileInputStream fin = new FileInputStream(path+fileName);

CopyUtil.copy(fin, dos, true, false);

System.out.println("업로드가 완료되었습니다.");

}

@Override

protected void receiveMsg() throws Exception {

}

}.doExecute();

}

public static void main(String[] args)throws Exception {

Mp3Client client = new Mp3Client("127.0.0.1","C:\\zzz\\");

client.execute();

}

}



--------------------------------------------------------------------------------------------------

package mp3example;


import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;


@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface KeyAnno {


public String value();

}

Posted by 타다키치
,