1 | /* |
2 | * @(#) $Id: ClientSessionHandler.java 357871 2005-12-20 01:56:40Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.examples.sumup; |
20 | |
21 | import org.apache.mina.examples.sumup.message.AddMessage; |
22 | import org.apache.mina.examples.sumup.message.ResultMessage; |
23 | import org.apache.mina.protocol.ProtocolHandler; |
24 | import org.apache.mina.protocol.ProtocolHandlerAdapter; |
25 | import org.apache.mina.protocol.ProtocolSession; |
26 | import org.apache.mina.protocol.filter.ProtocolLoggingFilter; |
27 | import org.apache.mina.util.SessionLog; |
28 | |
29 | /** |
30 | * {@link ProtocolHandler} for SumUp client. |
31 | * |
32 | * @author The Apache Directory Project |
33 | * @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $ |
34 | */ |
35 | public class ClientSessionHandler extends ProtocolHandlerAdapter |
36 | { |
37 | private final int[] values; |
38 | |
39 | private boolean finished; |
40 | |
41 | public ClientSessionHandler( int[] values ) |
42 | { |
43 | this.values = values; |
44 | } |
45 | |
46 | public boolean isFinished() |
47 | { |
48 | return finished; |
49 | } |
50 | |
51 | public void sessionCreated( ProtocolSession session ) |
52 | { |
53 | session.getFilterChain().addLast( |
54 | "logger", new ProtocolLoggingFilter() ); |
55 | } |
56 | |
57 | public void sessionOpened( ProtocolSession session ) |
58 | { |
59 | // send summation requests |
60 | for( int i = 0; i < values.length; i++ ) |
61 | { |
62 | AddMessage m = new AddMessage(); |
63 | m.setSequence( i ); |
64 | m.setValue( values[ i ] ); |
65 | session.write( m ); |
66 | } |
67 | } |
68 | |
69 | public void messageReceived( ProtocolSession session, Object message ) |
70 | { |
71 | // server only sends ResultMessage. otherwise, we will have to identify |
72 | // its type using instanceof operator. |
73 | ResultMessage rm = ( ResultMessage ) message; |
74 | if( rm.isOk() ) |
75 | { |
76 | // server returned OK code. |
77 | // if received the result message which has the last sequence |
78 | // number, |
79 | // it is time to disconnect. |
80 | if( rm.getSequence() == values.length - 1 ) |
81 | { |
82 | // print the sum and disconnect. |
83 | SessionLog.info( session, "The sum: " + rm.getValue() ); |
84 | session.close(); |
85 | finished = true; |
86 | } |
87 | } |
88 | else |
89 | { |
90 | // seever returned error code because of overflow, etc. |
91 | SessionLog.warn( session, "Server error, disconnecting..." ); |
92 | session.close(); |
93 | finished = true; |
94 | } |
95 | } |
96 | |
97 | public void exceptionCaught( ProtocolSession session, Throwable cause ) |
98 | { |
99 | session.close(); |
100 | } |
101 | } |