atomic web server threads

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

PBJim wrote: Fri Mar 15, 2024 8:14 am
idle wrote: Tue Mar 12, 2024 9:07 pm fixed so it will warn you if no www directory exists
This didn't happen for me just now — I created an index.html file in the folder of the server, which the server ignored, although its status window showed "Server Running on port 80" and then "Client IP x.x.x.x load index.html client 33984320", suggesting it had found the file "index.html", but in fact it hadn't.

When I realised my mistake and put the index.html file inside a www sub-folder, it worked, but I didn't see an error beforehand.
it's got an internal index.html and error.html so it will show those if it can't find the specified www/ folder

Atomic_Server_Init("My Atomic Webserver",80,"www/",@CBPost(),@CBGet())

so
1) you need to create a subfolder in the source directory "www" and the put assets there
2) compile the temp exe in the source folder

but if it doesn't find them it will display the internal html.index "under construction" or Errror.html or a valid uri handler
http://127.0.0.1/foo?foo=1234&bar=5678
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: atomic web server threads

Post by PBJim »

idle wrote: Fri Mar 15, 2024 9:22 am
PBJim wrote: Fri Mar 15, 2024 8:14 am
idle wrote: Tue Mar 12, 2024 9:07 pm fixed so it will warn you if no www directory exists
This didn't happen for me just now — I created an index.html file in the folder of the server, which the server ignored, although its status window showed "Server Running on port 80" and then "Client IP x.x.x.x load index.html client 33984320", suggesting it had found the file "index.html", but in fact it hadn't.

When I realised my mistake and put the index.html file inside a www sub-folder, it worked, but I didn't see an error beforehand.
it's got an internal index.html and error.html so it will show those if it can't find the specified www/ folder

Atomic_Server_Init("My Atomic Webserver",80,"www/",@CBPost(),@CBGet())

so
1) you need to create a subfolder in the source directory "www" and the put assets there
2) compile the temp exe in the source folder

but if it doesn't find them it will display the internal html.index "under construction" or Errror.html or a valid uri handler
http://127.0.0.1/foo?foo=1234&bar=5678
Thanks for the reply Idle. It isn't doing that though, when I wrongly set it up and accidentally omitted the www sub-folder. The client browser just displays the default "under construction", not an error.

Anyway, it's working fine, so long as I put the necessary files into www. I'd expected an error, because of the comment "... it will warn you if no www directory exists".

Incidentally just to add to this, my interest relates to its possible use as a front-end to a PureBasic application. I have no idea how practical that might be.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

That is the desired behavior the display message was before I added the handlers so I put rhe warning in but it's not needed now it's just a default urihandler.

The next addition will be an optional virtual file system which is the same contents as your www so once your application is ready you just packit and add the pack to the datasection.
This works well if you have js frameworks so it ideal for deploying or hosting spiderbasic applications for instance.
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: atomic web server threads

Post by PBJim »

idle wrote: Fri Mar 15, 2024 8:44 pm The next addition will be an optional virtual file system which is the same contents as your www so once your application is ready you just packit and add the pack to the datasection.
This works well if you have js frameworks so it ideal for deploying or hosting spiderbasic applications for instance.
Interesting, I see what you're intending to do now. Does a SpiderBasic app need only to be installed at the server side? SpiderBasic has been a bit of a mystery in terms of where it fits exactly. I asked about SpiderBasic (specifically, is it a web server) when I bought a copy of PB but I didn't really get much information.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

spiderbasic web applications need a server to run on. Unless it's changed the spider basic compiler uses an embedded mongoose server to run your apps.

My intent is to provide a simple solution to test and host an application as a server with no dependencies, I'm essentially replacing the basic functionality of civetweb (forked from mongoose) in my civetserver project, so that it's cross platform but it's not designed to be a full blown server just a quick, easy and flexible module to use as a component.

it literally will just be

Code: Select all

Procedure CBUri(*request.Atomic_Server_client,*contentType) 
;do anything here And return a databuffer To be served 
;Select *request\RequestedFile   
;ForEach *request\parameters()  you have access to all the variables 
;set contentType   
;return *data    
EndProcedure 

Procedure CBPost(*request.Atomic_Server_client)
;you have access to all the variables 
;use a select on the *request\RequestedFile  
EndProcedure 

Procedure CBGet(*request.Atomic_Server_client)
;you have access to all the variables 
;use a select on the *request\RequestedFile  
EndProcedure 

;to host static and dynamic urls where endpoints are arbitary 
Atomic_Server_Init("My Atomic Webserver",80,"www/",@CBPost(),@CBGet())
Atomic_Server_start(0)  ;no window 
Atomic_Server_Add_Handler("foo",@CBURI()) ;<- add a handler fuction dynamicly even at runtime     
Repeat 
Until quit 
Atomic_Server_Exit() 
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 772
Joined: Fri Dec 04, 2015 9:26 pm

Re: atomic web server threads

Post by skinkairewalker »

I'm trying to add several servers to test, and only the last one created listens to the request, the other previous ones stop listening.

In this case below:

Code: Select all

Atomic_Server_Init("My Atomic Webserver",80,"www/",@CBPost(),@CBGet())
Atomic_Server_Init("Webserver 2",81,"www/",@CBPost(),@CBGet())
Atomic_Server_Init("Webserver 3",82,"www/",@CBPost(),@CBGet())
only Webserver 3 listens on port: 82, ports 80 and 81 no longer work.

what am I doing wrong?
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

skinkairewalker wrote: Mon Mar 18, 2024 7:14 pm I'm trying to add several servers to test, and only the last one created listens to the request, the other previous ones stop listening.

In this case below:

Code: Select all

Atomic_Server_Init("My Atomic Webserver",80,"www/",@CBPost(),@CBGet())
Atomic_Server_Init("Webserver 2",81,"www/",@CBPost(),@CBGet())
Atomic_Server_Init("Webserver 3",82,"www/",@CBPost(),@CBGet())
only Webserver 3 listens on port: 82, ports 80 and 81 no longer work.

what am I doing wrong?
right now you can't it needs to be adapted to run multiple instances and that will take some time to do
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

I hacked in multiple server support still needs a bit of tuning

Code: Select all

server1 = Atomic_Server_Init("My Atomic Webserver 1","www/","127.0.0.1",80,#PB_Network_IPv4,@CBPost(),@CBGet())
server2 = Atomic_Server_Init("My Atomic Webserver 2","www/","::1",81,#PB_Network_IPv6,@CBPost(),@CBGet()) ;http://[::1]:81/
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 772
Joined: Fri Dec 04, 2015 9:26 pm

Re: atomic web server threads

Post by skinkairewalker »

Why did this happen?
https://prnt.sc/BJXLj_b-xL4Y
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

skinkairewalker wrote: Wed Mar 20, 2024 1:10 am Why did this happen?
https://prnt.sc/BJXLj_b-xL4Y
It will be a thread issue, it'll be clobbering some memory, the change to make it run on multiple IP's will have likely introduced side effects and I haven't written a test case for those changes yet nor had the time to debug it and I'm only testing it on windows at the moment.

Is this crash happening all the time or randomly?
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

3.0.4.b removed the internal window.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

3.0.5a
bug fix, seems I was missing #PB_File_SharedRead on file access, despite being led on a wild goose chase :oops:
note to self make sure #PB_File_SharedRead is set on threaded file access.
the purifier stated overflow in string memory block but the crash location was totally random.
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: atomic web server threads

Post by PBJim »

idle wrote: Mon Mar 25, 2024 6:12 am bug fix, seems I was missing #PB_File_SharedRead on file access, despite being led on a wild goose chase :oops:
Out of interest, as file numbers are available throughout a process, would it be practical to open the file once in the main thread?

The reason I mention it is because I'm mindful that the documentation indicates #PB_File_SharedRead is Windows-only.
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

I haven't tested it on Linux or osx so don't know if it's a problem. if it is I will have to reconsider how I do it.
I'm trying to keep it simple and the threading model I chose makes it lock free from the user point of view.
one thread per server many threads per request which can be done in parallel without locks.
I would expect threadsafe would call the appropriate file read() function on linux osx, flockread()
the bug in windows was hard to find as there was no useful information just that it was overflowing a string buffer
or I got an IMA with a constant address.

as far as I was aware #PB_File_SharedRead is for across process access maybe that should state across threads / process
if the file has been already opened by another process for read operation, this flag is needed to access it (Windows only).
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: atomic web server threads

Post by idle »

v3.05b
Rearranged a bit and added persistence for clients with timeout in preparation for adding SSL, clients sill spawn multiple threads so might add a thread pool later but for now it's still spawn a thread per request so the server can deal with multiple clients making multiple async requests with minimal blocks.
Post Reply