| 1 | /** |
| 2 | * Copyright (C) 2006 NetMind Consulting Bt. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Lesser General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 3 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | package hu.netmind.beankeeper.node.impl; |
| 20 | |
| 21 | import hu.netmind.beankeeper.common.StoreException; |
| 22 | import java.net.*; |
| 23 | import java.io.*; |
| 24 | import java.util.*; |
| 25 | import org.apache.log4j.Logger; |
| 26 | |
| 27 | /** |
| 28 | * A node service provider implmenetation, which uses a connection |
| 29 | * to a server node to delegate functionality. |
| 30 | * @author Brautigam Robert |
| 31 | * @version Revision: $Revision$ |
| 32 | */ |
| 33 | public class NodeClient extends NetEndpoint |
| 34 | { |
| 35 | private static Logger logger = Logger.getLogger(NodeClient.class); |
| 36 | private NodeManagerImpl nodeManager = null; |
| 37 | |
| 38 | public NodeClient(NodeManagerImpl nodeManager, Socket socket, int index, int serverIndex) |
| 39 | { |
| 40 | super(socket,"BeanKeeper Client (Node: "+index+" -> "+serverIndex+")"); |
| 41 | this.nodeManager=nodeManager; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * If a network error occurs, just disconnect node manager, which will eventually |
| 46 | * call this object's close too. |
| 47 | */ |
| 48 | public void onError() |
| 49 | { |
| 50 | logger.debug("handling communication error by disconnecting client"); |
| 51 | try |
| 52 | { |
| 53 | nodeManager.ensureDisconnect(this); |
| 54 | } catch ( Exception e ) { |
| 55 | logger.warn("could not ensure node manager is disconnected because of",e); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * The only message a client is supposed to get is a broadcast call |
| 61 | * message, so handle that. |
| 62 | */ |
| 63 | public void onIncoming(CommObject obj) |
| 64 | { |
| 65 | if ( ! (obj instanceof CallMessage) ) |
| 66 | return; |
| 67 | CallMessage msg = (CallMessage) obj; |
| 68 | CallResponse response = null; |
| 69 | // Do the call |
| 70 | try |
| 71 | { |
| 72 | // Execute the call locally |
| 73 | Object result = nodeManager.callLocal(msg.getService(),msg.getMethod(), |
| 74 | msg.getParameterTypes(),msg.getParameters()); |
| 75 | response = new CallResponse(obj,result,null); |
| 76 | } catch ( StoreException e ) { |
| 77 | logger.warn("called operation yielded: ",e); |
| 78 | response = new CallResponse(obj,null,e); |
| 79 | } |
| 80 | // Send the result back to server |
| 81 | send(response); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |