| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __NET_RPC_LIB_SERVER_RPC_SERVICE_INVOKER_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_SERVER_RPC_SERVICE_INVOKER_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <whisperlib/net/rpc/lib/types/rpc_loggable.h> |
|---|
| 37 |
#include <whisperlib/net/rpc/lib/types/rpc_message.h> |
|---|
| 38 |
#include <whisperlib/net/rpc/lib/server/rpc_core_types.h> |
|---|
| 39 |
|
|---|
| 40 |
// This is an interface for all rpc services. Every RPC service must implement |
|---|
| 41 |
// this interface in order to operate with the rpc::ServicesManager |
|---|
| 42 |
|
|---|
| 43 |
namespace rpc { |
|---|
| 44 |
class ServicesManager; |
|---|
| 45 |
class ServiceInvoker : public Loggable { |
|---|
| 46 |
public: |
|---|
| 47 |
ServiceInvoker(const string& service_class_name, |
|---|
| 48 |
const string& service_instance_name) |
|---|
| 49 |
: service_class_name_(service_class_name), |
|---|
| 50 |
service_instance_name_(service_instance_name) { |
|---|
| 51 |
} |
|---|
| 52 |
virtual ~ServiceInvoker() { |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
// Returns service class. |
|---|
| 56 |
const string& GetClassName() const { |
|---|
| 57 |
return service_class_name_; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
// Returns service name. |
|---|
| 61 |
const string& GetName() const { |
|---|
| 62 |
return service_instance_name_; |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
// Call a specific method of this service: |
|---|
| 66 |
// method(params) -> [status, return value] |
|---|
| 67 |
// Please check: rpc::ServicesManager::Call for a more detail description of |
|---|
| 68 |
// in and out parameters as the function relate (one calls |
|---|
| 69 |
// the other) |
|---|
| 70 |
virtual bool Call(rpc::Query * q) = 0; |
|---|
| 71 |
|
|---|
| 72 |
// Helper that allows the display of form for RPC requests directly |
|---|
| 73 |
// from the server. |
|---|
| 74 |
// base_path - the path on which we export the service (e.g. /rpc) |
|---|
| 75 |
// url_path - the path received from client (e.g. from |
|---|
| 76 |
// /rpc/MyService/__form_GetSomeStuff -> "__form_GetSomeStuff") |
|---|
| 77 |
// |
|---|
| 78 |
// we return empty string if we do not recognize the service. |
|---|
| 79 |
virtual string GetTurntablePage(const string& base_path, |
|---|
| 80 |
const string& url_path) const = 0; |
|---|
| 81 |
|
|---|
| 82 |
////////////////////////////////////////////////////////////////////// |
|---|
| 83 |
// |
|---|
| 84 |
// rpc::Loggable interface methods |
|---|
| 85 |
// |
|---|
| 86 |
string ToString() const { |
|---|
| 87 |
return string("RPCServiceInvoker ") + GetName(); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
protected: |
|---|
| 91 |
// the service class name (e.g. "Logger") |
|---|
| 92 |
const string service_class_name_; |
|---|
| 93 |
// a custom name for this instance (e.g. "log1", "log2") |
|---|
| 94 |
const string service_instance_name_; |
|---|
| 95 |
|
|---|
| 96 |
////////////////////////////////////////////////////////////////////// |
|---|
| 97 |
// |
|---|
| 98 |
// bug-trap. Mark registered services to prevent accidental deletion. |
|---|
| 99 |
// |
|---|
| 100 |
bool registered_; |
|---|
| 101 |
|
|---|
| 102 |
void SetRegistered(bool registered) { |
|---|
| 103 |
registered_ = registered; |
|---|
| 104 |
} |
|---|
| 105 |
|
|---|
| 106 |
friend class rpc::ServicesManager; |
|---|
| 107 |
|
|---|
| 108 |
////////////////////////////////////////////////////////////////////// |
|---|
| 109 |
|
|---|
| 110 |
DISALLOW_EVIL_CONSTRUCTORS(ServiceInvoker); |
|---|
| 111 |
}; |
|---|
| 112 |
} |
|---|
| 113 |
#endif // __NET_RPC_LIB_SERVER_RPC_SERVICE_INVOKER_H__ |
|---|