rippled
LedgerHandler.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_RPC_HANDLERS_LEDGER_H_INCLUDED
21 #define RIPPLE_RPC_HANDLERS_LEDGER_H_INCLUDED
22 
23 #include <ripple/app/main/Application.h>
24 #include <ripple/app/ledger/LedgerToJson.h>
25 #include <ripple/app/ledger/LedgerMaster.h>
26 #include <ripple/ledger/ReadView.h>
27 #include <ripple/json/Object.h>
28 #include <ripple/protocol/jss.h>
29 #include <ripple/rpc/Context.h>
30 #include <ripple/rpc/Status.h>
31 #include <ripple/rpc/impl/Handler.h>
32 #include <ripple/rpc/Role.h>
33 
34 namespace Json {
35 class Object;
36 }
37 
38 namespace ripple {
39 namespace RPC {
40 
41 struct JsonContext;
42 
43 // ledger [id|index|current|closed] [full]
44 // {
45 // ledger: 'current' | 'closed' | <uint256> | <number>, // optional
46 // full: true | false // optional, defaults to false.
47 // }
48 
50 public:
51  explicit LedgerHandler (JsonContext&);
52 
53  Status check ();
54 
55  template <class Object>
56  void writeResult (Object&);
57 
58  static char const* name()
59  {
60  return "ledger";
61  }
62 
63  static Role role()
64  {
65  return Role::USER;
66  }
67 
69  {
70  return NO_CONDITION;
71  }
72 
73 private:
78  int options_ = 0;
80 };
81 
84 //
85 // Implementation.
86 
87 template <class Object>
88 void LedgerHandler::writeResult (Object& value)
89 {
90  if (ledger_)
91  {
92  Json::copyFrom (value, result_);
93  addJson (value, {*ledger_, options_, queueTxs_, type_});
94  }
95  else
96  {
97  auto& master = context_.app.getLedgerMaster ();
98  {
99  auto&& closed = Json::addObject (value, jss::closed);
100  addJson (closed, {*master.getClosedLedger(), 0});
101  }
102  {
103  auto&& open = Json::addObject (value, jss::open);
104  addJson (open, {*master.getCurrentLedger(), 0});
105  }
106  }
107 }
108 
109 } // RPC
110 } // ripple
111 
112 #endif
ripple::RPC::LedgerHandler
Definition: LedgerHandler.h:49
ripple::RPC::JsonContext
Definition: Context.h:52
ripple::RPC::LedgerHandler::context_
JsonContext & context_
Definition: LedgerHandler.h:74
std::shared_ptr
STL class.
std::vector
STL class.
ripple::RPC::LedgerHandler::ledger_
std::shared_ptr< ReadView const > ledger_
Definition: LedgerHandler.h:75
Json::copyFrom
void copyFrom(Json::Value &to, Json::Value const &from)
Copy all the keys and values from one object into another.
Definition: Object.cpp:206
ripple::RPC::LedgerHandler::role
static Role role()
Definition: LedgerHandler.h:63
ripple::RPC::LedgerHandler::condition
static Condition condition()
Definition: LedgerHandler.h:68
Json
JSON (JavaScript Object Notation).
Definition: json_reader.cpp:26
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::Role::USER
@ USER
ripple::RPC::NO_CONDITION
@ NO_CONDITION
Definition: Handler.h:40
ripple::RPC::LedgerHandler::name
static char const * name()
Definition: LedgerHandler.h:58
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
ripple::RPC::Condition
Condition
Definition: Handler.h:39
ripple::RPC::Status
Status represents the results of an operation that might fail.
Definition: Status.h:39
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::RPC::LedgerHandler::LedgerHandler
LedgerHandler(JsonContext &)
Definition: LedgerHandler.cpp:34
ripple::RPC::LedgerHandler::writeResult
void writeResult(Object &)
Definition: LedgerHandler.h:88
ripple::LedgerEntryType
LedgerEntryType
Ledger entry types.
Definition: LedgerFormats.h:36
Json::addObject
Json::Value & addObject(Json::Value &, Json::StaticString const &key)
Add a new subobject at a named key in a Json object.
Definition: Object.h:399
ripple::RPC::LedgerHandler::type_
LedgerEntryType type_
Definition: LedgerHandler.h:79
ripple::RPC::LedgerHandler::check
Status check()
Definition: LedgerHandler.cpp:38
ripple::addJson
void addJson(Json::Value &json, LedgerFill const &fill)
Given a Ledger and options, fill a Json::Object or Json::Value with a description of the ledger.
Definition: LedgerToJson.cpp:263
ripple::RPC::LedgerHandler::options_
int options_
Definition: LedgerHandler.h:78
ripple::RPC::LedgerHandler::queueTxs_
std::vector< TxQ::TxDetails > queueTxs_
Definition: LedgerHandler.h:76
ripple::RPC::LedgerHandler::result_
Json::Value result_
Definition: LedgerHandler.h:77
ripple::Role
Role
Indicates the level of administrative permission to grant.
Definition: Role.h:40
Json::Value
Represents a JSON value.
Definition: json_value.h:141
ripple::open
void open(soci::session &s, BasicConfig const &config, std::string const &dbName)
Open a soci session.
Definition: SociDB.cpp:98