www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibe.de multiple ports.

reply seany <seany uni-bonn.de> writes:
Hello

I am trying to use this example for a iot application: 
https://aberba.com/2018/using-vibe-d-web-interface/

The code i use is:

ushort port               =       5504;

void main(char[][] args)
{

         auto router = new URLRouter;
         router.post("/archive", &savedata);
         router.get("/archive", &savedata);

         auto settings = new HTTPServerSettings;
         settings.port = port;
         settings.bindAddresses = ["::1", "0.0.0.0"];
         listenHTTP(settings, router);

         runApplication();
}


This is fine. But now that we have ~ 100 IoT devices in the 
field, I would like to assign each a new port. The devices should 
be communicating over HTTP as the vendor software running 
thereupon uses http.

What is the best practice to make a vibe.d server listen to 
multiple ports? Make a loop scanning each loop seems wasteful. Is 
there a function in vibe.d that can take multiple ports, and 
launch a set of threads assigned to each port?

Thank you
Sep 30 2020
next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
Dne st 30. 9. 2020 13:25 u=C5=BEivatel seany via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> napsal:

 Hello

 I am trying to use this example for a iot application:
 https://aberba.com/2018/using-vibe-d-web-interface/

 The code i use is:

 ushort port               =3D       5504;

 void main(char[][] args)
 {

          auto router =3D new URLRouter;
          router.post("/archive", &savedata);
          router.get("/archive", &savedata);

          auto settings =3D new HTTPServerSettings;
          settings.port =3D port;
          settings.bindAddresses =3D ["::1", "0.0.0.0"];
          listenHTTP(settings, router);

          runApplication();
 }


 This is fine. But now that we have ~ 100 IoT devices in the
 field, I would like to assign each a new port.
Why? I do not see any reason for that.

Sep 30 2020
parent reply seany <seany uni-bonn.de> writes:
On Wednesday, 30 September 2020 at 12:29:06 UTC, Daniel Kozak 
wrote:
 Dne st 30. 9. 2020 13:25 uživatel seany via Digitalmars-d-learn 
 < digitalmars-d-learn puremagic.com> napsal:

 Hello

 I am trying to use this example for a iot application: 
 https://aberba.com/2018/using-vibe-d-web-interface/

 The code i use is:

 ushort port               =       5504;

 void main(char[][] args)
 {

          auto router = new URLRouter;
          router.post("/archive", &savedata);
          router.get("/archive", &savedata);

          auto settings = new HTTPServerSettings;
          settings.port = port;
          settings.bindAddresses = ["::1", "0.0.0.0"];
          listenHTTP(settings, router);

          runApplication();
 }


 This is fine. But now that we have ~ 100 IoT devices in the 
 field, I would like to assign each a new port.
Why? I do not see any reason for that.
to separate the messages from the IoT responses quickly and forward them to different programs, and to have the capability in hand, so that when later i have an idea to exploit the capability, I can also do it.
Sep 30 2020
next sibling parent Daniel Kozak <kozzi11 gmail.com> writes:
On Wed, Sep 30, 2020 at 2:40 PM seany via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:

 On Wednesday, 30 September 2020 at 12:29:06 UTC, Daniel Kozak
 wrote:

 to separate the messages from the IoT responses quickly and
 forward them to different programs, and to have the capability in
 hand, so that when later i have an idea to exploit the
 capability, I can also do it.
Ok as Adam said you can do something like this: void main() { immutable ushort startPort = 5500; import std.range : iota; foreach (ushort port; iota!(ushort,ushort)(startPort, startPort + 100)) { auto settings = new HTTPServerSettings; settings.port = port; settings.bindAddresses = ["::1", "127.0.0.1"]; listenHTTP(settings, &hello); } runApplication(); } void hello(HTTPServerRequest req, HTTPServerResponse res) { import std.conv : to; res.writeBody(req.fullURL.port.to!string); }
Sep 30 2020
prev sibling parent aberba <karabutaworld gmail.com> writes:
On Wednesday, 30 September 2020 at 12:38:10 UTC, seany wrote:
 On Wednesday, 30 September 2020 at 12:29:06 UTC, Daniel Kozak 
 wrote:
 Dne st 30. 9. 2020 13:25 uživatel seany via 
 Digitalmars-d-learn < digitalmars-d-learn puremagic.com> 
 napsal:

 Hello

 I am trying to use this example for a iot application: 
 https://aberba.com/2018/using-vibe-d-web-interface/

 The code i use is:

 ushort port               =       5504;

 void main(char[][] args)
 {

          auto router = new URLRouter;
          router.post("/archive", &savedata);
          router.get("/archive", &savedata);

          auto settings = new HTTPServerSettings;
          settings.port = port;
          settings.bindAddresses = ["::1", "0.0.0.0"];
          listenHTTP(settings, router);

          runApplication();
 }


 This is fine. But now that we have ~ 100 IoT devices in the 
 field, I would like to assign each a new port.
Why? I do not see any reason for that.
to separate the messages from the IoT responses quickly and forward them to different programs, and to have the capability in hand, so that when later i have an idea to exploit the capability, I can also do it.
What you are doing, if I understand you well, is a way of scaling the server to handle high amount of traffic. If that's right, then I'm not sure how the number of PORT is the issue. Irrespective of the port, the server resources remains the same. And if that's the case, then you might need a load balancer (or use nginx or any of the options I've used in my projects). In that case, you run multiple versions of the server in stateless environments... scaled up or down on demand. -------- Been a long while since I wrote some vibe.d tutorials though :)
Sep 30 2020
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 30 September 2020 at 11:23:59 UTC, seany wrote:

         auto router = new URLRouter;
         router.post("/archive", &savedata);
         router.get("/archive", &savedata);

         auto settings = new HTTPServerSettings;
         settings.port = port;
         settings.bindAddresses = ["::1", "0.0.0.0"];
         listenHTTP(settings, router);
If you loop this section it should listen to each port. This is just a setup loop, it wouldn't actually be wasteful once it is running. (I think, I don't really know vibe super well)
Sep 30 2020