HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Review for a PHP socket proxy

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
proxyphpforsocketreview

Problem

For my current project we are commnunicating with an old mainframish hospital information system. This system has a service called 'Facelink' which is a simple TCP connection with a fixed set of commands. The problem is that a user can only be logged in one time. As we're hosting a web frontend with a lot of links to this service and we have some long running jobs running on the backend this can create concurrency issues (user already logged in).

To counter this I've created a proxyserver class that is the only thing connected to the Facelink service and my jobs and scripts can connect to this as much as they want. It does what it should but as I am the only programmer working on this I have the nagging feeling that it can be written better. Please give me some tips and pointers.

One question i already have is this:

The protocol works a bit like this [COMMANDCODE] [MESSAGE], for example 001 User Identification = 1234. I'd like to get rid of the magic numbers and create constants, but since I have a FacelinkConnection class (used to connect to the FL service, now to the proxy), a FacelinkProxyServer class, and a bunch of FacelinkServiceX classes I wouldn't know where to put these. I've heard that a Facelink class with only constants is bad practice.

Thanks for helping and sharing.

```
class FacelinkProxyServer
{
/**
* List of client sockets
* @var array
*/
protected $clients = array();

/**
* The socket clients will connect to
* @var resource
*/
protected $listenSocket = null;

/**
* The client socket that connects to the facelink server
* @var resource
*/
protected $facelinkSocket = null;

/**
* The port we listen on for new connections
* @var int
*/
protected $listenPort = 8000;

/**
* The IP address we bind to
* @var string
*/
protected $listenHost = '127.0.0.1';

/**
* Are we are shutting down (stop accepting new client

Solution

Why not place the magic numbers into your class as constants?

class FacelinkProxyServer {
   const USER_ID = "001 User Identification"
   // and so on
}


Then you can access them in the class as their name and outside as FacelinkProxyServer::USER_ID.

Code Snippets

class FacelinkProxyServer {
   const USER_ID = "001 User Identification"
   // and so on
}

Context

StackExchange Code Review Q#1518, answer score: 3

Revisions (0)

No revisions yet.