Skip to content

Commit 886eaba

Browse files
Merge greeterAsync into greeter (C++) (#256)
1 parent 561f229 commit 886eaba

20 files changed

+64
-850
lines changed

cpp/C++ demos.sln

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -458,20 +458,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "client", "Ice\greeter\msbui
458458
EndProject
459459
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "Ice\greeter\msbuild\server\server.vcxproj", "{E4B54261-3921-40D9-AA09-C3C89D096350}"
460460
EndProject
461-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "greeterAsync", "greeterAsync", "{2E31C2CA-8493-4957-9E89-FC9546C2F70B}"
462-
ProjectSection(SolutionItems) = preProject
463-
Ice\greeterAsync\README.md = Ice\greeterAsync\README.md
464-
EndProjectSection
465-
EndProject
466461
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "secure", "secure", "{DD1B1121-F502-4AB7-8012-633DDC3DD610}"
467462
ProjectSection(SolutionItems) = preProject
468463
Ice\secure\README.md = Ice\secure\README.md
469464
EndProjectSection
470465
EndProject
471-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "client", "Ice\greeterAsync\msbuild\client\client.vcxproj", "{BEB2285A-1D68-4B0B-BFDC-4DC9AFE43A75}"
472-
EndProject
473-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "Ice\greeterAsync\msbuild\server\server.vcxproj", "{9D71F7CB-6F15-41DC-B6D7-69A144513FC3}"
474-
EndProject
475466
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "client", "Ice\secure\msbuild\client\client.vcxproj", "{651398C4-452B-4E64-8536-4DD9FA88AE1A}"
476467
EndProject
477468
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "server", "Ice\secure\msbuild\server\server.vcxproj", "{A80500EC-13A8-4E5C-AE4D-5168572B9D11}"

cpp/Ice/greeter/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ target_link_libraries(client Ice::Ice)
1111
add_executable(server Server.cpp Chatbot.cpp Chatbot.h Greeter.ice)
1212
slice2cpp_generate(server)
1313
target_link_libraries(server Ice::Ice)
14+
15+
add_executable(serveramd ServerAMD.cpp ChatbotAMD.cpp ChatbotAMD.h GreeterAMD.ice)
16+
slice2cpp_generate(serveramd)
17+
target_link_libraries(serveramd Ice::Ice)

cpp/Ice/greeterAsync/Chatbot.cpp renamed to cpp/Ice/greeter/ChatbotAMD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) ZeroC, Inc.
22

3-
#include "Chatbot.h"
3+
#include "ChatbotAMD.h"
44

55
#include <chrono>
66
#include <future>

cpp/Ice/greeterAsync/Chatbot.h renamed to cpp/Ice/greeter/ChatbotAMD.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) ZeroC, Inc.
22

3-
#ifndef CHATBOT_H
4-
#define CHATBOT_H
3+
#ifndef CHATBOT_AMD_H
4+
#define CHATBOT_AMD_H
55

6-
#include "Greeter.h"
6+
#include "GreeterAMD.h"
77

88
namespace GreeterServer
99
{

cpp/Ice/greeter/Client.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "Greeter.h"
44

55
#include <cstdlib>
6+
#include <future>
67
#include <iostream>
78

89
using namespace std;
@@ -18,7 +19,7 @@ main(int argc, char* argv[])
1819
}
1920
if (name == nullptr)
2021
{
21-
name = "alice";
22+
name = "masked user";
2223
}
2324

2425
// Create an Ice communicator to initialize the Ice runtime. The CommunicatorHolder is a RAII helper that creates
@@ -32,10 +33,32 @@ main(int argc, char* argv[])
3233
// or IP address.
3334
VisitorCenter::GreeterPrx greeter{communicator, "greeter:tcp -h localhost -p 4061"};
3435

35-
// Send a request to the remote object and get the response. Both the -> and . syntax can be used to make
36-
// invocations with the proxy.
36+
// Send a request to the remote object and wait synchronously for the response.
37+
// Both the -> and . syntax can be used to make invocations with the proxy.
3738
string greeting = greeter->greet(name);
39+
cout << greeting << endl;
40+
41+
// Send another request to the remote object, this time with greetAsync. greetAsync returns a future immediately.
42+
future<string> futureGreeting = greeter->greetAsync("alice");
3843

44+
// Wait for the response.
45+
greeting = futureGreeting.get();
3946
cout << greeting << endl;
47+
48+
// Send a third request to the remote object, this time with the greetAsync overload that accepts callbacks.
49+
promise<void> promise;
50+
greeter->greetAsync(
51+
"bob",
52+
[&promise](string_view greeting) // response callback
53+
{
54+
cout << greeting << endl;
55+
promise.set_value();
56+
},
57+
[&promise](std::exception_ptr exceptionPtr) // exception callback
58+
{ promise.set_exception(exceptionPtr); });
59+
60+
// Wait for the response/exception callback to be called.
61+
promise.get_future().get();
62+
4063
return 0;
4164
}
File renamed without changes.

cpp/Ice/greeter/README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
# Greeter
22

3-
The Greeter demo illustrates how to send a request and wait for the response.
3+
The Greeter demo illustrates how to build a simple application with Ice.
44

5-
To build the demo run:
5+
It features a client which demonstrates the 3 APIs you can use to make remote invocations using generated Ice proxies:
6+
7+
- synchronous (`greet`),
8+
- asynchronous with futures (`greetAsync`); and
9+
- asynchronous with callbacks (another `greetAsync`)
10+
11+
This demo also provides two implementations for the server: a synchronous dispatch implementation (`server`), and an
12+
asynchronous dispatch implementation (`serveramd`). The client works with both.
13+
14+
To build the demo, run:
615

716
```shell
817
cmake -B build
918
cmake --build build --config Release
1019
```
1120

21+
The build produces 3 executables: client, server, and serveramd.
22+
1223
To run the demo, first start the server:
1324

1425
**Linux/macOS:**
@@ -17,10 +28,22 @@ To run the demo, first start the server:
1728
./build/server
1829
```
1930

31+
or
32+
33+
```shell
34+
./build/serveramd
35+
```
36+
2037
**Windows:**
2138

2239
```shell
23-
.\build\Release\server
40+
build\Release\server
41+
```
42+
43+
or
44+
45+
```shell
46+
build\Release\serveramd
2447
```
2548

2649
In a separate window, start the client:
@@ -34,5 +57,5 @@ In a separate window, start the client:
3457
**Windows:**
3558

3659
```shell
37-
.\build\Release\client
60+
build\Release\client
3861
```

cpp/Ice/greeter/Server.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ main(int argc, char* argv[])
2626

2727
// Start dispatching requests.
2828
adapter->activate();
29+
cout << "Listening on port 4061..." << endl;
2930

3031
// Wait until the user presses Ctrl+C.
3132
int signal = ctrlCHandler.wait();

cpp/Ice/greeterAsync/Server.cpp renamed to cpp/Ice/greeter/ServerAMD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) ZeroC, Inc.
22

3-
#include "Chatbot.h"
3+
#include "ChatbotAMD.h"
44

55
#include <iostream>
66

@@ -26,6 +26,7 @@ main(int argc, char* argv[])
2626

2727
// Start dispatching requests.
2828
adapter->activate();
29+
cout << "Listening on port 4061..." << endl;
2930

3031
// Wait until the user presses Ctrl+C.
3132
int signal = ctrlCHandler.wait();

cpp/Ice/greeterAsync/CMakeLists.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)