-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder_server.cpp
106 lines (79 loc) · 2.95 KB
/
order_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <cassert>
#include <stdexcept>
#include <iostream>
#include <unistd.h>
#include <string>
#include "xmlrpc-c/base.hpp"
#include "xmlrpc-c/registry.hpp"
#include "xmlrpc-c/server_abyss.hpp"
#include "xmlrpc-c/client_simple.hpp"
using namespace std;
using namespace xmlrpc_c;
string const catalogUrl = "http://localhost:8082/RPC2";
class buyMethod : public xmlrpc_c::method {
public:
buyMethod() {
this->_signature = "i:ii";
this->_help = "This method buys a book specified by an item number. The method returns -1 if the purchase failed, and 0 otherwise.";
}
void
execute(xmlrpc_c::paramList const& paramList,
xmlrpc_c::value * const retvalP) {
// Client stub to act as a client to the XML-RPC call to the catalog server
xmlrpc_c::clientSimple myClient;
// Variable for storing the result from the server
xmlrpc_c::value result;
// Verify that the input to this function is only a single parameter
paramList.verifyEnd(2);
// Get the first argument to the method, which should be a string
// int item_number = paramList.getInt(0);
//===================begin edits ===========================
xmlrpc_c::value result2;
int item_number = paramList.getInt(0);
int change = paramList.getInt(1);
//======== TODO QUERY ==========
int int_value;
myClient.call(catalogUrl, "queryItem.rpc", "i", &result, item_number);
value_array lookup_array(result);
vector<value> const lookup_array_entries(lookup_array.vectorValueValue());
int_value = value_int(lookup_array_entries[1]);
if(int_value > 0){
//======== QUANTITY GOOD ===============
myClient.call(catalogUrl, "update.rpc", "ii", &result2, item_number, change);
// Get the result from the catalog server, in the form of an array
value_int response_array(result2);
// Forward the array to the client
*retvalP = value_int(result2);
}
else{
//=========== QUANTITY BAD ===============
*retvalP = xmlrpc_c::value_int(-1);
}
//====================end edits ============================
} catch (exception const& e) {
cerr << "Client threw error: " << e.what() << endl;
} catch (...) {
cerr << "Client threw unexpected error." << endl;
}
};
int
main(int const,
const char ** const) {
try {
xmlrpc_c::registry myRegistry;
xmlrpc_c::methodPtr const buyMethodRPC(new buyMethod);
// The order server only exposes the buy RPC to the front-end server
myRegistry.addMethod("buy.rpc", buyMethodRPC);
// Configure the order server to start on port 8081
xmlrpc_c::serverAbyss myAbyssServer(
xmlrpc_c::serverAbyss::constrOpt()
.registryP(&myRegistry)
.portNumber(8081));
// Start my server; this call never returns
myAbyssServer.run();
assert(false);
} catch (exception const& e) {
cerr << "Something failed. " << e.what() << endl;
}
return 0;
}