OpenTTD webclient (PHP)

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Connum
Engineer
Engineer
Posts: 127
Joined: 25 Dec 2006 17:05

OpenTTD webclient (PHP)

Post by Connum »

Hey guys (and girls, if any ;))!

This is more PHP-related and has less to do with the OTTD-code itsself... But I thought this would be the right place to go with my issue...

I want to be able to start and stop my OpenTTD dedicated server from any computer through a kind of "web-client". It's just a small thing with a few lines of code, consisting of a form asking for a username and a password, and if you are logged in you have three links "start server", "stop server", and "logout". Killing the openttd.exe-Process is no problem.
It's a Windows-Server, so I use

Code: Select all

taskkill /F /IM openttd.exe
and I'm able to close a server manually opened (the server is not a paid server, it's located in the cellar of my parent's house).

But I can't get the function for starting the server to work!

I tried to execute ""C:\\OpenTTD\\openttd.exe -D" with exec and system commands, which did indeed open a openttd.exe-process (I could even see it in the taskmanager), but didn't start the server. And additionally, php was waiting for the process to end, so the browser kept on loading and loading... I found out that his is normal behaviour for the php execution commands and you can get around it by passing the output to a file. So I tried to do that with a code I found via google

Code: Select all

shell_exec("C:\\OpenTTD\\openttd.exe -D >temp.log 2>&1 &");
resulting in nothing. No process opened, browser page just staying blank, temp.log being created but staying empty.

By the way I tried that line with exec() and system() and with the path "C:\OpenTTD\openttd.exe -D" and "C:/OpenTTD/openttd.exe -D" in all combinations, without success...

The idea behind that came to me when my Server crashed at the beginning of this week and I - studying in another city - was not able to start it again.

Does anybody know how to get it to work? How do other webclients start their programs? I hope there is a way, as the last option would be a remote-software...

Thanks!!
Connum
Last edited by Connum on 24 Feb 2007 21:48, edited 1 time in total.
English is not my native language, so please excuse me if I sometimes might appear a bit harsh or if I make a spelling or grammar mistake!
User avatar
skidd13
OpenTTD Developer
OpenTTD Developer
Posts: 522
Joined: 03 Mar 2005 10:49
Location: Germany

Post by skidd13 »

Don't know how the php-shell works, but give this a try.

create a batch-file (eg: start_server.bat)

Code: Select all

@echo off
SET OTTD_PATH=C:\OpenTTD

%OTTD_PATH%\openttd.exe -D >>%OTTD_PATH%\server.log 2>&1 &
and execute it via php

Code: Select all

shell_exec("CMD /C C:\\Openttd\\start_server.bat");
You can extend the batch-script, so it moves the last log to a new file.
What does that mean - the circumstances? I determine what circumstances prevail. -- Napoleon Bonaparte
---
If we cannot end now our differences, at least we can help make the world safe for diversity. -- John F. Kennedy
---
Our problems are man-made, therefore they may be solved by man. No problem of human destiny is beyond human beings. -- John F. Kennedy
fabca2
Transport Coordinator
Transport Coordinator
Posts: 312
Joined: 14 Apr 2004 15:18
Location: Fr

Post by fabca2 »

connum,
maybe there is a kind of protection to avoid executing something outside of the web folder...
imagine the consequence of a system("format c: ");

the idea is to make a batch file inside your wwwroot folder that maybe can run ottd ?
User avatar
NukeBuster
Traffic Manager
Traffic Manager
Posts: 222
Joined: 04 Jan 2006 18:16
Location: Alphen aan den Rijn, The Netherlands
Contact:

Post by NukeBuster »

You don't seem to use the "-D" flag on your command line. This is probably why the game starts, but the server doesn't.
NukeBuster

Transport Empire: The Transport Empire Linux effort
Join the Transport Empire IRC channel: [url]irc://irc.oftc.net/transportempire[/url] !

OpenTTD patch(es): Password at join
Connum
Engineer
Engineer
Posts: 127
Joined: 25 Dec 2006 17:05

Post by Connum »

NukeBuster wrote:You don't seem to use the "-D" flag on your command line. This is probably why the game starts, but the server doesn't.
oh well I did, I just forgot to post it above! ;) Thanks for that, I will adjust it...
Thanks skidd13, I even tried it with a batch file before... Still doesn't work! :(
English is not my native language, so please excuse me if I sometimes might appear a bit harsh or if I make a spelling or grammar mistake!
User avatar
Darkvater
Tycoon
Tycoon
Posts: 3053
Joined: 24 Feb 2003 18:45
Location: Hong Kong

Post by Darkvater »

You have to CD to the directory where the openttd binary is, or at least set the working directory there. Otherwise no data files will be found.
TrueLight: "Did you bother to read any of the replies, or you just pressed 'Reply' and started typing?"
<@[R-Dk]FoRbiDDeN> "HELP, this litte arrow thing keeps following my mouse, and I can't make it go away."
User avatar
XeryusTC
Tycoon
Tycoon
Posts: 15415
Joined: 02 May 2005 11:05
Skype: XeryusTC
Location: localhost

Post by XeryusTC »

Darkvater wrote:You have to CD to the directory where the openttd binary is, or at least set the working directory there. Otherwise no data files will be found.
Can't OTTD find that out itself? There are only a few lines needed for that, I had a <10 line implementation of it once.
Don't panic - My YouTube channel - Follow me on twitter (@XeryusTC) - Play Tribes: Ascend - Tired of Dropbox? Try SpiderOak (use this link and we both get 1GB extra space)
Image
OpenTTD: manual #openttdcoop: blog | wiki | public server | NewGRF pack | DevZone
Image Image Image Image Image Image Image
User avatar
NukeBuster
Traffic Manager
Traffic Manager
Posts: 222
Joined: 04 Jan 2006 18:16
Location: Alphen aan den Rijn, The Netherlands
Contact:

Re: OpenTTD webclient (PHP)

Post by NukeBuster »

Connum wrote:

Code: Select all

shell_exec("C:\\OpenTTD\\openttd.exe -D >temp.log 2>&1 &");
The ampersand ("&") you are using, at the end of your command, only works on Linux.

But perhaps, this link will be of any help: http://www.somacon.com/p395.php
It gives some solutions of how to execute a command in the background using php on a Windows machine.

Let me know if it lead to any better results.
NukeBuster

Transport Empire: The Transport Empire Linux effort
Join the Transport Empire IRC channel: [url]irc://irc.oftc.net/transportempire[/url] !

OpenTTD patch(es): Password at join
User avatar
skidd13
OpenTTD Developer
OpenTTD Developer
Posts: 522
Joined: 03 Mar 2005 10:49
Location: Germany

Post by skidd13 »

Darkvater wrote:You have to CD to the directory where the openttd binary is, or at least set the working directory there. Otherwise no data files will be found.
Adapted batch:

Code: Select all

@echo off
SET OTTD_PATH=C:\OpenTTD
SET CURRENT_PATH=%CWD%

cd %OTTD_PATH%
openttd.exe -D >>server.log 2>&1

cd %CURRENT_PATH%
NukeBuster wrote:But perhaps, this link will be of any help: http://www.somacon.com/p395.php
It gives some solutions of how to execute a command in the background using php on a Windows machine.
>>
Skidd13 wrote:

Code: Select all

shell_exec("CMD /C start_ottd_server.bat");
What does that mean - the circumstances? I determine what circumstances prevail. -- Napoleon Bonaparte
---
If we cannot end now our differences, at least we can help make the world safe for diversity. -- John F. Kennedy
---
Our problems are man-made, therefore they may be solved by man. No problem of human destiny is beyond human beings. -- John F. Kennedy
User avatar
NukeBuster
Traffic Manager
Traffic Manager
Posts: 222
Joined: 04 Jan 2006 18:16
Location: Alphen aan den Rijn, The Netherlands
Contact:

Post by NukeBuster »

Skidd13 wrote:

Code: Select all

shell_exec("CMD /C start_ottd_server.bat");
Won't work.
Also tested it to be sure ;)
http://www.somacon.com/p395.php wrote: For example, suppose one wanted to launch cmd from PHP, and then continue executing. The following examples do not work.
exec("cmd");
exec("cmd >NUL");
exec("cmd /c cmd");
exec("start /b cmd");
exec("runas cmd");

In each case, PHP waits for cmd to quit before continuing.
The reason why the page stays blank, is because php stopped executing your script, waiting for openttd to finish. What you want to do is start openttd and then run it in the background. this way all output wil go to temp.log file, php finishes executing and your page will be filled with information.

On the page mentioned, there are several workarounds for the problem.
NukeBuster

Transport Empire: The Transport Empire Linux effort
Join the Transport Empire IRC channel: [url]irc://irc.oftc.net/transportempire[/url] !

OpenTTD patch(es): Password at join
User avatar
imaginner
Engineer
Engineer
Posts: 62
Joined: 19 Nov 2006 07:38
Location: Poland
Contact:

Post by imaginner »

Darkvater wrote:You have to CD to the directory where the openttd binary is, or at least set the working directory there. Otherwise no data files will be found.
Is there any specific reason for that behaviour? Can't OTTD find it's working directory itself, as XeryusTC says?
Code needs love, like everything else.
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

I'm not really into the Windows stuff of OpenTTD, but there are a few options that will not work/are not feasible:
  • Using the registry to store the installation directory; this is impossible as there is actually no need to install it, if you unzip the OpenTTD archive it works (almost) out-of-the-box (you only need to copy TTD files). You will then be limiting OpenTTD to one installation, i.e. no MiniIN, trunk, 0.5.0, 0.4.8 installed in the same computer because when you've got more than one installation directory you do not know which one to use.
  • Searching the whole HDD for OpenTTD. Same issue with limiting OpenTTD to one installation per computer and not forgetting the fact that searching a whole HDD takes a lot of time, especially as you do not know where to look for the binaries.
There might be an API to find the location where the launched OpenTTD binary is located, but that makes it (almost AFAIK) impossible to run OpenTTD in the MSVC debugger.
Quark
Transport Coordinator
Transport Coordinator
Posts: 325
Joined: 20 Sep 2006 11:36
Location: Russia, Moscow

Post by Quark »

There is a way to get full path to your executable under Windows. And why it is impossible to run from debugger? Just change target directory for executable to where all your need files are or put links to data files into your build directory, or even run executable normally and attach debugger later.
Image
User avatar
XeryusTC
Tycoon
Tycoon
Posts: 15415
Joined: 02 May 2005 11:05
Skype: XeryusTC
Location: localhost

Post by XeryusTC »

Rubidium wrote:I'm not really into the Windows stuff of OpenTTD, but there are a few options that will not work/are not feasible:
  • Using the registry to store the installation directory; this is impossible as there is actually no need to install it, if you unzip the OpenTTD archive it works (almost) out-of-the-box (you only need to copy TTD files). You will then be limiting OpenTTD to one installation, i.e. no MiniIN, trunk, 0.5.0, 0.4.8 installed in the same computer because when you've got more than one installation directory you do not know which one to use.
  • Searching the whole HDD for OpenTTD. Same issue with limiting OpenTTD to one installation per computer and not forgetting the fact that searching a whole HDD takes a lot of time, especially as you do not know where to look for the binaries.
There might be an API to find the location where the launched OpenTTD binary is located, but that makes it (almost AFAIK) impossible to run OpenTTD in the MSVC debugger.
Look in argv[0], this always holds the path and name of the executable. A friend of mine found some functions that could set the execution path of the program to the path in argv[0] (after some stripping). This worked on both Windows and Linux IIRC.
Don't panic - My YouTube channel - Follow me on twitter (@XeryusTC) - Play Tribes: Ascend - Tired of Dropbox? Try SpiderOak (use this link and we both get 1GB extra space)
Image
OpenTTD: manual #openttdcoop: blog | wiki | public server | NewGRF pack | DevZone
Image Image Image Image Image Image Image
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

XeryusTC wrote:Look in argv[0], this always holds the path and name of the executable.
This is not true according to the C++ specification.
C++ specification wrote:If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (_lib.multibyte.strings_) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "".
User avatar
XeryusTC
Tycoon
Tycoon
Posts: 15415
Joined: 02 May 2005 11:05
Skype: XeryusTC
Location: localhost

Post by XeryusTC »

argv[0] is the first parameter (executable information) supplied. And from what I can interpret from your little quotation I'm actually right.
argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program
Disclamer: this is by my interpretation, yours can be different.
The name used here is the actual path of the program. It will also hold the executable name (thus the actual name). It could be that it includes the relative path instead of the absolute part though.

I also seem to remember from what I read on the internet and my C++ that argc is always 1 or higher because argv[0] always holds the path and executable name. The first actual parameter would be argv[1] then.
Don't panic - My YouTube channel - Follow me on twitter (@XeryusTC) - Play Tribes: Ascend - Tired of Dropbox? Try SpiderOak (use this link and we both get 1GB extra space)
Image
OpenTTD: manual #openttdcoop: blog | wiki | public server | NewGRF pack | DevZone
Image Image Image Image Image Image Image
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

My interpretation of it:
If argc is nonzero ... -> argv[0] does not (per specification) hold the application name when argc is 0, i.e. argc can be 0.
.... invoke the program or "" -> argv[0] can be "" when argc > 0.
DaleStan
TTDPatch Developer
TTDPatch Developer
Posts: 10285
Joined: 18 Feb 2004 03:06
Contact:

Post by DaleStan »

AFACT, when argc is 0, argv[0] is not necessarily even addressable. When argc is non-zero, argv[0] contains something, but it is not guaranteed to be useful. In Windows, it is what the user typed on the command-line (usually relative), or in the Target box of the shortcut. It is, however, possible to use a single on-disk executable to run multiple copies of the game out of different directories (with different working directory settings), and this must be preserved.
To get a good answer, ask a Smart Question. Similarly, if you want a bug fixed, write a Useful Bug Report. No TTDPatch crashlog? Then follow directions.
Projects: NFORenum (download) | PlaneSet (Website) | grfcodec (download) | grfdebug.log parser
Quark
Transport Coordinator
Transport Coordinator
Posts: 325
Joined: 20 Sep 2006 11:36
Location: Russia, Moscow

Post by Quark »

It's easy to preserve and allow automatic locating of data directory. First, look for data files in working directory — if they not found, then look in executable directory.

On Windows you can use this:
MSDN wrote:• To obtain the command-line string for the current process, use the GetCommandLine function.
• To obtain the full path and file name for the executable file containing the process code, use the GetModuleFileName function.
MSDN wrote:argc
An integer that contains the count of arguments that follow in argv. The argc parameter is always greater than or equal to 1.

argv
An array of null-terminated strings representing command-line arguments entered by the user of the program. By convention, argv[0] is the command with which the program is invoked, argv[1] is the first command-line argument, and so on, until argv[argc], which is always NULL.

The first command-line argument is always argv[1] and the last one is argv[argc – 1].

Note
By convention, argv[0] is the command with which the program is invoked. However, it is possible to spawn a process using CreateProcess and if you use both the first and second arguments (lpApplicationName and lpCommandLine), argv[0] may not be the executable name; use GetModuleFileName to retrieve the executable name, and its fully-qualified path.
Image
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Post by Rubidium »

Quark, that just says that for Microsoft's C++ compiler argc is always 1 or bigger. It doesn't say that argc is for all compilers/systems 1 or bigger nor that all compilers/systems actually have the command line in argv[0] (the spec says that it may be "").

So even though XeryusTC statement might hold for Windows (i.e. MS C++ compilers), it does not necessarily hold for all compilers/systems, which was the point I tried to make together with "never assume something holds for all cases when you've tried only two cases".
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 22 guests