1 | /* |
2 | * @(#) $Id: ServerSessionHandler.java 210062 2005-07-11 03:52:38Z 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 java.util.logging.Level; |
22 | |
23 | import org.apache.mina.common.IdleStatus; |
24 | import org.apache.mina.examples.sumup.message.AddMessage; |
25 | import org.apache.mina.examples.sumup.message.ResultMessage; |
26 | import org.apache.mina.protocol.ProtocolHandler; |
27 | import org.apache.mina.protocol.ProtocolSession; |
28 | import org.apache.mina.protocol.filter.ProtocolLoggingFilter; |
29 | import org.apache.mina.util.SessionLog; |
30 | |
31 | /** |
32 | * {@link ProtocolHandler} for SumUp server. |
33 | * |
34 | * @author The Apache Directory Project |
35 | * @version $Rev: 210062 $, $Date: 2005-07-11 12:52:38 +0900 $ |
36 | */ |
37 | public class ServerSessionHandler implements ProtocolHandler |
38 | { |
39 | |
40 | public ServerSessionHandler() |
41 | { |
42 | } |
43 | |
44 | public void sessionCreated( ProtocolSession session ) throws Exception |
45 | { |
46 | session.getFilterChain().addLast( |
47 | "logger", new ProtocolLoggingFilter() ); |
48 | } |
49 | |
50 | public void sessionOpened( ProtocolSession session ) |
51 | { |
52 | // set idle time to 60 seconds |
53 | session.getConfig().setIdleTime( IdleStatus.BOTH_IDLE, 60 ); |
54 | |
55 | // initial sum is zero |
56 | session.setAttachment( new Integer( 0 ) ); |
57 | } |
58 | |
59 | public void sessionClosed( ProtocolSession session ) |
60 | { |
61 | } |
62 | |
63 | public void messageReceived( ProtocolSession session, Object message ) |
64 | { |
65 | // client only sends AddMessage. otherwise, we will have to identify |
66 | // its type using instanceof operator. |
67 | AddMessage am = ( AddMessage ) message; |
68 | |
69 | // add the value to the current sum. |
70 | int sum = ( ( Integer ) session.getAttachment() ).intValue(); |
71 | int value = am.getValue(); |
72 | long expectedSum = ( long ) sum + value; |
73 | if( expectedSum > Integer.MAX_VALUE || expectedSum < Integer.MIN_VALUE ) |
74 | { |
75 | // if the sum overflows or underflows, return error message |
76 | ResultMessage rm = new ResultMessage(); |
77 | rm.setSequence( am.getSequence() ); // copy sequence |
78 | rm.setOk( false ); |
79 | session.write( rm ); |
80 | } |
81 | else |
82 | { |
83 | // sum up |
84 | sum = ( int ) expectedSum; |
85 | session.setAttachment( new Integer( sum ) ); |
86 | |
87 | // return the result message |
88 | ResultMessage rm = new ResultMessage(); |
89 | rm.setSequence( am.getSequence() ); // copy sequence |
90 | rm.setOk( true ); |
91 | rm.setValue( sum ); |
92 | session.write( rm ); |
93 | } |
94 | } |
95 | |
96 | public void messageSent( ProtocolSession session, Object message ) |
97 | { |
98 | } |
99 | |
100 | public void sessionIdle( ProtocolSession session, IdleStatus status ) |
101 | { |
102 | SessionLog.log( Level.WARNING, session, "Disconnecting the idle." ); |
103 | // disconnect an idle client |
104 | session.close(); |
105 | } |
106 | |
107 | public void exceptionCaught( ProtocolSession session, Throwable cause ) |
108 | { |
109 | // close the connection on exceptional situation |
110 | session.close(); |
111 | } |
112 | } |