How to use listener.d example?

Neia Neutuladh neia at ikeran.org
Fri Aug 31 17:55:15 UTC 2018


On Friday, 31 August 2018 at 07:38:54 UTC, Marcin wrote:
> https://github.com/dlang/dmd/blob/master/samples/listener.d
>
> Can some one add more comment to that example?
>
> I need to make code that connects to local application, very 
> similar to this.
>
> Assumptions:
> 1. Create an application that listens to arguments.
> 2. Create an application that will send arguments to the 
> application mentioned above.
> 3. The application will return the sine (argument) to the 
> client.
> 4. All communication must be realized via console.

Another, more structured way to do this is with an RPC framework 
like Apache Thrift. With Thrift, you'd write an interface 
description:

---
namespace d math.api
service Sine
{
   double sine(1: double value);
}
---

In your application, you'd have something like:

---
import vibe.d;
import vibethrift;
import math.api.Sine;

class SineImpl : Sine
{
   double sine(double value)
   {
     static import std.math;
     return std.math.sin(value);
   }
}
void main()
{
   serve!Sine(new SineImpl, "0.0.0.0", 5555);
   return runApplication();
}
---

Then you use the corresponding Thrift client to make requests 
against it.

You could also do this with Vibe and REST:

---
import vibe.d;
class Sine
{
   @path("/sine")
   string getSine(double value)
   {
     static import std.math;
     import std.conv : to;
     return std.math.sin(value).to!string;
   }
}
void main()
{
   auto settings = new HTTPServerSettings;
   settings.port = 5555;
   auto router = new URLRouter;
   router.registerWebInterface(new Sine);
   listenHTTP(settings, router);
   runApplication();
}
---

And then you can point your browser at 
http://localhost:5555/sine?value=1.5 and get back 0.997495. You 
can similarly use std.net.curl to make the request.

But let's say you want to do everything yourself.

That script is the server. Line 55 is the thing that handles the 
input. You need to put your code there.

The remote application might send input in multiple packets. That 
means you have to collect input somewhere and figure out where 
the end is. You can either pass a length as the first part 
(usually a 4-byte value in network byte order), or require a 
special character to terminate the commend (I recommend the ASCII 
Record Separator character, U+001E, or the like), or know enough 
about your input to figure out where it ends anyway.

Once you get the end of your input, you send it off somewhere to 
parse and process. It's up to you how you want to send numbers 
across. When you're done, you need to convert the output numbers 
back to bytes somehow and send them back with socket.send(), and 
then you close the connection.

Once you've got that working, you need to write a client that 
does pretty much the same thing, but Socket.connect instead of 
the bind/listen/accept business, and you can just use Socket.read 
for the response rather than dealing with a SocketSet.


More information about the Digitalmars-d-learn mailing list