OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <assert.h> |
| 6 #include <stddef.h> |
| 7 #include <stdint.h> |
| 8 #include <stdio.h> |
| 9 #include <stdlib.h> |
| 10 #include <string.h> |
| 11 |
| 12 #include <new> |
| 13 #include <vector> |
| 14 |
| 15 #include "mojo/public/bindings/generated/sample_service.h" |
| 16 #include "mojo/public/bindings/generated/sample_service_proxy.h" |
| 17 #include "mojo/public/bindings/generated/sample_service_stub.h" |
| 18 |
| 19 namespace sample { |
| 20 |
| 21 static void PrintSpacer(int depth) { |
| 22 for (int i = 0; i < depth; ++i) |
| 23 printf(" "); |
| 24 } |
| 25 |
| 26 static void Print(int depth, const char* name, bool value) { |
| 27 PrintSpacer(depth); |
| 28 printf("%s: %s\n", name, value ? "true" : "false"); |
| 29 } |
| 30 |
| 31 static void Print(int depth, const char* name, int32_t value) { |
| 32 PrintSpacer(depth); |
| 33 printf("%s: %d\n", name, value); |
| 34 } |
| 35 |
| 36 static void Print(int depth, const char* name, uint8_t value) { |
| 37 PrintSpacer(depth); |
| 38 printf("%s: %u\n", name, value); |
| 39 } |
| 40 |
| 41 static void Print(int depth, const char* name, mojo::Handle value) { |
| 42 PrintSpacer(depth); |
| 43 printf("%s: 0x%x\n", name, value.value); |
| 44 } |
| 45 |
| 46 static void Print(int depth, const char* name, const mojo::String* str) { |
| 47 PrintSpacer(depth); |
| 48 printf("%s: \"%*s\"\n", name, static_cast<int>(str->size()), str->data()); |
| 49 } |
| 50 |
| 51 static void Print(int depth, const char* name, const Bar* bar) { |
| 52 PrintSpacer(depth); |
| 53 printf("%s: %p\n", name, bar); |
| 54 if (bar) { |
| 55 ++depth; |
| 56 Print(depth, "alpha", bar->alpha()); |
| 57 Print(depth, "beta", bar->beta()); |
| 58 Print(depth, "gamma", bar->gamma()); |
| 59 --depth; |
| 60 } |
| 61 } |
| 62 |
| 63 template <typename T> |
| 64 static void Print(int depth, const char* name, const mojo::Array<T>* array) { |
| 65 PrintSpacer(depth); |
| 66 printf("%s: %p\n", name, array); |
| 67 if (array) { |
| 68 ++depth; |
| 69 for (size_t i = 0; i < array->size(); ++i) { |
| 70 char buf[32]; |
| 71 sprintf(buf, "%lu", i); |
| 72 Print(depth, buf, array->at(i)); |
| 73 } |
| 74 --depth; |
| 75 } |
| 76 } |
| 77 |
| 78 static void Print(int depth, const char* name, const Foo* foo) { |
| 79 PrintSpacer(depth); |
| 80 printf("%s: %p\n", name, foo); |
| 81 if (foo) { |
| 82 ++depth; |
| 83 Print(depth, "name", foo->name()); |
| 84 Print(depth, "x", foo->x()); |
| 85 Print(depth, "y", foo->y()); |
| 86 Print(depth, "a", foo->a()); |
| 87 Print(depth, "b", foo->b()); |
| 88 Print(depth, "c", foo->c()); |
| 89 Print(depth, "bar", foo->bar()); |
| 90 Print(depth, "extra_bars", foo->extra_bars()); |
| 91 Print(depth, "data", foo->data()); |
| 92 Print(depth, "files", foo->files()); |
| 93 --depth; |
| 94 } |
| 95 } |
| 96 |
| 97 class ServiceImpl : public ServiceStub { |
| 98 public: |
| 99 virtual void Frobinate(const Foo* foo, bool baz, mojo::Handle port) { |
| 100 // Users code goes here to handle the incoming Frobinate message. |
| 101 // We'll just dump the Foo structure and all of its members. |
| 102 |
| 103 printf("Frobinate:\n"); |
| 104 |
| 105 int depth = 1; |
| 106 Print(depth, "foo", foo); |
| 107 Print(depth, "baz", baz); |
| 108 Print(depth, "port", port); |
| 109 } |
| 110 }; |
| 111 |
| 112 class SimpleMessageSender : public mojo::MessageSender { |
| 113 public: |
| 114 virtual void Send(const mojo::Message& message) { |
| 115 // Imagine some IPC happened here. |
| 116 |
| 117 // In the receiving process, an implementation of ServiceStub is known to |
| 118 // the system. It receives the incoming message. |
| 119 ServiceImpl impl; |
| 120 |
| 121 ServiceStub* stub = &impl; |
| 122 stub->OnMessageReceived(message); |
| 123 } |
| 124 }; |
| 125 |
| 126 void Exercise() { |
| 127 SimpleMessageSender sender; |
| 128 |
| 129 // User has a proxy to a Service somehow. |
| 130 Service* service = new ServiceProxy(&sender); |
| 131 |
| 132 // User constructs a message to send. |
| 133 |
| 134 // Notice that it doesn't matter in what order the structs / arrays are |
| 135 // allocated. Here, the various members of Foo are allocated before Foo is |
| 136 // allocated. |
| 137 |
| 138 mojo::MessageBuffer buf; |
| 139 |
| 140 Bar* bar = buf.New<Bar>(); |
| 141 bar->set_alpha(20); |
| 142 bar->set_beta(40); |
| 143 bar->set_gamma(60); |
| 144 |
| 145 const char kName[] = "foopy"; |
| 146 const size_t kNameLen = sizeof(kName) - 1; |
| 147 mojo::String* name = buf.NewString(kNameLen); |
| 148 memcpy(name->data(), kName, kNameLen); |
| 149 |
| 150 const size_t kExtraBarsLen = 3; |
| 151 mojo::Array<Bar*>* extra_bars = buf.NewArray<Bar*>(kExtraBarsLen); |
| 152 for (size_t i = 0; i < kExtraBarsLen; ++i) { |
| 153 Bar* bar = buf.New<Bar>(); |
| 154 bar->set_alpha(i * 100); |
| 155 bar->set_beta(i * 100 + 20); |
| 156 bar->set_gamma(i * 100 + 40); |
| 157 (*extra_bars)[i] = bar; |
| 158 } |
| 159 |
| 160 const size_t kDataLen = 10; |
| 161 mojo::Array<uint8_t>* data = buf.NewArray<uint8_t>(kDataLen); |
| 162 for (size_t i = 0; i < kDataLen; ++i) |
| 163 (*data)[i] = static_cast<uint8_t>(kDataLen - i); |
| 164 |
| 165 const size_t kFilesLen = 4; |
| 166 mojo::Array<mojo::Handle>* files = buf.NewArray<mojo::Handle>(kFilesLen); |
| 167 for (size_t i = 0; i < kFilesLen; ++i) |
| 168 (*files)[i].value = 0xFFFF - i; |
| 169 |
| 170 Foo* foo = buf.New<Foo>(); |
| 171 foo->set_name(name); |
| 172 foo->set_x(1); |
| 173 foo->set_y(2); |
| 174 foo->set_a(false); |
| 175 foo->set_b(true); |
| 176 foo->set_c(false); |
| 177 foo->set_bar(bar); |
| 178 foo->set_extra_bars(extra_bars); |
| 179 foo->set_data(data); |
| 180 foo->set_files(files); |
| 181 |
| 182 mojo::Handle port = { 10 }; |
| 183 |
| 184 service->Frobinate(foo, true, port); |
| 185 } |
| 186 |
| 187 } // namespace sample |
| 188 |
| 189 int main() { |
| 190 sample::Exercise(); |
| 191 return 0; |
| 192 } |
OLD | NEW |