Analysis of Hessian usage in PHP

Source: Internet
Author: User
This article provides a detailed analysis of Hessian's use in PHP.

This article provides a detailed analysis of Hessian's use in PHP.

What is Hessian?
Hessian is an open-source remote communication protocol provided by caucho.
Using the binary RPC protocol, the U.S. server is based on HTTP transmission, and the server does not need to open a firewall port.
The protocol specification is public and can be used in any language.
Use the Client/Server mode.
A request is a client, a website space, and a service provider is a server.
The client calls the process to send a call with process parameters to the service process, and then waits for the response information.
On the server side, the process remains asleep until the call information reaches.
When a call Information arrives, the server obtains the process parameters, calculates the results, sends the reply information, and waits for the next call Information. Finally, the client calls the process to receive the reply information,
Obtain the process results, and then call the execution to continue.

Hessian protocol Workflow
Client Requests Server Functions
1. Call the client handle to execute the transfer parameters.
2. Call the local system kernel to send network messages.
3. Send messages to the remote host.
4. The server handle obtains the message and obtains parameters.
5. Execute the remote process.



The server function returns the result to the client.
1. the execution process returns the result to the server handle.
2. The result returned by the server handle is the Hong Kong Space that calls the remote system kernel.
3. The message is sent back to the local host.
4. The client handle is used by the kernel to receive messages.
5. The customer receives the data returned by the handle.

Source code

1. reference the configuration file, including the website root directory and the Hessian address.

The Code is as follows:


/**
* File name: config. php
* Purpose: Hessian configuration file
*
* @ Package system. core. code applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/

// Root directory
Define ('path', dirname (_ FILE _). DIRECTORY_SEPARATOR );

// Hessian Url
Define ('hessian _ url', 'HTTP: // qx.com/server.php ');

// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>


2. Configure the server.

The Code is as follows:


/**
* File name: server. php
*
* References:
* 1. (Hessian homepage)
* 2. (Hessian PHP)
* 3. (Hessian PHP open source)
* 4. (Singleton Mode)
*
* @ Author wu1_qing
* @ Package system. core applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
Require_once (dirname (_ FILE _). DIRECTORY_SEPARATOR. 'config. php ');
Require_once (PATH. 'Extensions/HessianPHP/HessianService. php ');

Class HessianServer
{
Public function _ construct (){}
/**
* Product details APi Interface
* @ Param string $ title
* @ Param int $ price
*/
Public function goodsInfomationApi ($ title, $ price ){
$ Price = (int) $ price;
Return 'Call a remote method using the Hessian protocol. title: '. $ title .'
Price: '. $ price;
}
}

$ Server = new HessianService (new HessianServer ());
// $ Server-> displayInfo ();
$ Server-> handle ();

// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper
?>


3. You can use the displayInfo method in the HessianService class to view how many communication methods are enabled.
If you need to use the handle method to set up the server, if the prompt "Hessian Requires POST" appears, the server has been set up successfully.

4. encapsulate the Hessian Interface

The Code is as follows:


/**
* Class Name: HessianApi
*
* References:
* 1. (Hessian homepage)
* 2. (Hessian PHP)
* 3. (Hessian PHP open source)
* 4. (Singleton Mode)
*
* @ Author wu1_qing
* @ Package system. core applied to the whole site
* @ Copyright Copyright (c) 2012
* @ Since 1.0
*/
Class HessianApi
{
/**
* @ Var string interface address
*/
Private $ _ url = NULL;

/**
* @ Var result handle
*/
Private $ _ handle = NULL;

/**
* @ Var array stores the singleton mode array
*/
Private static $ _ objects = array ();

/**
* Set the URL address
* Instantiate the HessianClient class
* Parameter: (1) url, 2
*
* 2. Java call Field
* @ Param string $ url
*/
Public function _ construct ($ url)
{
$ This-> setUrl ($ url );
$ Handler = new HessianClient ($ this-> getUrl (), $ this-> getOptions ());
$ This-> setHandler ($ handler );
}

/**
* @ Return result $ _ handle
*/
Public function getHandler (){
Return $ this-> _ handle;
}

/**
* Set the handle
* @ Param result $ _ handle
*/
Public function setHandler ($ _ handle ){
$ This-> _ handle = $ _ handle;
}

/**
* Obtain the URL
*/
Public function getUrl (){
Return $ this-> _ url;
}

/**
* Set the URL address
* @ Param string $ url
*/
Public function setUrl ($ url ){
$ This-> _ url = $ url;
}

/**
* TypeMap maps platform objects such as Java
* @ Return array
*/
Public function getOptions (){
Return array (
'Version' => 1,
'Saveraw' => TRUE,
'Typemap' => array (
'Javanullpointexception '=> 'java. lang. nullpointerexception ',
'Stacktraceelement' => 'java. lang. stacktraceelement ')
);
}

/**
* Record interface call Information
* @ Param string $ method call method
* @ Param string $ returnMsg the text information to be recorded in the log
*/
Public function resultLog ($ method, $ returnMsg)
{
$ LogPath = PATH. '/runtime/hessian /';
If (! Is_dir ($ logPath )){
Mkdir ($ logPath, 0777 );
}
Error_log (date ('ymd H: I: s', time ()). '| '. $ method. '| '. $ returnMsg. "\ n", 3, $ logPath. date ('Y-m-d', time ()). '. log ');
}

/**
* The static factory method generates a unique instance of a single URL
* @ Param string $ url
*/
Public static function start ($ url)
{
$ Key = md5 ($ url );

If (isset (self ::$ _ objects [$ key]) {
Return self: $ _ objects [$ key];
}

Self: $ _ objects [$ key] = new HessianApi ($ url );
Return self: $ _ objects [$ key];
}
}

Class JavaNullPointException extends Exception {}

Class StackTraceElement extends Exception {}

// IDE: Zend Studio 9.0
// IDE Extension: Toggle Vrapper

?>


5. encapsulate the client request method and inherit the HessianApi class

The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.