Skip to content

Commit

Permalink
New Greeter demo for C#
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier committed Jan 27, 2025
1 parent 2072caf commit 1b917a6
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cpp/Ice/greeter/Chatbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;
string
GreeterServer::Chatbot::greet(string name, const Ice::Current&)
{
cout << "Dispatching greet request { name = " << name << " }" << endl;
cout << "Dispatching greet request { name = '" << name << "' }" << endl;

ostringstream os;
os << "Hello, " << name << "!";
Expand Down
2 changes: 1 addition & 1 deletion cpp/Ice/greeterAsync/Chatbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ GreeterServer::Chatbot::greetAsync(
// thread pool is configured with a single thread in this demo (the default configuration); as a result, we don't
// need to serialize access to the _tasks field.

cout << "Dispatching greet request { name = " << name << " }" << endl;
cout << "Dispatching greet request { name = '" << name << "' }" << endl;

// Simulate a long-running background operation by using std::async.
// Note that we're moving all arguments except current into the lambda expression.
Expand Down
20 changes: 20 additions & 0 deletions csharp/Ice/Greeter/Client/Client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Copy the PDBs from the NuGet packages to get file names and line numbers in stack traces. -->
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
</PropertyGroup>
<ItemGroup>
<SliceCompile Include="../slice/Greeter.ice" />
<PackageReference Include="zeroc.ice.net" Version="3.8.0-alpha0" />
<PackageReference Include="zeroc.icebuilder.msbuild" Version="5.0.9" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions csharp/Ice/Greeter/Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) ZeroC, Inc.

// Slice module VisitorCenter in Greeter.ice maps to C# namespace VisitorCenter.
using VisitorCenter;

// Create an Ice communicator to initialize the Ice runtime. The communicator is disposed before the program exits.
using Ice.Communicator communicator = Ice.Util.initialize(ref args);

// GreeterPrx is a class generated by the Slice compiler. We create a proxy from a communicator and a "stringified
// proxy" with the address of the target object.
// If you run the server on a different computer, replace localhost in the string below with the server's hostname
// or IP address.
GreeterPrx greeter = GreeterPrxHelper.createProxy(communicator, "greeter:tcp -h localhost -p 4061");

// Send a request to the remote object and get the response.
// TODO: remap to GreetAsync with cs:identifier
string greeting = await greeter.greetAsync(Environment.UserName);

Console.WriteLine(greeting);
36 changes: 36 additions & 0 deletions csharp/Ice/Greeter/Greeter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33122.133
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{527EEA4D-77B9-4252-A2CD-C641A25CAD53}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FBDAF448-665A-4595-98D3-4538C56A666D}"
ProjectSection(SolutionItems) = preProject
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBF1199A-46A4-4AE9-AFFE-4D8DD59EB874}.Release|Any CPU.Build.0 = Release|Any CPU
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{527EEA4D-77B9-4252-A2CD-C641A25CAD53}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B36F1E1-0592-4A15-9981-67BC4A653EC4}
EndGlobalSection
EndGlobal
23 changes: 23 additions & 0 deletions csharp/Ice/Greeter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Greeter

The Greeter demo illustrates how to send a request and wait for the response.

You can build the client and server applications with:

``` shell
dotnet build
```

First start the Server program:

```shell
cd Server
dotnet run
```

In a separate terminal, start the Client program:

```shell
cd Client
dotnet run
```
16 changes: 16 additions & 0 deletions csharp/Ice/Greeter/Server/Chatbot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) ZeroC, Inc.

using VisitorCenter;

namespace GreeterServer;

/// <summary>A Chatbot is an Ice servant that implements Slice interface Greeter.</summary>
internal class Chatbot : GreeterDisp_
{
// Implements the abstract method greet from the GreeterDisp_ class generated by the Slice compiler.
public override string greet(string name, Ice.Current current)
{
Console.WriteLine($"Dispatching greet request {{ name = '{name}' }}");
return $"Hello, {name}!";
}
}
17 changes: 17 additions & 0 deletions csharp/Ice/Greeter/Server/Program.CancelKeyPressed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) ZeroC, Inc.

// Extends Program to provide the CancelKeyPressed property.
public static partial class Program
{
/// <summary>Gets a task that completes when the console receives a Ctrl+C.</summary>
public static Task CancelKeyPressed => _cancelKeyPressedTcs.Task;

private static readonly TaskCompletionSource _cancelKeyPressedTcs = new();

static Program() =>
Console.CancelKeyPress += (sender, eventArgs) =>
{
eventArgs.Cancel = true;
_ = _cancelKeyPressedTcs.TrySetResult();
};
}
18 changes: 18 additions & 0 deletions csharp/Ice/Greeter/Server/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) ZeroC, Inc.

using GreeterServer;

// Create an Ice communicator to initialize the Ice runtime. The communicator is disposed before the program exits.
using Ice.Communicator communicator = Ice.Util.initialize(ref args);

// Create an object adapter that listens for incoming requests and dispatches them to servants.
var adapter = communicator.createObjectAdapterWithEndpoints("GreeterAdapter", "tcp -p 4061");

// Register the Chatbot servant with the adapter.
adapter.add(new Chatbot(), Ice.Util.stringToIdentity("greeter"));

// Start dispatching requests.
adapter.activate();

// Wait until the user presses Ctrl+C.
await CancelKeyPressed;
20 changes: 20 additions & 0 deletions csharp/Ice/Greeter/Server/Server.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<!-- Copy the PDBs from the NuGet packages to get file names and line numbers in stack traces. -->
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
</PropertyGroup>
<ItemGroup>
<SliceCompile Include="../slice/Greeter.ice" />
<PackageReference Include="zeroc.ice.net" Version="3.8.0-alpha0" />
<PackageReference Include="zeroc.icebuilder.msbuild" Version="5.0.9" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions csharp/Ice/Greeter/slice/Greeter.ice
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) ZeroC, Inc.

#pragma once

module VisitorCenter
{
/// Represents a simple greeter.
interface Greeter
{
/// Creates a personalized greeting.
/// @param name: The name of the person to greet.
/// @returns: The greeting.
string greet(string name);
}
}

0 comments on commit 1b917a6

Please sign in to comment.