![]() |
| [[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]] |
|
|
#1 |
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
Hi all,
To analyze a real-time application you typically have to write a custom solution. Think about a developer console, or a transparent head-up display (HUD). But that can be quite a bit of work even it you try to keep things simple. The poor man's option is often to use a log file to output diagnostic information, and a configuration file for input parameters. But that's limited to just text and clearly not interactive... So what I'd like to present here is a solution that is as powerful as a console & HUD, but almost as simple as a log & configuration file. The key is to use existing web technology. By letting a browser take care of the interface rendering you get a lot of possibilities, just by writing a bit of HTML code. To make it interactive, you need an embedded server that will serve the web page and responds to input. So were do we find a simple embeddable server? Actually, it's really easy to write our own! The first thing we need to do is to open a network socket and make it listen for TCP/IP connection requests. In Windows, we can use Winsock for this (similar API's exist for other platforms). Note that you'll have to link in Ws2_32.lib before you can make use of Winsock. The code for opening a socket looks like this: Code:
Code:
Once accept returns, you can start exchanging data with the browser! You only need to know two more functions; recv and send. As the names already imply, recv will wait for a request from the client, and send can be used to reply. All communication happens using simple byte strings, and the HTTP protocol uses messages that are fairly easy to read. So let's receive our first request message: Code:
GET /panel [...] The [...] is a bunch of additional information the browser decided to send us, like the browser name and language, and what formats it accepts. So let's reply with "Hello World!": Code:
The first line of the response message tells the browser that the request was understood and this message contains the reply. The second line tells it the reply consists of html code, encoded using UTF-8 (a popular ASCII extension that can also represent Unicode). This is followed by the length of the body. Next is another carriage return and newline to separate the headers from the body of the message. And finally we have the HTML code (not standards compliant, but most browsers will take it anyway). The send function also takes the client socket and the total message length as a parameter. From this point forward the possibilities are almost endless. HTTP and HTML are fairly simple so I'll leave it up to you to decide what you want to do and explore tutorials and documentation. To make things interactive you probably want to create a form with a submit buttom. This is what SwiftShader 2.0 does. Just run the demo and point your browser to http://localhost:8080/swiftconfig (and have a peek at the HTML code if you're not sure how it's done). With a form the browser will send the server a "POST" request, containing the enabled checkboxes and selected drop down box elements. Beware that some browsers will send that in a second message. After parsing just resend the entire page with appropriately selected controls. Note that the essential code is really just a few dozen lines, hardly any longer than for reading and writing configuration and log files. You now have all the power of HTML at your fingertips to create any interface you like. You can even change your application's behavior remotely (try with your smartphone)! You can also get back diagnostic information from your application. However, when you use forms you'll have to click a button to get updated information. That can be annoying, especially when you're waiting for a specific event to happen. Also, forms require redrawing the entire web page. So isn't there a way to automatically get regular updates, without reloading the entire page? Absolutely, but for this we'll need JavaScript. So make sure you're fully comfortable with using HTTP and HTML first. The key to dynamically update your web page is the use of the XMLHttpRequest JavaScript API. It can be used to make an HTTP request from within JavaScript, and the response can be plain text or XML. The latter case gave birth to a suite of techniques known as Ajax, but here I'll keep things simple by using the plain text version to create a framerate counter. Here's the essential JavaScript code for making a request: Code:
Code:
So we want to assign that string to a certain HTML element where the framerate should go. This can be achieved by navigating the Document Object Model. The DOM is a tree-like structure representing all the elements of an HTML (or XML) document. In this case we locate the element that has the "fps" id and we overwrite the HTML contained within that element with the server response containing the framerate as a string. Finally, we also make the script call the request function again after 1000 milliseconds. This way you'll see the framerate get updated automatically every second. Note that this is a 'pull' approach. If the sever has some updated data sooner it can't send that to the browser before it asks for it. However, an alternative solution is to immediately make the next request, and have the sever send it when ready. Anyway, on a local network you can have hundreds if not thousands of updates per second without stressing anything. So finally lets put it all together: Code:
That said, this code may still not work under all circumstances. The XMLHttpRequest API has only recently become available on all popular browsers, so you may want to update your browser to the latest version or look for backward compatible implementations. I also didn't add any kind of error checking, so if you need this to be more robust check the documentation of the used APIs. And of course you may want to extend this with convenient methods to extend the HTML code with certain controls and asynchronously updated diagnostic information. You can even update images to create things like graphs... Either way I hope I've convinced you that with relatively little code you can get access to a wide range of possibilities to diagnose and control you application with a neat interface. Please let me know if you use this anywhere, or if you know any other web based technology that is useful during development! Cheers, Nicolas "Nick" Capens Last edited by Nick : 11-03-2009 at 03:06 PM. |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
A big thanks goes out to the people in the Framerate in a browser thread for helping me get started with asynchronous updates!
|
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Sep 2005
Location: .nl
Posts: 505
|
Very inspiring, thanks!
Offtopic: I never bothered to do more Javascript than an occasional assignment for my study, but I'm very surprised that it allows (or forces?) things like "xhr.readyState == 4". Now that is ugly! Last edited by roel : 11-03-2009 at 09:15 AM. |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
Oh my God, so application analysis is what it was all about :> Although I really like the nice tutorial on implementing quick-and-dirty dynamic web servers I really don't think it's worth it given that creating another form with .NET (for example) takes two lines of code. As always though, you manage to demonstrate a very large number of concept within a few lines and you do it very clearly. And I'm serious on it, it's not just blabla nice job blabla.
|
|
|
|
|
|
#5 | |
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
Quote:
You may want to add a clarifying comment though... |
|
|
|
|
|
|
#6 | ||
|
Senior Member
Join Date: Aug 2004
Location: Ghent, Belgium
Posts: 1,056
|
Quote:
But yeah, personally I'm using it to analyse and control real-time applications. ![]() Quote:
|
||
|
|
|
|
|
#7 | ||
|
Senior Member
Join Date: Jan 2003
Posts: 868
|
Quote:
Yes, it certainly adds another perspective to browsing and abstracts the idea of "what" is being browsed. In that sense, it's an innovative and therefore interesting work. Besides, it's implementable with a few strokes. Quote:
I was about to point at X11 before reading the "not with two lines". After reading, I agree, although technologies are evolving fast and it's certainly possible (using a decent framework) to achieve the same although it will not reach that many people as your solution. I'm personally interested in trying it out with Lynx. ![]() |
||
|
|
|
|
|
#8 | |
|
Senior Member
Join Date: Sep 2005
Location: .nl
Posts: 505
|
Quote:
|
|
|
|
|
|
|
#9 |
|
Valued Member
Join Date: Dec 2007
Posts: 107
|
I see this as a tremendous debugging utiliy, you run your engine on the main pc and with an internet connection you can debug on another pc connected trhough the net , both local or global, brilliant...
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
|