Help on Understating - Wrapper Method

Hello all,

I’m trying to understand a given class for interacting with a EPP server.
On that class we have two methods:

One:


function getFrame() {
			if (@feof($this->socket)) return new PEAR_Error('connection closed by remote server');

			$hdr = @fread($this->socket, 4);

			if (empty($hdr) && feof($this->socket))
                        {
				return new PEAR_Error('connection closed by remote server');

			} 
                        elseif (empty($hdr))
                        {
				return new PEAR_Error('Error reading from server: '.$php_errormsg);

			}
                        else
                        {
                                //este unpack parece estar relacionado com o pern, pois não devolve
                                //nenhum array associativo. Mas, apesar disto... temos de ver que está chamar por uma chave numérica.
				$unpacked = unpack('N', $hdr);
				$length = $unpacked[1];
				if ($length < 5)
                                {
					return new PEAR_Error(sprintf('Got a bad frame header length of &#37;d bytes from server', $length));

				} 
                                else
                                {
					return fread($this->socket, ($length - 4));

				}
			}
		}

Two:

function sendFrame($xml) {
                    fwrite($this->socket, pack('N', (strlen($xml)+4)).$xml);
		}

And then, we have a wrapper method:

function request($xml) {
			$this->sendFrame($xml);
			return $this->getFrame();
		}

Assuming that a wrapper methods commonly, are used to add behavior before and after the default method executes.

I presume that this interacts with the sendFrame and getFrame somehow. But I’m not getting how?
Is this something we do when we instantiate?
But how?

When we call the request method, it will execute the sendFrame($xml) and return a Frame.
So, it is a wrapper because, by calling this $request we are executing both, SendFrame and then getFrame?

$this->sendFrame($xml);
return $this->getFrame();

Can anyone help me understand this if, possible by relating it with a EPP communication context?

Any lights into this, any thoughts could help me out somehow. :slight_smile:

Additional request: we can use PHP exceptions and have one less dependency by removing the required perl. Can’t we?

Thanks a lot,
Márcio

Assuming that a wrapper methods commonly, are used to add behavior before and after the default method executes.

A wrapper is a vague term, which could be used to describe several design patterns. Adapter is probably the most common association, but clearly you stated adding behavior (ie: decorator) and in some cases developers use wrappers to simply complex interfaces (ie: facade).

I presume that this interacts with the sendFrame and getFrame somehow. But I’m not getting how?
Is this something we do when we instantiate?

Well sockets it would seem…

Additional request: we can use PHP exceptions and have one less dependency by removing the required perl. Can’t we?

Is that all the code???

PEAR_Error looks like it could be removed but the class inheritence you left out and this class may very well inherit from a PEAR class in which case it would be more difficult to remove the dependency on PEAR - possibly anyways.

Cheers,
Alex

Ok. So I need to look to a more specific concepts that describe some specific design patterns. ok.

I do realize what this class intends to do, more or less… the point of this class is to send xml information but, prior to that, it needs to send the required 4 bytes containing the length of the sended (this word doesn’t exist right? :s) request, to the server… that is, more or less, what I could understand right now.
We can use this class, either to send or to receive server information.

I’m just not getting how can we use it, I mean, should we call sendFrame when we need to send one? So when will the request be triggered? Will it be triggered automatically, or we should call it ?
Should we first use the resquest method and then either sendFrame or Receive frame? Or should we called after? Or, before AND after?

Those are the questions that I’m not getting.

Yes. Pretty much. You have another method for logout the server and one to connect to the server. That’s all.
The require(‘PEAR.php’); is there, but there is no class extension on that.

Since it’s not the case, we can use PHP own exception methods? I’m asking this, because I’m not quite sure if/when we should use those trow exceptions that PHP provide to us.

Thanks for your feedback. :slight_smile: