Thread overview
vibe.de multiple ports.
Sep 30
seany
Sep 30
seany
Sep 30
aberba
September 30
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
September 30
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.

>


September 30
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.
September 30
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)
September 30
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);
}


September 30
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 :)