plebble
trader.h
Go to the documentation of this file.
1 /*
2 -------------------------------------------------------------------------------
3  PLEBBLE
4 -------------------------------------------------------------------------------
5  Copyright (C) 2019-2020 KATLAS Technology. (http://katlastechnology.com)
6  Copyright (C) 2017-2020 Marcos Mayorga. (mm@mm-studios.com)
7 
8  This file is part of our Plebble(R) Platform.
9 
10  The code below cannot be copied, used for any purpose other than the one
11  agreed and/or distributed without the express permission of
12  KATLAS Technology.
13 -------------------------------------------------------------------------------
14 
15 
16 */
17 #ifndef USGOV_de56e188fcfd89220cc987a4586be02f78f88b2010f135329f1082714d9eaaa6
18 #define USGOV_de56e188fcfd89220cc987a4586be02f78f88b2010f135329f1082714d9eaaa6
19 
21 #include <us/gov/crypto/ec.h>
22 #include <us/gov/socket/datagram.h>
23 #include <us/gov/socket/client.h>
24 #include <utility>
25 #include <thread>
26 #include <vector>
27 #include <mutex>
28 #include <atomic>
29 #include <condition_variable>
30 #include <fstream>
31 #include <sstream>
32 #include "trader_protocol.h"
33 
34 namespace us{ namespace wallet {
35  using namespace std;
37 
39  struct peer_t;
40  struct wallet_local_api;
41  //struct shop_t;
43  using namespace us::gov;
44 
45  struct wallet_daemon;
46 
47  struct trader {
49 
50  trader(uint32_t host, uint16_t port, protocol* p, wallet_daemon&); //initiator constructor
51  trader(const hash_t& trade_id, socket::client&, protocol* p, wallet_daemon&); //responder constructor
52  void init();
54 
55  void on_finish();
56 
57  static string ts();
58 
59 
60  mutable ostringstream logos;
61  mutable mutex mx_logos;
62 
63 
64  template<typename type1>
65  void dolog(const type1& arg1) const {
66  logos << arg1 << ' ';
67  }
68 
69  template<typename type1, typename... types>
70  void dolog(const type1& arg1, const types& ... args) const {
71  logos << arg1 << ' ';
72  dolog(args...);
73  }
74 
75  template<typename... Args>
76  void olog(const Args&... args) const {
77  lock_guard<mutex> lock(mx_logos);
78  logos << ts() << ' ';
79  dolog(args...);
80  logos << endl;
81  }
82 
83  enum state_t {
90  num_states
91  };
92 
93  static constexpr const char* statestr[num_states]={"offline","req_online","connecting","connect_failed","connected","online"};
94 
95  string reason;
96 
97  void unable(const string& r) {
98  lock_guard<mutex> lock(mx);
99  reason=r;
100  if (cli!=0) {
101  cli->disconnect();
102  }
103  }
104 
105  bool exec(istream& cmd, ostream& os);
106  void process_work(socket::client *c, unsigned char code, const string& data);
107  void process_work(socket::client *c, unsigned char code, const vector<unsigned char>& data);
108  void dump(const string& prefix, ostream&) const;
109  void api_list(ostream&) const;
110  void help(ostream&) const;
111  void to_stream(ostream&) const;
112  static trader* from_stream(istream&);
113  void offline();
115  void run();
117 
120  uint32_t host{0};
121  uint16_t port{0};
122 
123  condition_variable cv;
126  void wait();
127 
128  void kill();
129 
130  thread* worker{0};
131  bool resume{false};
132  bool killed{false};
133 
134  vector<string> cmdq;
135 
136  socket::client* cli{0};
137  bool initiator{false};
138  wallet_daemon* demon{0};
140  mutable mutex mx;
141 
142  void show_log(ostream& os) const;
143  void show_data(ostream& os) const;
144 
145  };
146 
147 }}
148 
149 #endif
150 
us::wallet::trader::dump
void dump(const string &prefix, ostream &) const
us::wallet::hash_t
us::gov::crypto::ripemd160::value_type hash_t
Definition: trader.h:38
us::wallet::trader::state_t
state_t
Definition: trader.h:83
us::wallet::trader::dolog
void dolog(const type1 &arg1) const
Definition: trader.h:65
us::wallet::trader::state
state_t state
Definition: trader.h:139
us::wallet::trader::offline
void offline()
us::wallet::trader::set_state
void set_state(state_t)
us::wallet::trader::reason
string reason
Definition: trader.h:95
bgtask.h
us::wallet::trader::unable
void unable(const string &r)
Definition: trader.h:97
us::wallet::trader::state_connected
@ state_connected
Definition: trader.h:88
us::gov::socket::client
Definition: client.h:44
us::wallet::trader_protocol
Definition: trader_protocol.h:45
us::wallet::trader::cmdq
vector< string > cmdq
Definition: trader.h:134
us::wallet::trader::flush_command_queue
void flush_command_queue()
us::gov::dfs::peer_t
relay::peer_t peer_t
Definition: daemon.h:31
us
Definition: daemon.h:22
us::gov::crypto::ripemd160::value_type
Definition: ripemd160.h:38
us::wallet::trader::dolog
void dolog(const type1 &arg1, const types &... args) const
Definition: trader.h:70
us::wallet::trader::ts
static string ts()
us::wallet::trader::id
hash_t id
Definition: trader.h:119
trader_protocol.h
us::wallet::trader::help
void help(ostream &) const
us::wallet::trader::show_data
void show_data(ostream &os) const
us::wallet::trader::trader
trader(uint32_t host, uint16_t port, protocol *p, wallet_daemon &)
us::wallet::trader::p
protocol * p
Definition: trader.h:118
us::wallet::trader::to_stream
void to_stream(ostream &) const
us::gov::socket::datagram
Definition: datagram.h:44
us::gov::crypto::ripemd160.hash_t
Definition: ripemd160.java:25
us::wallet::trader::trader
trader(const hash_t &trade_id, socket::client &, protocol *p, wallet_daemon &)
us::gov::wbgtask
Definition: bgtask.h:67
us::wallet::trader::from_stream
static trader * from_stream(istream &)
us::wallet::trader::state_connect_failed
@ state_connect_failed
Definition: trader.h:87
us::wallet::trader::mx_logos
mutex mx_logos
Definition: trader.h:61
us::wallet::trader::protocol
trader_protocol protocol
Definition: trader.h:48
c
us::gov::wbgtask c
Definition: wbgtask.cpp:22
us::wallet::trader::show_log
void show_log(ostream &os) const
us::wallet::trader::state_connecting
@ state_connecting
Definition: trader.h:86
us::wallet::trader::state_offline
@ state_offline
Definition: trader.h:84
us::wallet::trader::api_list
void api_list(ostream &) const
us::wallet::trader::run
void run()
us::gov
Definition: daemon.h:22
us::wallet::trader::wait
void wait()
datagram.h
us::wallet::trader::~trader
~trader()
ripemd160.h
us::wallet::trader::on_finish
void on_finish()
us::wallet::trader::init
void init()
c
Definition: client.cpp:417
us::wallet::trader
Definition: trader.h:47
us::wallet::trader::mx
mutex mx
Definition: trader.h:140
std
Definition: app.h:380
us::wallet::trader::state_online
@ state_online
Definition: trader.h:89
us::wallet::trader::kill
void kill()
us::wallet::trader::online
void online(socket::client &cl, protocol *np)
us::wallet::trader::olog
void olog(const Args &... args) const
Definition: trader.h:76
us::gov::crypto::ec::keys
Definition: ec.h:43
us::wallet::trader::state_req_online
@ state_req_online
Definition: trader.h:85
us::wallet::trader::exec
bool exec(istream &cmd, ostream &os)
us::wallet::trader::set_state_
void set_state_(state_t)
us::wallet::trader::cv
condition_variable cv
Definition: trader.h:123
ec.h
us::wallet::trader::process_work
void process_work(socket::client *c, unsigned char code, const vector< unsigned char > &data)
us::wallet::trader::process_work
void process_work(socket::client *c, unsigned char code, const string &data)
client.h
us::wallet::keys
us::gov::crypto::ec::keys keys
Definition: trader.h:36
us::wallet::trader::logos
ostringstream logos
Definition: trader.h:60