A multi-process simulation of a gas container controlled via a REST API. A server simulates the container’s physics (mass, temperature, pressure) and exposes endpoints. Independent input and output clients read the pressure and add/remove gas mass according to configurable thresholds. The container temperature drifts every 2 seconds; if pressure crosses safety limits the container is marked destroyed and then reset.
This repository documents the REST implementation.
RestGasPressure/
— ASP.NET Core Web API (the server)Controllers/GasPressureController.cs
exposes the REST endpointsGasContainerLogic.cs
holds state and business logic- Swagger/OpenAPI available at
/swagger
input/
— input client- Periodically calls the API; when pressure is below the lower limit it increases mass
- Client class generated with NSwag (
InputClient.cs
)
output/
— output client- Periodically calls the API; when pressure is above the upper limit it decreases mass
- Client class generated with NSwag (
OutputClient.cs
)
Processes run independently and communicate only through HTTP.
-
State and physics
- Temperature (K), mass (arbitrary units), pressure computed as
P = (m * T) / 22.4
- Background loop (server) changes temperature randomly every 2s and checks limits
- Limits (defaults from code):
- Lower add limit:
PressureLimit = 100
— inputs can add mass only when pressure is below this value - Upper remove limit:
UpperPressureLimit = 150
— outputs can remove mass only when pressure is above this value - Implosion limit:
ImplosionLimit = 10
— below this, container is destroyed - Explosion limit:
ExplosionLimit = 200
— above this, container is destroyed
- Lower add limit:
- When destroyed, the server logs the event and resets state to initial values
- Temperature (K), mass (arbitrary units), pressure computed as
-
Clients’ behavior
- Input client: reads pressure; if
< 100
, posts a small random mass increase - Output client: reads pressure; if
> 100
(client-side threshold), it attempts a small random mass decrease; the server only applies removal when pressure is above the upper limit (> 150
by default) - Both clients stop acting if the container is destroyed (until it’s reset by the server’s loop)
- Input client: reads pressure; if
-
Concurrency and safety
- All state mutations are protected by a lock in
GasContainerState
- Server operations are non-blocking; the periodic work is done in a background thread
- All state mutations are protected by a lock in
Base URL: http://127.0.0.1:5000
POST /increaseMass?mass=<double>
— add mass if pressure is below lower limitPOST /decreaseMass?mass=<double>
— remove mass if pressure is above upper limitGET /getPressure
— returns the current pressure (double)GET /isContainerDestroyed
— returnstrue
/false
OpenAPI/Swagger UI: http://127.0.0.1:5000/swagger
Client proxies are generated with NSwag and checked in (input/InputClient.cs
, output/OutputClient.cs
).
Prerequisites:
- .NET 8 SDK
In separate terminals, run the server and each client.
Server (port 5000):
cd RestGasPressure
dotnet run
Input client:
cd input
dotnet run
Output client:
cd output
dotnet run
Optional quick test with curl:
curl http://127.0.0.1:5000/getPressure
curl -X POST "http://127.0.0.1:5000/increaseMass?mass=2"
curl -X POST "http://127.0.0.1:5000/decreaseMass?mass=1"
curl http://127.0.0.1:5000/isContainerDestroyed
Logs are written to stdout via NLog in all processes.
- Port: configured in
RestGasPressure/Program.cs
to listen on127.0.0.1:5000
- CORS: open policy enabled to allow any origin/method/header (for easy local testing)
- Swagger: XML comments included; UI enabled by default
- Clear separation of concerns: controller (API), logic (state/physics), networking (generated clients)
- Contracts published via OpenAPI; strongly-typed clients generated from the contract
- Continuous cycles for each component; robust to transient HTTP errors
- Thread-safe state updates; checks for implosion/explosion conditions
This project is licensed under the MIT License. See the LICENSE
file for details.
- ASP.NET Core for the server
- NSwag for client generation
- NLog for logging