Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Build Simple Asynchronous Pluggable Protocols for Windows : Page 2

You're not limited to the familiar asynchronous pluggable protocols (APPs) such as ftp://, http://, and javascript:// that are built into Windows; you can implement your own protocols, add them to Windows' recognized protocol list, and then use them from any application—and you can implement your protocol in any language. This article uses the Rebol scripting language to create a protocol that works similarly to the javascript:// protocol, compiling and evaluating arbitrary Rebol statements and displaying the results.


advertisement
A JavaScript Alternative: Rebol
Most protocol implementations are done with a particular goal in mind, however there are certain protocols, such as the JavaScript protocol, that are designed to extend the functionality of the system as a whole, and the browser in particular.

It is this latter type that I will build in this article, using the JavaScript protocol as a model. The JavaScript protocol allows you to transmit JavaScript code that will then be evaluated within the browser. This means that the objects available to the protocol are the objects exposed by the browser. In our earlier example, the alert() functioned because alert() is supported in the browser environment.

Figure 2: Security Check. Rebol's default security asks you before it opens a port to our protocol interpreting script.
I'm willing to bet there are a lot of folks out there, like me, who like scripting languages but abhor JavaScript. Using the same technique of dynamically loading a script in an .exe, as shown in the ws-proto example, it should be possible to make a protocol handler that accepts script in your favorite scripting language and evaluates it. This opens up the possibility of bookmarklets written in languages such as Python and Perl, as well as the usual VBScript and JavaScript. In the next part of the article I will make a protocol handler that accepts script in my favorite scripting language, Rebol.



Why Rebol? Because Rebol is particularly good at handling protocols. By implementing a dynamic evaluating protocol in Rebol, I can very easily add functionality to interact with ftp, smtp, http, etc. (These protocols are easily accessible from Rebol using Rebol's one-liner scripts, e.g. one line of code to read a URL and write the output to a local file.) Rebol can interact with any protocol that can be called via bookmarklet-type behavior, from a link on a web page, from writing in the address bar, from shortcuts, or from most any programming language/environment in the Windows system.

Figure 3: Rebol View. The large text area contains the URL body you passed by clicking on the link.
You can download a free, noncommercial version of Rebol from http://www.rebol.com/downloads/view-pro031.zip. It includes a Rebol dialect (sort of a DSL) for graphical interfaces, which I will use to implement a small form in my script. This form will help me decide, by showing the code passed to it, whether to evaluate the code, append it to a local file containing the script fragments I receive, or to edit it and then evaluate it.

It's time to implement the Rebol script. There are links at various points to the Rebol function dictionary, which provides definitions of built-in Rebol functions to help you along. In the code download for this article there is a registry file called rebsettings.reg, which sets the reb protocol to be evaluated using a script called reb.r. Reb.r is the protocol handler that the registry files point at (see Listing 1). Install Rebol in the default installation folder c:\rebol\view. Be advised that the Rebol installation program asks you for information such as your email address, email server etc., which it needs to send and receive mail and for the examples to work.

Once Rebol is installed, you should merge the accompanying .reg file and also install the files in the downloadable sample code (see link in left column) to the c:\rebol\view directory. When you've done that, open the example.html file in your default browser. Listing 2 shows the contents of the example.html file.

This file has three simple JavaScript functions called callreb(),browseloc(), and insertPrompt().

  • Callreb() takes a string parameter, which should be a reb:// link, and then uses the browser's location.href method to navigate to the reb:// link.
  • When executed in a link, browseloc() will tell Rebol to browse the page on which the link was executed.
  • The insertPrompt() function accepts three arguments and builds a string containing the reb:// protocol. The middle argument is the result of a JavaScript prompt that gets a user-entered value and places it in the middle of the reb:// protocol.

The first link in the HTML page is:

<a onclick="callreb('reb://write %newfile.txt read %oldfile.txt')">
read a local file and write result to new local file</a>

Everything past the reb:// is legal Rebol code. And it does exactly what the link text tells you: It reads a local file, contained in the code download called oldfile.txt and writes a local file called newfile.txt. (Note that the parameters are intentionally opposite their function—this is because of the evaluation order of Rebol syntax). The % before the file names tells Rebol that these are in fact files. When you click on the link you should see something similar to Figure 2:

The popup is generated by Rebol's built-in security (more information on controlling Rebol security settings). When you click Yes, reb.r loads (see Figure 3).

Pressing Do causes the Rebol interpreter to evaluate the text contained in the <textarea> tag, which is the value of the protocol body. The little field at the top of the form will display the string "No string output returned." This is because as written the script fragment you just wrote doesn't return a value. As a quick test go to the text area, erase the text that's there, and write the following:

1 + 5

1 + 5 is valid Rebol code so if you press Do the text field should read 6. If you wrote the following in the text area:

1 + 5 > 7

the text field would display false. If your wrote 1 + 5 > 4 it would display true. And if you wrote:

(1 + 5) * 7 + 6

the text field would display 48. You can find more information about Rebol Math at http://www.rebol.com/docs/core23/rebolcore-11.html.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date