Develop Web services with XML-RPC: XML-RPC Middleware
English original
content:
Middleware: A loose coupling of application components WEB account: an actual example PHP client does not have a precedent reference material about the author
related information:
Develop Web Services with XML-RPC: Getting Started for Perl Using SOAP :: Lite with Perl
There is also a Web service area:
Teaching tools and products all articles
Use XML-RPC in Web Services Part 2
Joe Johnston
(Jjohn@cs.umb.edu) Senior Software Engineer, O'Reilly & Associates 2004 April
Popular web applications often make their hardware resources that their hardware resources are overwhelmed. By using a web service middleware, developers can create an application architecture that is divided into logical components that are connected by middleware so that performance bottlenecks can be easier to eliminate. All this is done by simply adding better processing in the problem area. XML-RPC is a simple Web service protocol that is used to build middleware.
Middleware: Loose coupling of application components in my previous article (see Resources), I introduced XML-RPC (see Resources) As an easy way to perform functions on remote machines. However, single single function is not enough to make this protocol worth learning. We will see an interesting web service application combined with XML-RPC: Middleware (Middleware). In its most basic level, the middleware is just an abstract method of accessing the resource by using the application programming interface (Application Programming Interface, API). The API is used to simplify the details of the software resource, such as the user verify the database or file system. For example, the user verifies the database or file system. Obviously, users who call the API will benefit from this abstraction because the details of the underlying implementation are hidden in them. It may not be so definitely that the implementation of the API can also benefit from this abstraction because they can fundamentally change the underlying library code, and do not destroy the use of old implementations. This independent software library that enhances and calling code is considered to be loose-coup. If the call code ignores the API and processes the data structure in the library code, this independence will be lost because changes in the library implementation may destroy the client of the behavior. Such systems are called tightly-coups. Good software engineering practice requires loosely coupled systems in all possible places. API This idea can surpass the programming library to expand into greater concepts. Of course, an application that allows the user to graphically modify the database content itself must be able to handle raw data. A database like MySQL and PostgreSQL provides a C interface to achieve this. If this hypothesis is directly using the local interface of a specific relational database management (RDBM) system, the transition to a new RDBM system will include all the code to override all the old system. By introducing an intermediate part layer as a proxy between all communication between the display logic and the routine that needs to handle a specific underlying database (as shown in Table 1), the programmer can fund an fundamental change in any end of the API fence without Corresponding changes at the other end. Figure 1: Typical middleware settings WEB account: A actual example According to this short theoretical background, let us use XML-RPC middleware to solve a problem in the real world. Imagine a Web site that requires a user account login. Of course, Apache's built-in HTTP verification can be used to store the user's name and password. Under normal circumstances, once the user is verified, the other account-specific data must be available, and they must also be stored. This is why the account information is stored in the RDBM system like MySQL (see Reference). If the front end is written in Perl or PHP, then using the CGI script hardcodes to access mysql is very tempting. However, doing so allows display logic and specific RDBM systems. This is the land of the middleware. Middleware Creates a flexible system, in which the underlying RDBM can be seamlessly changed, and the cluster is allowed to replace a single database server. Thus, loose coupling created using middleware is the key to creating a scalable web application architecture. In order to build a middleware bridge between the front-end CGI display code and the backend database and the service logic code, you must define an API that allows all required data to access. Table 1 shows the XML-RPC middleware API of the user account data stored in this application.
The front-end code can only be visible to the underlying data by calling these XML-RPCs. Table 1: XML-RPC API of account information
Host: http://marian.daiSypark.net/rpc2 port: 1080 Procedure Description Process Name Input Output Description Authenticate
If Authenticate () fails, send a failed message, the user is given the opportunity to log in again. Listing 2 does not come from XML-RPC, but from SQL. The reader should have been familiar with the DBI module of Perl. If you are not familiar with, please read the MySQL homepage (see Resources), where a complete introduction to using this module is provided. After pulling out all the libraries on line 4-7, a global DBI handle is created, $ dbh, all subroutines will use it to access the database. When the script terminates and ensures that the end () subroutine is executed when there is no DBI error message that is not explicitly separated by the database. In line 15-22, the name of the released API program is associated with the Perl function that implements their PERL. Line 36 shows how the verification is done. This function requires two scales; check them to ensure that at least their size and form are required. Query SQL Table Users to see if the credentials match. Listing 3 gives the SQL code for creating this table. Listing 3: Sqlcreate Table Users for creating user tables
Username char (12) Not null default '',
Password char (13) Not null default ',
Fullname Char (50) Not Null Default '',
Points Int default 0,
PRIMARY Key (username),
Index (Fullname)
);
Prepare a SQL statement to find rows that match the user name. Because the username is the primary key, there will be one line of matching. A column returned as "Password" and it is a string of DES encryption. Use Perl's Crypt () function DES encrypted the transferred $ Password and then compare this scalar with the table. If these strings match, record this event in another MySQL table through the logger () function as a log, and generate a new session ID. The New_Session_ID () function of line 210 is only inserted into the sessions table. Listing 4 gives the SQL code for the creation session table. Listing 4: Creating SQL of the session table
Create Table sessions
ID Int Auto_Increment Not Null Primary Key,
Username char (13),
Issued TimeStamp
);
The session ID is just an auto_increment field. Create a new, unique id, and a timestamp that occurs when inserting when inserting insertion. MySQL clever place is that the mysql_insertid attribute using the DBI handle can easily determine the most recently used insert ID. Then this integer returns to the XML-RPC client. Since the secret of how to generate a session ID has been unspeakable, let's take a look at the function using it. At 66 line, get_account_info () begins. It is expected to pass to it an effective session ID. Recalling, the session ID belongs to Table Sessions, but the desired information is stored in the UserS table. It is necessary to get this information by using the SQL Join operator. Sessions and Users have a field called "Username". You can also find the username (username) with the session ID in Sessions. Then, use this username (username) to find rows that contain the username (Username) in Users. If the lookup is successful, the row can be retrieved as a hash reference to return to the caller. If this SQL is a bit difficult to understand, use two small SELECT calls, and use Perl hash to connect to the line. The final function is set_account_info (), and it needs a structure. In Perl, this XML-RPC structure represents a hash reference. Because this function will be INSERT or UPDATE, lock the user table to prevent race condition between the two SQL operations. Ideally, we can use the latest version of MySQL because it supports transactions, but there are many previously installed MySQLs, and how it is useful to handle transactions in this RDBM. The number of rows returned by the DO () method of the DBI is affected by the specific SQL statement. This return value can be used to speculate if it is necessary to update or create an account. Keep in mind that only existing users can change their full name or score. "Username" field is a primary key, which should change it in a more controlled manner. It can be proved that the password can be changed here, but it seems to be better in another function. Once updated, unlock this table and use Logger () to record this transaction. If you need to create a new account, the table will be unlocked, and this information is inserted after the encrypted password. In addition, the success or failure of this operation is returned to the XML-RPC client. Without a precedent, if the number of lines of the SQL, XML-RPC listeners and clients are calculated, the code of this middleware system is estimated to be less than 600 rows. XML-RPC is a powerful and simple messaging system that exceeds the application described in this article. The successor of XML-RPC is a Simple Object Access Protocol (SOAP) (see Resources), which extends some of the features you see here. I wrote the next series of articles will be concentrated in SOAP. To get more information about XML-RPC, you can view its home page (see Resources). Reference
Check the first article involving web services, XML-RPC and Perl: Getting started for Perl XML-RPC. Also read his third article, use soing soap :: lite with perl. Access the XML-RPC home page to get a complete understanding of XML-RPC. Review the PHP document. Accessing the MySQL home page can find more complete information. Learn more about SOAP by reading the SOAP specification. About author
During the day, Joe Johnston (Jjohn@cs.umb.edu) is a programmer of O'Reilly Labs (a new department of O'Reilly and Associates). As long as his cat is sitting on his keyboard, he wrote articles for the Perl Journal, Use.perl.org, www.perl.com and O'Reilly Network. He and Michael Lord have created a desired UFO folklore site, aliens, aliens, aliens.