Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Adding simple HTTP controls to existing code

by bliako (Monsignor)
on Jul 05, 2021 at 12:09 UTC ( [id://11134665]=note: print w/replies, xml ) Need Help??


in reply to Adding simple HTTP controls to existing code

Are you looking for modules enabling asynchronous programming? For example, Net::Async::HTTP. Or plain-and-simple: Net::Curl::Simple which basically fetches a url and when is done it executes a callback.

Replies are listed 'Best First'.
Re^2: Adding simple HTTP controls to existing code
by brachism (Novice) on Jul 05, 2021 at 13:19 UTC

    Oversimplifying my use case, imagine a Perl program running on a headless Linux computer. A simple Jukebox playing random tracks from a play list, managed by parameters, endlessly running. Occasionally it also periodically plays announcements, such as song metadata, commercials, weather, etc between tracks.

    I want to be able to send simple HTTP commands to the headless server to, for example, change playlists, advance to next, repeat current, reshuffle, or manage the frequency of announcements, etc.

    These commands from a web browser (wget, curl, etc) via cell phone, computer, etc would take the form of http://host:port/CMD=XXXX. Where XXXX might be ATPN = play-next, ATRC = repeat-current; ATLP3 = load playlist #3; ATPP = pause playback; ATRP = resume playback, ATATF60 = adjust time announcement frequency to 60 minutes, etc. These command generally just modify variable in the host.

    I do this often on Arduino/NodeMCU’s using the ESP8266WiFi.h library. In setup() I initialize the server. In loop() I simply check, validate, process, and reply to any client request each iteration.

        Sorry, for possibly being too verbose in this thread. First, thanks for replying.

        I have a Perl program. The 1,300-ish line program runs on a headless box. It works great. It blissfully runs along managing a series of tasks. It also has absolutely nothing to do with the network.

        I simply need that program to also listen on the network for very infrequent simple HTTP requests. Those requests, if valid, simply tweak parameters in that Perl program altering it functionality. The client, in this case is not Perl. The client is just some simple web browser passing a parameter within the URL. No parallel, one host, and one client.

        The core of my program is basically an endless loop, that fires roughly every 200ms. If there is something for it to do, it does it. I just want to add into that loop, a listener for requests. If we got a request in the last 200-ish ms, process it, and continue on. If not, continue on.

        The solutions I found online and tried, all so far seem to be blocking. In other words, when I check for a request, it just listens and waits forever. Some did have timeout features, but waiting and timeout out repeatedly for infrequent traffic just seems wasteful.

        I just figured this would be easier. On a NodeMCU ESP8266 this is just a few lines of code.

        #include <ESP8266WiFi.h> Void loop() { … if (WiFi.status() != WL_CONNECTED) ConnectWifi(); client = server.available(); if (client) { // HAVE A REQUEST while (!client.available()) delay(10); String request = client.readStringUntil('\r'); client.flush(); // PROCESS REQUEST } // CONTINUE DOING ALL THE NON-NETWORK STUFF sleep(50); … }

        I will investigate Corion suggestions. This is basic stuff, so I'm sure I’ll figure out something.

      I misunderstood your use-case. You need something which listens+possibly requests, asynchronously and not something which does just requests asynchronously. So, above will be useful in replying to your clients.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134665]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-29 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found