Smart CODE | |
Your on-line guide to the generated code |
Fetching Web Pages. A worked example |
So far we have been writing traditional programs, and illustrating techniques
for keeping the application code separate from the interface.
The next two examples show how you can extend the range of your application and use resources that are out there on the internet,or in your local Intranet, as easily as you can access and use files on your local machine. The examples only use HTML resources, ie URLs available from a web server. However the technique can be used for fetching any data that is served across the internet, from stock prices, to images and movies. Also where a server offers a form-based query mechanism, such as a search engine or on-line translation, there is no reason why your program can't perform the interaction. Interacting with web servers, and accessing information is not the sole domain of the web browser. This example will create a URL fetcher. The interface will have three text fields, so that you can set up your proxy host and port if you are behind a firewall - and of course you can enter the URL you want to fetch. The Fetch button has the smart callback. When you press the button, it will connect to the appropriate web server and download the URL. In this example, the routine you provide to handle the incoming URL simply drops it raw into a textwidget. In the next example, we parse the incoming HTML and display only the links. |
Environment Variables etc | |
---|---|
As always, you should make sure that $XDROOT/bin is in your path.
Here is a summary of the variables you will need to set up before working through the example | |
XDROOT | |
LM_LICENSE_FILE |
Step 1 | Create a new directory called, for example, htmlfetch |
Step 2 | Change directory to htmlfetch |
Step 3 | Run xdesigner using the xdreplay script $XDROOT/src/examples/sc/fetch.vcr, ie
|
This will create the example application, generate the code, copy standard versions of the callbacks you would have written into the appropriate place, and invoke make to build the program. |
First, check that you have set up the environment variables. If there is a
problem, check that they are set correctly, eg
|
You run the example application by typing
You may want to resize the program so that the text data area is sufficiently large to show the HTML when it is accessed. If you are behind a firewall, you should set the proxy field to the name of your proxy, and the port field to the port number, just as you would to configure a proxy for your web browser; To fetch and process the URL, type the full URL, eg:
into the URL field and press the Fetch button.
The raw HTML is placed in the text widget. |
Creating the Interface | |
---|---|
Step 1 | Create an Application Shell, a form, a button, three text fields, and one text widget. and text field |
Step 1a | You may like to add three labels, that say Proxy, Port and URL, so you can tell the three text fields apart. |
Step 1b | You may also like to use the layout editor to set up your form so that when you resize the dialog, the text widget is the component that grows. We intend to fill it with HTML. |
Step 2 | You should name the three text widgets proxy, port and url - and name the text widget data |
Step 3 | Click on the first text field, then Shift-click on the other text fields and the text widget, so they are all selected |
Step 4 | Select add to new group from the toolbar. Close the group editor when it appears. |
Step 5 | Select the button, bring up the callbacks dialog, name the callback doit_callback, select Smart/Code- Internet. Then press the group button, and in the Group Editor make sure the group is selected, and press apply. |
Step 6 | The customiser button is now sensitive. Press it to access the Internet properties page. when it appears. |
Step 7 | Set the Style to Receive Only (GET URL) |
Step 7a | Set the Proxy Host to a function called myProxy |
Step 7b | Set the Proxy Port to a function called myPort |
Step 7c | Set the URL to a function called myURL |
Step 7d | In the Custom Data Handlers frame, set the Recieve (URL Content) field to your own function called myData |
Step 8 | Apply these changes, closing the properties page, and then press the
add button in the callbacks dialog.
Close the callbacks dialog. |
Step 9 | Use Generate/generate... from the menu bar to get to the code generation
page. Make sure that you are generating C, that you are generating to the
right directory, and that the default toggles are set including the
Stubs generation toggle Press the generate button. |
The Access Functions | |
---|---|
In the previous examples we have set the Internet properties to constant
values. In a real program, you would rarely want to hard-wire such information
as proxy/port usage and the server to connect to as a string. If you make them
functions instead, a getter/setter pair of stubs are generated for you in your
callouts directory, and these are used by the internet connection layer.
using the getters and setters, these functions can be wired up to the interface text fields trivially: | |
Proxy Host |
|
Proxy Port |
|
URL |
|
Processing the URL data | |
---|---|
In the Internet Properties dialog, you overrode the default
data handler to give yourself full control of the incoming data.
A stub is generated that gives you access to
The code you add is quite simple:
|