Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 14 in printed book and digital formats -- plus a one-year subscription so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

PDF:

Yuma Development

Connecting to Other Servers From Yuma

Issue: 7.3 (March/April 2009)
Author: Brad Weber
Article Description: No description available.
Article Length (in bytes): 6,601
Starting Page Number: 50
RBD Number: 7315
Resource File(s): None
Related Web Link(s):

www.YumaDev.com

Known Limitations: None

Excerpt of article text...

Yuma is a web development platform for REALbasic developers. In my previous Yuma columns, I've focused on features that are resident on the Yuma server, including session management and HTML form POST processing. But Yuma provides tools that you can use to reach beyond the resources of your server. In this column I've explored some options for integrating with services on other servers. Database Access Your Yuma scripts can connect to several types of databases when building dynamic database-driven pages, including MySQL, PostgreSQL, and SQLite (REALSQLDatabase). But, at least in the case of MySQL and PostgreSQL, those database servers don't need to be running on the same server that is serving your Yuma file. You'll use the Host property of the Database class to connect to the appropriate server. And you're not limited to a single connection or even a single type of server in your Yuma document, as you'll see in the code listing below: <?yuma dim mysql as new MySQLDatabase mysql.Host = "server.YourCompany.com" mysql.DatabaseName = "accountingDB" mysql.UserName = "yuma" mysql.Password = "money" dim postgres as new PostgreSQLDatabase postgres.Host = "anotherServer.YourCompany.com" postgres.DatabaseName = "marketingDB" postgres.UserName = "yuma" postgres.Password = "sizzle" dim sqlite as new SQLiteDatabase sqlite.DatabaseFile = GetFolderItem("customerDB.sqlite") if mysql.Connect and postgres.Connect and sqlite.Connect then ... ?> Your script might serve as an aggregator of data across those three databases. Once connected, you are free to interact with them as you would a resident database on the same machine, adding, updating, deleting, or just retrieving data. Remember that there will be a speed penalty related to network performance when connecting to remote databases. Performance will be best when connecting to a database on the same server. It may be somewhat slower to connect to another server on the same network. It may be noticeably slower to connect to a remote server. Also note that remote MySQL and PostgreSQL servers you access must be configured to allow connections from remote users (IP addresses), which is not the default for either application. HTTP Socket The HTTPSocket class in the Yuma framework allows your script to communicate with HTTP servers as if it were a browser. Why would you want to do that? You'll probably think of plenty more. But I've got a few to spark your creativity. First, if you run several web servers, you might want to monitor their availability. To do so, you can add to your script HTTPSocket calls to your other servers and verify that you get a reply. For that, and the rest of the examples, it will be handy to have a function to get the contents of a URL. <?yuma Function getURLContent(url As String) As String dim sock as new HTTPSocket if not sock.get(url) then return "" return sock.reply End Function ?> To check server availability, verify that the reply from a server is not empty. <?yuma if getURLContent("http://www.YumaDev.com/").Len > 0 then print "The server is available." else print "The server is unavailable." end ?> Second, you might want to monitor the web pages of your vendors, competitors, or news outlets so you know when they've changed. That will allow you to react more quickly to product announcements, industry news, etc. In that case, it's not enough to know that the server is running. You'll also want to analyze the response to detect a change since your last request. Compare the HTTPSocket reply to something you've cached on disk or in a database from the previous request. The MD5 function in Yuma produces an MD5 checksum of a larger string (like the HTTP server reply). The compact checksum requires much less storage space than the complete response and makes it easy to use the standard string comparison features to know whether or not two replies are equal. <?yuma // I've split these operations into several lines // so they fit in the column width of the magazine. dim url As String = "http://www.InspiringApps.com/" dim fileName As String = url.ReplaceAll("http://", "") fileName = fileName.ReplaceAll("/", "|") fileName = fileName.ReplaceAll(".", "_") dim cacheFile As new FolderItem(fileName) dim cacheCheck As String if cacheFile.Exists then dim cacheIn As TextInputStream cacheIn = cacheFile.OpenAsTextFile if cacheIn <> nil then cacheCheck = cacheIn.ReadAll cacheIn.Close end end dim reply As String = getURLContent(url) dim replyCheck As String = MD5(reply) if strComp(replyCheck, cacheCheck, 0) = 0 then // No change print url + ": No change" else print url + ": Changed" // Update the cache if cacheFile.Exists then cacheFile.Delete dim cacheOut As TextOutputStream cacheOut = cacheFile.CreateTextFile if cacheOut <> nil then cacheOut.Write(replyCheck) cacheOut.Close end end ?> Finally, you might want to embed in your website some content from another. While I intended the first two applications to be diagnostic and more behind-the-scenes, this final technique is useful to present customer-facing data on your site. <?yuma // Get the first TECH headline from the CNN // website to display in our own. dim fullPage As String = getURLContent("http://www.cnn.com/") dim r As new RegEx dim m As RegExMatch // Identify the appropriate portion of the // page with a regular expression pattern. r.SearchPattern = "<div class=""cnnHeaderLnk"">_ + "<a href=""/TECH/""><span>Tech</span> >></a>"_ + "</div>\s*?<ul> <li>.*?<a href="".*?"">(.*)</a></li>" m = r.Search(fullPage) if m <> nil then // Print the found headline print m.SubExpressionString(1) end ?> The code will display somewhere on our site the first "Tech" headline from the main page of the CNN website. Note that there is a risk involved in this approach. If the site from which you are pulling content changes its design, you'll need to update your script. Before using the techniques described in this column to retrieve data from third party servers, please be a good citizen and read the Terms of Use policies for those sites. I hope this column helps expand your Yuma horizons! Please visit www.YumaDev.com for product documentation, community discussion forums, and an occasional blog entry about Yuma development.

...End of Excerpt. Please purchase the magazine to read the full article.

Article copyrighted by REALbasic Developer magazine. All rights reserved.


 


|

 


Weblog Commenting and Trackback by HaloScan.com