Index: examples/bank_app/customer.cc |
diff --git a/examples/bank_app/customer.cc b/examples/bank_app/customer.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aa05c2eb625aaf0180e2d3c23098078ab49c2652 |
--- /dev/null |
+++ b/examples/bank_app/customer.cc |
@@ -0,0 +1,68 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <stdio.h> |
+ |
+#include "examples/bank_app/bank.mojom.h" |
+ |
jamesr
2015/08/19 05:30:55
nit: no blank line needed here
gautham
2015/08/19 17:45:51
Done.
|
+#include "mojo/public/c/system/main.h" |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/application_runner.h" |
+#include "mojo/public/cpp/utility/run_loop.h" |
+#include "mojo/services/vanadium/security/public/interfaces/principal.mojom.h" |
+ |
+namespace mojo { |
jamesr
2015/08/19 05:30:55
drop the mojo namespace
gautham
2015/08/19 17:45:51
Done.
|
+namespace examples { |
+ |
+using namespace vanadium; |
jamesr
2015/08/19 05:30:55
ditto re using-declarations
gautham
2015/08/19 17:45:51
Done.
|
+ |
+class LoginHandler { |
+ public: |
+ void Run(const BlessingPtr& b) const { |
+ std::string user; |
+ if (b) { |
+ for (size_t i = 0; i < b->chain.size(); i++) { |
+ user += "/"; |
+ user += b->chain[i]->extension; |
+ } |
+ } |
+ if (!user.empty()) { |
+ MOJO_LOG(INFO) << "Welcome: " << user; |
+ } |
+ RunLoop::current()->Quit(); // All done! |
jamesr
2015/08/19 05:30:55
should the login callback quit? feels like this sh
gautham
2015/08/19 17:45:51
Your right. This should not quit. I thought I'd re
|
+ } |
+}; |
+ |
+class BankCustomer : public ApplicationDelegate { |
+ public: |
+ void Initialize(ApplicationImpl* app) override { |
+ // Get user login credentials |
+ app->ConnectToService("mojo:principal_service", &login_service_); |
+ login_service_->Login(LoginHandler()); |
+ login_service_.WaitForIncomingResponse(); |
jamesr
2015/08/19 05:30:55
check for failure
gautham
2015/08/19 17:45:51
Done.
|
+ |
+ BankPtr bank; |
+ app->ConnectToService("mojo:bank", &bank); |
+ bank->Deposit(500/*usd*/); |
+ bank->Withdraw(100/*usd*/); |
+ auto gb_callback = [](const int32_t& balance) { |
+ MOJO_LOG(INFO) << "Bank balance: " << balance; |
+ }; |
+ bank->GetBalance(Callback<void(const int32_t&)>(gb_callback)); |
jamesr
2015/08/19 05:30:54
does just GetBalance(gb_callback) not work? the ty
gautham
2015/08/19 17:45:51
../../mojo/public/cpp/bindings/callback.h: In inst
|
+ bank.WaitForIncomingResponse(); |
+ } |
+ void Quit() override { |
+ login_service_->Logout(); |
+ } |
+ private: |
+ PrincipalServicePtr login_service_; |
+}; |
+ |
+} // namespace examples |
+} // namespace mojo |
+ |
+MojoResult MojoMain(MojoHandle application_request) { |
+ mojo::ApplicationRunner runner(new mojo::examples::BankCustomer); |
+ return runner.Run(application_request); |
+} |