tutorials:http_server
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorials:http_server [2024/12/02 08:32] – [Starting the Server and Running a first Model] tim | tutorials:http_server [2024/12/16 11:45] (current) – gaetan | ||
|---|---|---|---|
| Line 12: | Line 12: | ||
| + | If we now go on [[http:// | ||
| + | The command we are interested in is the open command, which is called with: | ||
| + | |||
| + | http:// | ||
| + | |||
| + | This works with every project and just opens the project, but the definition of the path follows some semi-intuitive rules: | ||
| + | - you don't use a key: different to most URL based parameters you do write something like ? | ||
| + | - you cant use absolute paths. all paths start form the Root directory defined in the GroIMP preferences under HTTP server/ Open Project. | ||
| + | |||
| + | |||
| + | ==== Get a return value ==== | ||
| + | |||
| + | |||
| + | The returned values for the client needs to be defined in the project it self. Therefore we need a custom startup function which is called very time the project is started. | ||
| + | |||
| + | Ours will now try to get the HttpResponse object for this workbench and if that exists tell GroIMP to start the run function with the response as a parameter. | ||
| <code java> | <code java> | ||
| - | // import other compiled files (.rgg, .java, .xl) without subdirectory path. | ||
| - | import parameters.*; | ||
| import de.grogra.imp.net.*; | import de.grogra.imp.net.*; | ||
| - | /*** | ||
| - | A simple exampel of using the GroIMP HTTP server | + | protected void startup() |
| + | { | ||
| + | super.startup(); | ||
| + | HttpResponse resp = HttpResponse.get(workbench()); | ||
| + | if(resp!=null){ | ||
| + | runLater(resp); | ||
| + | } | ||
| + | } | ||
| - | http:// | + | </code> |
| - | java -jar platform.core-2.1.5-jar-with-dependencies.jar --headless -- -cmd "/ | ||
| + | The Run function can than be defined as you wish and would normally include the execution of your model similar to a headless execution. In our case we skip this part and directly return our message to the client and then close the workbench: | ||
| + | <code java> | ||
| - | ***/ | + | protected void run(Object info) |
| + | { | ||
| + | HttpResponse resp = (HttpResponse)info; | ||
| + | resp.setContent(" | ||
| + | resp.send(true); | ||
| + | closeWorkbench(); | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | If you save both of this files in a project and open it through the server it will return " | ||
| + | |||
| + | Additionally it is possible to access the original request from the project, which allows us to receive additional parameters given by he client. | ||
| + | |||
| + | For instance the following allows us to add ''& | ||
| + | |||
| + | <code java> | ||
| + | import de.grogra.imp.net.*; | ||
| protected void startup() | protected void startup() | ||
| Line 36: | Line 75: | ||
| HttpResponse resp = HttpResponse.get(workbench()); | HttpResponse resp = HttpResponse.get(workbench()); | ||
| if(resp!=null){ | if(resp!=null){ | ||
| - | println(" | ||
| runLater(resp); | runLater(resp); | ||
| } | } | ||
| } | } | ||
| - | |||
| protected void run(Object info) | protected void run(Object info) | ||
| Line 53: | Line 90: | ||
| closeWorkbench(); | closeWorkbench(); | ||
| } | } | ||
| - | |||
| - | |||
| </ | </ | ||
| ===== Starting the server headless ===== | ===== Starting the server headless ===== | ||
tutorials/http_server.1733124774.txt.gz · Last modified: 2024/12/02 08:32 by tim
