본문 바로가기

IT for developer/Netty

(9)
Netty 예제 분석 - LocalTime LocalTime 기존 예제들과 다른 형태의 통신방식을 보여준다. 73 LocalTimeClientHandler handler = 74 channel.getPipeline().get(LocalTimeClientHandler.class); 75 76 // Request and get the response. 77 List response = handler.getLocalTimes(cities); 핸들러에 구현한 함수를 호출해서 리턴받는 형태이다. 그러면 getLocalTImes 라는 함수를 한번 살펴보자. 65 channel.write(builder.build());서버에 도시 목록을 전달하면 해당 도시들의 시간을 응답해주는 듯하다. 여기에서 하려는 것은 시간을 응답해줄 때 까지 기다렸다가 나머지 처리를 ..
Netty 예제 분석 - PortUnification PortUnification 단일 포트로 여러 서비스를 해주는 예제이다. 현재 요청하는 서비스가 무엇인지 판단하여 파이프라인에 현재 핸드러를 삭제하고 새로운 핸들러들을 등록하는 형태이다. HTTP 서비스로 연결해주는 경우 아래와 같이 HTTP.snoop 예제에서 살펴본 것처럼 4개의 핸들러를 새롭게 등록하고 현재 핸들러(this)를 제거한다. 147 private void switchToHttp(ChannelHandlerContext ctx) { 148 ChannelPipeline p = ctx.getPipeline(); 149 p.addLast("decoder", new HttpRequestDecoder()); 150 p.addLast("encoder", new HttpResponseEncoder())..
Netty 예제 분석 - ObjectEcho ObjectEcho 이번 예제의 특징은 자바 객체를 송수신하다는 점이다. 우선 클라이언트 프로그램을 살펴보자. 67 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { 68 public ChannelPipeline getPipeline() throws Exception { 69 return Channels.pipeline( 70 new ObjectEncoder(), 71 new ObjectDecoder(), 72 new ObjectEchoClientHandler(firstMessageSize)); 73 } 74 }); ChannelBuffer String 변환을 하는 StringEncoder와 StringDecoder가 아니라 ObjectEncode..
Netty 예제 분석 - Quote of the Moment Quote of the Moment UDP/IP 브로드캐스트 예제 기존에 예제들 처럼 1:1 통신이 아니라 브로드캐스트 통신 예제이다. 우선 클리이언트를 살펴보면 49 ConnectionlessBootstrap b = new ConnectionlessBootstrap(f);62 b.setOption("broadcast", "true"); ClientBootstrap 이 아니라 ConnectionlessBootstrap을 사용하고 있다.ConnectionlessBootstrap은 UDP/IP와 같은 연결지속성이 없는 네트워크를위해 사용된다. 그리고 브로드캐스트 형태로 통신하도록 옵션을 설정했다. 74 b.setOption( 75 "receiveBufferSizePredictorFactory", 76 new..
Netty 예제 분석 - Telnet Telnet http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/telnet/package-summary.html 우선 클라이언트를 살펴보자. main 함수에서 키보드로 입력한 데이터를 서버에 전송한다. 직접 서버에 데이터를 전송한다. 'bye' 를 칠때 까지 계속 전송한다. 'bye'를 치면 접속을 종료한다. 71 // Read commands from the stdin. 72 ChannelFuture lastWriteFuture = null; 73 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 74 for (;;) { 75 String line = in.rea..
Netty 예제 분석 - HTTP.snoop HTTP.snoop http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/http/snoop/package-summary.html 간단한 HTTP 클라이언트와 서버를 구현하고 있다. 클라이언트 부터 살펴보자. 기존 다른 예제와 다르게 클라이언트 main 함수에서 하는일들이 참 많다. 79 bootstrap.setPipelineFactory(new HttpClientPipelineFactory(ssl));PipelineFactory는 Anonymous 클래스를 사용하지않고 HttpClientPiplelineFactory 클래스를 별도로 생성했다. PipelineFactory는 결국 핸들러들을 등록하는 역할을 수행하는데 좀 더 많은 핸들러를 조건에 따라..
Netty 예제 분석 - Uptime Uptime http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/uptime/package-summary.html Uptime 예제는 연결을 끊기면 재접속을 시도하는 클라이언트측 프로그램이다. Discard와 Echo 클라이언트 프로그램과 다른 코드 부분을 살펴보면 다음과 같다. 72 bootstrap.setPipelineFactory(new ChannelPipelineFactory() { 73 public ChannelPipeline getPipeline() throws Exception { 74 return Channels.pipeline( 75 new ReadTimeoutHandler(timer, READ_TIMEOUT), 76 new Upt..
Netty 예제 분석 - Echo Echo http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/echo/package-summary.html Discard 와 다른점은 클라이언트가 전달한 데이터를 서버가 응답한다는 것이다. 핸들러에 messageReceived라는 함수가 다음과 같이 구현되었다. 47 @Override 48 public void messageReceived( 49 ChannelHandlerContext ctx, MessageEvent e) { 50 // Send back the received message to the remote peer. 51 transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readab..