<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; Architecture</title>
	<atom:link href="http://googolflex.com/?feed=rss2&#038;cat=101" rel="self" type="application/rss+xml" />
	<link>http://googolflex.com</link>
	<description></description>
	<lastBuildDate>Fri, 06 Aug 2010 18:46:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL Master-Master Replication</title>
		<link>http://googolflex.com/?p=542</link>
		<comments>http://googolflex.com/?p=542#comments</comments>
		<pubDate>Tue, 09 Feb 2010 16:34:16 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[master-master]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=542</guid>
		<description><![CDATA[In my opinion, having dual-writable master MySQL databases (in a replication configuration) is not worth the hassle.  There are a host of problems, enough that you should seriously consider what you&#8217;re trying to gain when attempting it.  However, the master-master replication scheme still has some very good uses when used in an active-passive [...]]]></description>
			<content:encoded><![CDATA[<p>In my opinion, having dual-writable master MySQL databases (in a replication configuration) is not worth the hassle.  There are a host of problems, enough that you should seriously consider what you&#8217;re trying to gain when attempting it.  However, the master-master replication scheme still has some very good uses when used in an active-passive way.  The two most compelling reasons for me are:</p>
<p>1. Keeping a &#8220;hot spare&#8221;.  You have an additional database already configured as a master.  This might not seem like much, since you &#8220;almost&#8221; gain the same thing in a master-slave setup.  However, if your master-master includes slave servers, this topology provides a very high degree of fault tolerance.  Especially when each master-server pair is geographically separated.</p>
<p>2. Making changes to the database.  Certain changes made to the database may require a long time to complete, particularly if you have a very large database.  In the master-master setup, you can take one of the servers &#8220;offline&#8221; (by telling the other master not to replicate its changes), and make the changes necessary.  Then bring it back online, after the changes have been made, make it the active master, and let the other master perform the changes.</p>
<p>This could be seen as an added benefit of keeping a &#8220;hot spare&#8221;.</p>
<p>The good news is that setting this up is identical to setting up master-slave replication, you simply do it twice.  Each master is essentially the master of and a slave to the other database.  To keep it active-passive, one of the databases will need to be made read-only.  Here are the changes you will need to the my.cnf configuration file:</p>
<p><strong>Active Master my.cnf</strong></p>
<pre class="brush: bash;">
log_bin = mysql-bin
server_id = 1001
relay_log = mysql-relay-bin
log_slave_updates = 1
</pre>
<p><strong>Passive Master my.cnf</strong></p>
<pre class="brush: bash;">
log_bin = mysql-bin
server_id = 1002
relay_log = mysql-relay-bin
log_slave_updates = 1
read_only = 1  # Notice this line
</pre>
<p>Then set up the replication user accounts, as described in this post: <a href="http://googolflex.com/?p=518">Simple MySQL Master-Slave Replication</a></p>
<p>Finally you issue the slave directives, and start the slave process:</p>
<p><strong>Active Master &#8216;change master&#8217;</strong></p>
<pre class="brush: bash;">
# Active master is slave to passive host
CHANGE MASTER TO MASTER_HOST='passive.mysql.host',
MASTER_USER='rep_user',
MASTER_PASSWORD='reppassword',
MASTER_LOG_FILE='mysql-bin-000001',
MASTER_LOG_POS=0;

start slave;
</pre>
<p><strong>Passive Master &#8216;change master&#8217;</strong></p>
<pre class="brush: bash;">
# Passive master is slave to active host
CHANGE MASTER TO MASTER_HOST='active.mysql.host',
MASTER_USER='rep_user',
MASTER_PASSWORD='reppassword',
MASTER_LOG_FILE='mysql-bin-000001',
MASTER_LOG_POS=0;

start slave;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=542</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Flex Socket Client</title>
		<link>http://googolflex.com/?p=367</link>
		<comments>http://googolflex.com/?p=367#comments</comments>
		<pubDate>Tue, 09 Feb 2010 04:39:43 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Application Servers]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[client/server]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=367</guid>
		<description><![CDATA[Client-server programming is one of my passions, and I enjoy doing it in almost any language.  Which makes it fitting that Flex (ActionScript) is one of my favorite languages to develop in, because it truly is a client-side technology which pretty much means it&#8217;s open to just about any type of backend server.  [...]]]></description>
			<content:encoded><![CDATA[<p>Client-server programming is one of my passions, and I enjoy doing it in almost any language.  Which makes it fitting that Flex (ActionScript) is one of my favorite languages to develop in, because it truly is a client-side technology which pretty much means it&#8217;s open to just about any type of backend server.  I suppose you could make that argument with just about any language depending on how it&#8217;s being used, but since Flex isn&#8217;t processed and returned to the browser as HTML, that makes it a little different in my opinion.</p>
<p>Even in Flex, most applications will connect to a backend server that&#8217;s running an HTTP service (which queries the database on behalf of Flex), but I&#8217;m even going to depart from that in this post.  I&#8217;m going to be demonstrating how to make socket calls to another service.  You could apply this to making HTTP requests on port 80, make requests directly to a MySQL server running on port 3306.  In this particular post, I will keep it simple by demonstrating how to make an HTTP request.</p>
<hr />
<strong><u>Declaring and initializing the Socket</u></strong></p>
<p>You declare socket like this:</p>
<pre class="brush: as3;">
import flash.net.Socket;

private var socket : Socket;
</pre>
<p>Inside an initialization method, we will add a listener for the CONNECT event, and the SOCKET_DATA event.  The CONNECT event alerts us when the connection has been made, and allows us to send our request.  The SOCKET_DATA event is triggered whenever there is data waiting to be read from the socket.</p>
<pre class="brush: as3;">
socket = new Socket();
socket.addEventListener( Event.CONNECT, onConnect);
socker.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData);
</pre>
<hr />
<strong><u>Connecting and Sending Data</u></strong></p>
<p>At some point you will need to call connect on the socket, which accepts the URL of the server and the port it will be connecting to.  In the <code>onConnect()</code> method, you will write the request using one of the <code>writeXXX()</code> methods, and call <code>flush()</code> which actually sends the data over the wire.</p>
<pre class="brush: as3;">
socket.connect(txtUrl.text, 80);

private function onConnect(event : Event) : void {
   var requestString : String = getHttp_1_1Request();
   socket.writeUTFBytes(requestString);
   socket.flush();
}
</pre>
<hr />
<strong><u>Reading Data</u></strong></p>
<p>As not all socket data will be available at once, as in most client programs, you will want to read the data in a loop, using one of the <code>readXXX(</code>) methods.</p>
<pre class="brush: as3;">
private function onSocketData(event : ProgressEvent) : void {
   while (socket.bytesAvailable) {
      txtResponse.text += socket.readUTFBytes(socket.bytesAvailable);
   }
   socket.close();
}
</pre>
<p>In general you would be checking for the end of the response you&#8217;re expecting (based on protocol), to simplify things I simply close the socket after the while loop exits the first time.  If you leave the socket open for too long without reading data, I have noticed that I receive the following error, regardless of whether I have the correct policy file including cross-domain.xml:</p>
<p><code>Error #2044: Unhandled securityError:. filename.swf text=Error #2048: Security sandbox violation cannot load data from the.host.name</code></p>
<p>In this example, the raw output received from the socket request will be shown in the text box.  You can download the complete source to this example <a href="http://flexplanations.com/wp-content/uploads/2009/03/socketconnection.mxml">here </a>(where you may also view a screencast demo of this procedure).</p>
<p>Depending on what version of the Flash Player you&#8217;re running, this example may not run as-is if the server you are connecting to does not implement the policy-file (served from port 843.  I will be discussing this in an upcoming post where I delve into a little more advanced socket programming in Flex, including how and where to deploy a master policy file.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=367</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Simple MySQL Master-Slave Replication</title>
		<link>http://googolflex.com/?p=518</link>
		<comments>http://googolflex.com/?p=518#comments</comments>
		<pubDate>Sat, 06 Feb 2010 21:33:03 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[master-slave]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=518</guid>
		<description><![CDATA[I&#8217;ve been doing a lot of research (not cutting edge type stuff) into MySQL scalability, and the first exercise I went through was configuring a simple master-slave replication setup.  It was much simpler than I thought it would be.  Here are the steps.

Editing the my.cnf Files
Because of the way replication works in MySQL, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a lot of research (not cutting edge type stuff) into MySQL scalability, and the first exercise I went through was configuring a simple master-slave replication setup.  It was much simpler than I thought it would be.  Here are the steps.</p>
<hr />
<strong><u>Editing the my.cnf Files</u></strong></p>
<p>Because of the way replication works in MySQL, you will need to turn on binary logging.  Essentially, the slave is going to connect to the master and request the log.  After it gets the log (or the parts that it needs) it will replay it, executing the queries to bring it up to date.  You also need to give the server an ID.  So add these lines to the my.cnf (usually /etc/my.cnf) on the master database:</p>
<pre class="brush: bash;">
&amp;lt;mysqld&amp;gt;
log_bin = mysql-bin
server_id = 1001
</pre>
<p>You can give it any ID you want, you just want it to have a different ID than the slave.  In the slave we will add a few additional lines, as well:</p>
<pre class="brush: bash;">
&amp;lt;mysqld&amp;gt;
log_bin = mysql-bin
server_id = 1002
relay_log = mysql-relay-bin
log_slave_updates = 1
read_only = 1
</pre>
<p>Note: Replace the angle brackets above with square brackets.  I haven&#8217;t figured out how to make my code plugin not treat [mysqld] as another code block definition.</p>
<p>According to Schwartz, et al. in <em>High Performance MySQL</em> (review forthcoming), the only parameter required on a slave is the server_id.  The other parameters make it easy to switch the server between being a master or a slave.  They also mention the read_only parameter is a good safety precaution but might not be applicable in all cases.</p>
<hr />
<strong><u>User Account Setup</u></strong></p>
<p>Replication privileges need to be granted to the replication user on the master.  If you want to be able to easily switch your server between master or slave, you can grant these privileges on both servers.</p>
<pre class="brush: sql;">
GRANT REPLICATION SLAVE
ON *.*
TO 'rep_user'@'10.0.0.%'
IDENTIFIED BY 'reppassword';
</pre>
<p>There are certain features that are accessible by granting the <code>REPLICATION CLIENT</code> privilege.  You can also limit the replication privileges to only certain databases or tables if so desired.  Though it is not recommended, you could also grant access to all hosts, just as you might in any other GRANT statement.</p>
<hr />
<strong><u>Starting the Replication Procedure</u></strong></p>
<p>Now that both the master and slave are configured, and correct permissions are granted to the replication user, the slave needs to be &#8220;started&#8221;.  This is done by declaring the master host, and indicating necessary credentials and log file information, and then issuing the &#8217;start slave&#8217; command.</p>
<p>The host and credential information can be declared in the my.cnf file.  However there are some advantages to making the declarations as MySQL commands.  Specifically you will be able to make changes to these without having to restart the daemon.  Here is the command you should issue to configure the slave:</p>
<pre class="brush: sql;">
CHANGE MASTER TO MASTER_HOST='master.mysql.host',
MASTER_USER='rep_user',
MASTER_PASSWORD='reppassword',
MASTER_LOG_FILE='mysql-bin-000001',
MASTER_LOG_POS=0;
</pre>
<p>And then you issue the command to start the slave:</p>
<pre class="brush: sql;">start slave</pre>
<p>If you were previously using binary logs on the master, and then cloned the server to create the slave, you might have trouble getting replication started without first deleting the binary log from the slave.</p>
<p><strong><u>Sources</u></strong><br />
<a href="http://dev.mysql.com/doc/refman/5.1/en/replication-howto.html">The MySQL documentation</a><br />
<em>High Performance MySQL</em>, by Baron Schwartz et al.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=518</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache mod_proxy_balancer Self Registration : Part 3</title>
		<link>http://googolflex.com/?p=507</link>
		<comments>http://googolflex.com/?p=507#comments</comments>
		<pubDate>Thu, 04 Feb 2010 22:25:51 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Shell Scripting]]></category>
		<category><![CDATA[client/server]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[load balancing]]></category>
		<category><![CDATA[mod_proxy_balancer]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[self registration]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=507</guid>
		<description><![CDATA[I&#8217;ll start off by going over the basic high level architecture for my self registration procedure:
There is a register.php script residing on the load balancer, accessible via HTTP.
There is a deregister.php script residing on the load balancer, accessible via HTTP.
There is a register_with_lb.pl script residing on the web server, in /usr/local/bin/.
There is a deregister_with_lb.pl script [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll start off by going over the basic high level architecture for my self registration procedure:</p>
<p>There is a register.php script residing on the load balancer, accessible via HTTP.<br />
There is a deregister.php script residing on the load balancer, accessible via HTTP.<br />
There is a register_with_lb.pl script residing on the web server, in /usr/local/bin/.<br />
There is a deregister_with_lb.pl script residing on the web server, in /usr/local/bin/.<br />
There is a MySQL database that stores the current configuration state, on it are two stored procedures register_lb and deregister_lb.</p>
<hr/>
<strong>register.php</strong></p>
<p>No changes were made to register.php as described in this <a href="http://googolflex.com/?p=494">post </a>, though I&#8217;m considering some alterations to increase its security.</p>
<hr/>
<strong>deregister.php</strong></p>
<p>The biggest difference between register.php and deregister.php (aside from their purpose) is where the insert/delete database code is called from and why.  When register.php is called by the web server, it will have already inserted information about itself into the database, including its hash.  I made the decision that I did not want the load balancer responsible for inserting servers into the database.  It would merely check that the requesting server inserted itself, and then regenerate the balancer_members.conf.</p>
<p>In the case of deregister.php I decided I wanted the server making the call to still be in the database so the script could verify the identity before removing it and regeneration the balancer_members.  And since the deregistration SQL is contained within a stored procedure, I needed to make some changes to the script (as compared to register.php) regarding the database.</p>
<p>Specifically, the standard mysql library cannot call stored procedures.  So I had to convert it to using mysqli, which is a similar, though more OO approach.  The portion of the code that regenerates the balancer_members.conf is similar enough that I won&#8217;t re-list it here, but I will show how to connect using mysqli, and how to call a stored procedure.</p>
<pre class="brush: php;">
$mysqli = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()) {
  printf(&quot;Connect failed: %s\n&quot;, mysqli_connect_error());
}

$query = &quot;SELECT count(*) as count FROM &quot; . $dbtable . &quot; WHERE ip='&quot; . $_SERVER['REMOTE_ADDR'] . &quot;';&quot;;
$result = $mysqli-&gt;query($query);
$row = $result-&gt;fetch_row();
echo $row[0];

if ($row[0] &gt;= 1) {
  $del_query = &quot;call deregister_lb('&quot; . $_SERVER['REMOTE_ADDR'] . &quot;');&quot;;
  $del_result = $mysqli-&gt;query($del_query);

  //&lt;code for regenerating the conf file removed here&gt;

  echo exec('echo &quot;' . $file . '&quot; &gt; /etc/httpd/conf.d/balancer_members.conf');
  echo exec(&quot;sudo /usr/local/bin/reload_httpd&quot;);
}
</pre>
<p>As you can see, I&#8217;m using the actual REMOTE_ADDR to determine the validity of the request.</p>
<hr/>
<strong>(de)register_lb.sql Stored Procedure</strong></p>
<p>Here is the code for the deregister_lb stored procedure:</p>
<pre class="brush: sql;">
DROP PROCEDURE IF EXISTS deregister_lb $$

CREATE PROCEDURE deregister_lb ( ip VARCHAR(100) )
  BEGIN
    DELETE FROM lb2_members
	WHERE ip=_ip;
  END $$
</pre>
<p>and also for the register_lb stored procedure:</p>
<pre class="brush: sql;">
DROP PROCEDURE IF EXISTS register_lb $$
CREATE PROCEDURE register_lb (
  _hostname VARCHAR(100),
  _ip VARCHAR(40),
  _loadfactor INT,
  _hash VARCHAR(100)
  )

  BEGIN
    DECLARE already_exists INT DEFAULT 0;
    SELECT count(*) INTO already_exists FROM lb2_members WHERE hash=_hash;

    IF already_exists=1 THEN
	  UPDATE lb2_members
	  SET hostname=_hostname, ip=_ip, loadfactor=_loadfactor
	  WHERE hash=_hash;
    ELSE
      INSERT INTO lb2_members (ip, hostname, loadfactor, hash)
	  VALUES (_ip, _hostname, _loadfactor, _hash);
    END IF;
  END $$
</pre>
<p>Note that I&#8217;ve omitted the code that changes the delimiter to $$ instead of a semicolon.</p>
<hr />
<strong>register_with_lb.pl</strong></p>
<p>This perl script uses perl DBI for accessing the database.  I had to get that installed on my web server since it wasn&#8217;t already.  Normally you can install perl packages using the cpan command.  In which case you would issue the following commands to install DBI and a MySQL driver for it:</p>
<pre class="brush: bash;">
cpan DBI
cpan DBD::mysql
</pre>
<p>If it&#8217;s the first time you&#8217;ve run cpan, you will need to go through some configuration.  It&#8217;s pretty much self explanatory, and I just accepted all of the defaults.  Everything installed correctly except for the MySQL driver, which I ended up having to install from source.  If I had executed the command:</p>
<pre class="brush: bash;">yum install mysql-devel.i386</pre>
<p>first, then my cpan install of DBD::mysql might have worked, but I didn&#8217;t realize that until installing from source.  In case you ever need to install a perl module from source, particularly the DBD::mysql driver, enter these commands (which I think is basically what cpan does):</p>
<pre class="brush: bash;">
yum install mysql-devel.i386 #(only requred in this particular instance)
wget http://www.cpan.org/modules/by-module/DBD/DBD-mysql-4.011.tar.gz
gzip -cd DBD-mysql-4.011.tar.gz | tar xf -
cd DBD-mysql-4.011 #(or whatever version you downloaded)
perl Makefile.PL
make
</pre>
<p>Here is how you connect to the database and call a stored procedure:</p>
<pre class="brush: perl;">
my $dsn = &quot;DBI:mysql:host=mysql.host;database=lb_register&quot;;
my $dbh = DBI-&gt;connect ($dsn, &quot;lbuser&quot;, &quot;lbpasswd&quot;)
  or die &quot;Cannot connect to MySQL server\n&quot;;

my $sql = &quot;call register_lb('&quot; . $localhost . &quot;', '&quot; . $localip . &quot;', &quot; . $loadfactor . &quot;, '&quot; .  $hash . &quot;')&quot;;
$dbh-&gt;do($sql);

$dbh-&gt;disconnect();
</pre>
<p>After that, register_with_lb.pl opens a socket to the load balancer and makes an HTTP request over the socket.  There are probably easier ways to do this, I just happened to have the socket code lying around and was glad to be able to reuse it.  Here&#8217;s the gist of it, in case you&#8217;re interested:</p>
<pre class="brush: perl;">
# Parse the URI.
my $url = URI-&gt;new(&quot;http://load.balancer.com/register/register.php?hash=&quot; . $hash);

# Parse these in from the command line
$host = $url-&gt;host;
$port = $url-&gt;port;
$resource = $url-&gt;path;
$query = $url-&gt;query;

# Initialize the socket
$socket = IO::Socket::INET-&gt;new ( Proto =&gt; &quot;tcp&quot;, PeerAddr =&gt; $host, PeerPort =&gt; $port,);
unless ($socket) { die &quot;Error connecting to $host&quot; }
$socket-&gt;autoflush(1);

# Format the request
my $request = &quot;GET &quot; . $resource . (($query)?&quot;?&quot; . $query : &quot;&quot;) . &quot; HTTP/1.1&quot; . $EOL . &quot;Host: &quot; . $host . $EOL . &quot;User-agent: register_script&quot; . $EOR;

# Use send() to make the request, and output the response.
# Not necessary in this example, but informational.
if ( $socket-&gt;send($request) ) {
  while ( &lt;$socket&gt; ) { print }
}

# Close the socket
close $socket;
</pre>
<p>The above code pretty much sums up deregister_from_lb.pl, since no database calls are made, a call is simply made to the deregister script.  The line you would change is as follows:</p>
<pre class="brush: perl;">my $url = URI-&gt;new(&quot;http://my.balancer.com/register/deregister.php&quot;);</pre>
<p>Then make the files executable, and copy them to be used by the startup script described in the previous <a href="http://googolflex.com/?p=499">post</a>:</p>
<pre class="brush: bash;">
chmod a+x register_with_lb.pl
chmod a+x deregister_with_lb.pl
cp register_with_lb.pl /usr/local/bin/
cp deregister_with_lb.pl /usr/local/bin
</pre>
<p>I don&#8217;t show it here, but right now my IP addresses are hard coded.  There are a number of ways you can find out your actual IP address from within perl, I&#8217;m just not doing that right now.</p>
<hr/>
<strong>Securing the register scripts</strong></p>
<p>As an additional security measure, I&#8217;ve restricted access to the /register/ location on the load balancer to the IP address range I expect my web servers to be from, like this:</p>
<pre class="brush: bash;">
&lt;Location /register&gt;
  Order Deny,Allow
  Deny from all
  Allow from 10.0.0.
&lt;/Location&gt;
</pre>
<p>And now you have a web server that can register automatically (if you&#8217;ve gone through the previous two posts as well) with a mod_proxy_balancer load balancer.</p>
<hr/>
<strong>Update</strong></p>
<p>I did some searching around to find a way to determine your IP address from inside the perl script.  This is a simple way if your server has a public IP address and reverse DNS set up correctly for that IP address:</p>
<pre class="brush: perl;">
use Socket;
use Sys::Hostname;
my $host = hostname();
my $addr = inet_ntoa(scalar(gethostbyname($host)) || 'localhost');
</pre>
<p>If your slave web servers are on a private network, the above command will return the loopback IP address (127.0.0.1) which isn&#8217;t useful for the load balancer (I wonder if it would start an infinite loop and crash the load balancer?).  I found a function that prints out the IP address by parsing it out from the results of the ifconfig command.</p>
<p>It seemed a little long to just rip off and copy verbatim.  So here&#8217;s a link to that code (which is what I&#8217;m using now) in case you&#8217;d like to use it.    <a href="http://ubuntuforums.org/showthread.php?t=273433">Perl script to get IP address</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=507</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache mod_proxy_balancer Self Registration : Part 1</title>
		<link>http://googolflex.com/?p=494</link>
		<comments>http://googolflex.com/?p=494#comments</comments>
		<pubDate>Wed, 03 Feb 2010 20:39:12 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Amazon Web Services]]></category>
		<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[EC2]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[client/server]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[load balancing]]></category>
		<category><![CDATA[mod_proxy_balancer]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[self registration]]></category>
		<category><![CDATA[Shell Scripting]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=494</guid>
		<description><![CDATA[Load balancers are great, but they become even more powerful when servers have the ability to self-register when they come online, and deregister when they go offline.  This is especially true with services such as EC2, when the size of the server group might grow or shrink in response to need.  This is [...]]]></description>
			<content:encoded><![CDATA[<p>Load balancers are great, but they become even more powerful when servers have the ability to self-register when they come online, and deregister when they go offline.  This is especially true with services such as EC2, when the size of the server group might grow or shrink in response to need.  This is a tutorial describing my particular (partially insecure at the moment) solution for allowing self-registration with Apache&#8217;s mod_proxy_balancer.  Specifically this covers the load balancer side of the equation.  Tomorrow I hope to get a post out describing the server side.</p>
<p>Here is my flowchart for how self registration will work:<br />
1. Server comes online.<br />
2. A startup script will register itself with the MySQL database (including hostname, ip, loadfactor, and a hash that it will generate in some way).<br />
3. The server will then call a PHP script on the load balancer: &#8220;register/register.php&#8221;.<br />
4. The PHP script will verify that a server sent the request.<br />
5. The PHP script will query the database to get the current list of balancer members, and regenerate the balancer_members.conf file.<br />
6. The PHP script will then issue a command to reload Apache&#8217;s configuration files.</p>
<p>Deregistration, which my PHP script as presented doesn&#8217;t display, will work as follows:<br />
1. Server sends its hash to the PHP script, and shuts down.<br />
2. The PHP script will check the hash against the database.<br />
3. The PHP script will remove the server from the database.<br />
4. The PHP script will repeat steps 5 and 6 above.</p>
<p>First, set up the database and created a user with sufficient privileges.</p>
<pre class="brush: sql;">
CREATE DATABASE lb_register;
GRANT ALL ON lb_register.* TO 'lbuser'@'%' IDENTIFIED BY 'password';

CREATE TABLE lb2_members(
ip VARCHAR(20) NOT NULL PRIMARY KEY,
hostname VARCHAR(100) NOT NULL,
loadfactor INT NOT NULL DEFAULT 0,
hash VARCHAR(40) );
</pre>
<p>Second, create the PHP script.</p>
<pre class="brush: php;">
$dbhost = &quot;mysql.host.com&quot;;
$dbuser = &quot;lbuser&quot;;
$dbpass = &quot;password&quot;;
$dbname = &quot;lb_register&quot;;
$dbtable = &quot;lb2_members&quot;;

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die (mysql_error());
mysql_select_db($dbname);

$query = &quot;SELECT count(*) as count FROM &quot; . $dbtable . &quot; WHERE hash='&quot; . $_GET['hash'] . &quot;';&quot;;
$result = mysql_query($query);

$row = mysql_fetch_assoc(mysql_query($query));
if ($row['count'] &gt;= 1) {

  $file = &quot;&lt;Proxy balancer://mycluster&gt;&quot; . &quot;\n&quot;;
  $member_query = &quot;SELECT hostname, loadfactor FROM &quot; . $dbtable . &quot;;&quot;;
  $member_result = mysql_query($member_query);

  while ($row = mysql_fetch_array($member_result, MYSQL_BOTH)) {
    $file .= &quot;   BalancerMember http://&quot; . $row['hostname'] . &quot; &quot;;
    $file .= ($row['loadfactor'] &gt; 1) ? (&quot;loadfactor=&quot; . $row['loadfactor'] . &quot;\n&quot;) : &quot;\n&quot;;
  }
  $file .= &quot;&lt;/Proxy&gt;&quot;;

  exec('echo &quot;' . $file . '&quot; &gt; /etc/httpd/conf.d/balancer_members.conf');
  exec(&quot;sudo /usr/local/bin/reload_httpd&quot;);
}

mysql_close($conn);
</pre>
<p>You can tell a few things about the server configuration by looking at the script:<br />
1. User apache will need to be able to write to the &#8220;/etc/httpd/conf.d/balancer_members.conf&#8221; file.<br />
2. User apache will need to be able to execute the script &#8220;/usr/local/bin/reload_httpd&#8221;.<br />
3. User apache will need sudoer rights.<br />
4. This script was used for debugging, and not by a server that is actually registering&#8230; tyou can see that deregistration is not handled yet.</p>
<p>To grant write privileges to apache, I changed the owner of the balancer_members.conf to apache.</p>
<pre class="brush: bash;"> chown apache /etc/httpd/conf.d/balancer_members.conf</pre>
<p>This is probably the least secure aspect of my solution, as if the apache user were compromised, then any directives could be written to this file.  I&#8217;m not sure how big a threat this is, but it&#8217;s something that concerns me at least enough to think about this some more (and invite suggestions).</p>
<p>Next is to grant apache privileges to execute &#8220;/usr/local/bin/reload_httpd&#8221;.  We could accomplish this the same as we did above, but then it wouldn&#8217;t allow apache to execute what&#8217;s inside of the script, which is this:</p>
<pre class="brush: bash;">
#!/bin/bash
service httpd reload
</pre>
<p>unless we give execution rights to apache on service, which we don&#8217;t want.  What we also don&#8217;t want is for apache to be able to write to the file reload_httpd.  So what I ended up doing was, as you see in the script, to make root the owner of reload_httpd and remove write privileges for all (so apache couldn&#8217;t change it) and then add apache to the sudoers file, granting rights to execute this script without a password.</p>
<pre class="brush: bash;">visudo</pre>
<p>is the generally accepted way to edit the sudoers file.  And I added this line:</p>
<pre class="brush: bash;">apache ALL=(ALL) NOPASSWD: /usr/local/bin/reload_httpd</pre>
<p>I&#8217;m open to more secure ways of implementing this aspect as well, as I don&#8217;t consider myself a sudo configuration expert.  I think this gives apache rights to execute everything from anywhere if he knows the password; but he can also execute the /usr/local/bin/reload_httpd script without a password.</p>
<p>I also had to comment out the line:</p>
<pre class="brush: bash;">#Defaults   requiretty</pre>
<p>to allow sudo to function properly from a script not executed in a terminal.</p>
<p>Finally I had to disable proxying for the register script in my balancer.conf file:</p>
<pre class="brush: bash;">ProxyPass /register/ !</pre>
<p>And then your server is configured to dynamically update its list of balance members, you can check by going to the balancer-manager if you&#8217;ve got that enabled.  Next I will discuss how to handle the web server side of things.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=494</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Apache mod_proxy_balancer: No Protocol handler was valid</title>
		<link>http://googolflex.com/?p=492</link>
		<comments>http://googolflex.com/?p=492#comments</comments>
		<pubDate>Wed, 03 Feb 2010 04:09:36 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[HTTP Servers]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[client/server]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[error message]]></category>
		<category><![CDATA[mod_proxy_balancer]]></category>
		<category><![CDATA[no protocol]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=492</guid>
		<description><![CDATA[Just this afternoon, one of my colleagues and I were discussing our feelings about the &#8220;semicolon-class&#8221; of bugs that developers will inevitably spin their wheels on from time to time.  I&#8217;ve had just such an experience the past two evenings with what started out as a simple recipe from the Apache Cookbook, entitled &#8220;Load [...]]]></description>
			<content:encoded><![CDATA[<p>Just this afternoon, one of my colleagues and I were discussing our feelings about the &#8220;semicolon-class&#8221; of bugs that developers will inevitably spin their wheels on from time to time.  I&#8217;ve had just such an experience the past two evenings with what started out as a simple recipe from the Apache Cookbook, entitled &#8220;Load Balancing with mod_proxy_balancer&#8221;.</p>
<p>The recipe itself is straightforward, taking up just over a page.  I was able to get a basic load balancer working within a few minutes by following the recipe, but for some reason I couldn&#8217;t get the balancer-manager site to load correctly.  The following directives should configure the balancer-manager:</p>
<pre class="brush: bash;">
&lt;Location /balancer-manager&gt;
  SetHandler balancer-manager
  Order Deny,Allow
  Allow from all
&lt;/Location&gt;
</pre>
<p>When I would start the server, however, I got an HTTP 500 error response, with this message in the log:</p>
<pre class="brush: bash;">
[Tue Feb 02 12:49:09 2010] [warn] proxy: No protocol handler was valid for the URL /balancer-manager. If you are using a DSO version of mod_proxy, make sure the proxy submodules are included in the configuration using LoadModule.
</pre>
<p>My searches on Google were fruitless: most people were able to solve the problem by ensuring these lines were added to their httpd.conf:</p>
<pre class="brush: bash;">
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
</pre>
<p>But they were already included in my conf file, so no dice.  Other people solved their problem by loading the mod_proxy_html.so, which doesn&#8217;t come on a standard install of Apache.  I ended up compiling it from source (learned a lot about compiling Apache shared modules) but that didn&#8217;t work for me either.</p>
<p>Eventually I realized that the only time my requests were being proxied to the BalancerMembers was when I was at the URL root, for example tacking on an &#8216;index.php&#8217; to the URL would generate the same error as trying to access &#8216;balancer-manager&#8217;.  And about that time I stumbled upon a user who was able to solve their problem by adding a few rewrite rules&#8230; which caused me to finally understand part of my problem.</p>
<p>The reason &#8216;balancer-manager&#8217; wasn&#8217;t loading correctly was because that request was being proxied to the BalancerMember, which didn&#8217;t have &#8216;balancer-manager&#8217; enabled (hence the &#8216;no protocol handler error&#8217;).  This still didn&#8217;t explain why &#8216;index.php&#8217; wasn&#8217;t working, either, since I knew index.php was on the BalancerMembers.</p>
<p>These discoveries quickly culminated into leading me to my solution, which was a missing trailing-slash in my ProxyPass directive.  That&#8217;s what was causing my &#8216;index.php&#8217; to not be proxied.  Once I realized the problem was in my ProxyPass&#8211; reading the documentation also taught me that there&#8217;s a way to keep certain requests from being proxied (which solved my &#8216;balancer-manager&#8217; issue).</p>
<p>So I present to you, my completed (and functional) balancer.conf.  And remember, <strong>the trailing slash at the end of the ProxyPass directive IS CRUCIAL!</strong></p>
<pre class="brush: bash;">
ProxyPass /balancer-manager !  # Prevents balancer-manager from being proxied.
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/

&lt;Proxy balancer://mycluster&gt;
  BalancerMember http://10.0.0.10 loadfactor=2
  BalancerMember http://10.0.0.11
  BalancerMember http://10.0.0.12
&lt;/Proxy&gt;

&lt;Location /balancer-manager&gt;
  SetHandler balancer-manager
  Order Allow,Deny
  Allow from all
&lt;/Location&gt;
</pre>
<p>I hope this saves someone some time when searching on the particular error message I got.  Now I&#8217;m off to figure out how to get my web servers to auto register with the balancer-manager.  I will blog about it when I figure it out.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=492</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JUICE chat (Stratus Peer to Peer)</title>
		<link>http://googolflex.com/?p=454</link>
		<comments>http://googolflex.com/?p=454#comments</comments>
		<pubDate>Tue, 22 Dec 2009 20:05:17 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Cold Fusion]]></category>
		<category><![CDATA[ColdFusion Enterprise Server]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[adobe]]></category>
		<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[juice chat]]></category>
		<category><![CDATA[p2p]]></category>
		<category><![CDATA[peer to peer]]></category>
		<category><![CDATA[stratus]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=454</guid>
		<description><![CDATA[One of my ongoing weekend projects is an AIR chat application, recently rechristened &#8220;JUICE&#8221; a.k.a. &#8220;John&#8217;s Ultimate Internet Chat Experience&#8221;.  I realize that may only be the case for myself, but the project has been very educational and entertaining for me.
The initial release used AIR/Flex as the front-end, ColdFusion and MySQL for the backend, [...]]]></description>
			<content:encoded><![CDATA[<p>One of my ongoing weekend projects is an AIR chat application, recently rechristened &#8220;JUICE&#8221; a.k.a. &#8220;John&#8217;s Ultimate Internet Chat Experience&#8221;.  I realize that may only be the case for myself, but the project has been very educational and entertaining for me.</p>
<p>The initial release used AIR/Flex as the front-end, ColdFusion and MySQL for the backend, and BlazeDS messaging for message transmission and &#8220;peer discovery&#8221;.  I worked on it over a couple weeks adding features like encryption, <a href="http://www.adobe.com/devnet/air/flash/quickstart/creating_toast-style_windows.html" target="_blank">toast-style notifications</a>, as well as a few other less notable things.</p>
<p>I knew the architecture was awful and would never scale beyond a couple hundred users (if that many).  Here&#8217;s the basics of how it worked:</p>
<ol>
<li>The user would login, ColdFusion/MySQL would send back a list of chat buddies.</li>
<li>Each logged in user would ping the messaging server every 10-30 seconds to announce &#8220;I&#8217;m still here!&#8221;</li>
<li>The pings would be broadcast to everyone logged in (and connected to the messaging server). The application would filter out the ones it was interested in based on user id.</li>
<li>Sending messages worked similarly, the user would send the message (which included information about the intended recipient), which would then be broadcast to EVERYONE.</li>
<li>Everyone would filter this information, and the recipient would know it was for him, and would display it.</li>
</ol>
<p>You can see why this would not scale well, the amount of traffic and pinging going on.  Eventually even a user with only 1 or 2 buddies would notice a significant drop in performance as the number of total users increased.  There are some applications of &#8220;chat&#8221; for which BlazeDS messaging and polling is well suited for (like a chat room).  A chat application involving &#8220;buddies&#8221;  is much better suited for P2P.</p>
<p>Enter Stratus.  I discovered Stratus a few months ago but was unwilling to delve into it on account of my hectic schedule.  Fortunately one of my friends went and did all the dirty work figuring out how to get it up and running (turns out it&#8217;s pretty easy) and I was able to apply it to my chat client.</p>
<p>I&#8217;m very pleased with the results, though it&#8217;s still very much a work in progress.  I&#8217;ve even been able to add video and voice chat features, which is why I went through the trouble in the first place.  Here is how my peer discovery mechanism operates.  It requires only 3 calls to the server per client (which I could easily condense to 2 if I wanted).</p>
<ol>
<li>The user logs in.</li>
<li>If the user successfully authenticated, he connects to Stratus and receives his peer ID.</li>
<li>The user updates his peer ID in the database (which was set to 0) and gets a list of his buddies and their peer IDs.  I also update information like whether or not I have a camera/mic.</li>
<li>The user processes his buddy.  If the peer ID is something other than 0, then I know this buddy logged in before me, so I must send him my peer ID.</li>
<li>Any buddies that have a peer ID of 0 are not logged in, I need to listen for them though since they will get my peer ID when they log in.</li>
<li>When I log out or close the application, I call the server and reset my peer ID to 0.  I also notify all of my connected peers I&#8217;m logging out.</li>
</ol>
<p>There are a few more steps in the process, but it decreases the load on the server significantly, transferring the &#8220;burden of proof&#8221; onto the clients.</p>
<p>Interesting to note is the way I get the peer ID of a client who connects after me (step 4).  I don&#8217;t actually have to make a call for this, rather he simply has to start broadcasting to me on the NetStream I set up to listen for him (step 5).  When the connection is made, an event will be dispatched by the NetStream class.  In fact, 3 events will be fired, I just chose to act on one of them.</p>
<p>The peer ID can be obtained from the peerStreams array on the publishing NetStream.  I then use that information to set up my subscribing NetSream.</p>
<pre class="brush: as3;">
if (this.peerId == null || this.peerId == &amp;amp;quot;0&amp;amp;quot;) {
    this.peerId = publishNetStream.peerStreams[0].farID;

    this.subscribeNetStream = new NetStream(model.netConnection, this.peerId);
    this.subscribeNetStream.client = new NetStreamClient();
    this.subscribeNetStream.play(streamName);
}
</pre>
<p>Anyway,I&#8217;m planning on doing another post to outline my video and voice handshake protocols.  They&#8217;re nothing to write home about, but I&#8217;d love some feedback if there&#8217;s a better way.  I&#8217;m also planning on releasing JUICE after I bring it&#8217;s level of functionality back up to where the original version was, so stay tuned and you should see my install badge at the bottom of my blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=454</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contextual Shortcut Manager</title>
		<link>http://googolflex.com/?p=391</link>
		<comments>http://googolflex.com/?p=391#comments</comments>
		<pubDate>Thu, 20 Aug 2009 01:43:05 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[shortcut manager]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=391</guid>
		<description><![CDATA[I&#8217;m about ready to sleep on my ShortcutManager, I figured I might as well finish off the series.  I added context to it, while still maintaining backward compatibility with the non-context enabled version.  I don&#8217;t think I posted it, so it probably won&#8217;t matter so much to my readers.  For those who [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m about ready to sleep on my ShortcutManager, I figured I might as well finish off the series.  I added context to it, while still maintaining backward compatibility with the non-context enabled version.  I don&#8217;t think I posted it, so it probably won&#8217;t matter so much to my readers.  For those who haven&#8217;t read the previous posts on my ShortcutManager, the &#8220;context&#8221; I&#8217;m referring to is the context in which a keyboard shortcut might be used; such that a given shortcut might have several functions associated with it depending on its context.</p>
<p>In short I needed four things to implement the context aware shortcut manager.</p>
<p><strong>1. The <code>IKeyboardContext</code> interface</strong></p>
<p>This is a simple interface that a model or class can implement that allows a <code>keyboardContext</code> to be set and retrieved.  A <code>keyboardContext</code> is simply a string that identifies the current context.  These contexts will be associated with a shortcut in the manager.  Here is the entire interface:</p>
<pre class="brush: as3;">
package com.googolflex.gflib.interfaces {

	public interface IKeyboardContext {

		[Bindable(event=&quot;propertyChange&quot;)]
		function get keyboardContext() : String;
		function set keyboardContext(v : String) : void;
	}
}
</pre>
<p><strong>2. A reference to an <code>IKeyboardContext</code> needed to be added to the <code>ShortcutManager</code></strong></p>
<p>As the <code>keyboardContext</code> changes in the model, the ShortcutManager needs to be able to access it, so it can retrieve the correct function for a given keycode-flags-context tuple.  The <code>IKeyboardContext</code> was left null, which helps with the backward compatibility (the implementer will be forced to set it if she wants to use it).  Here is the line I added, which is pretty trivial:</p>
<pre class="brush: as3;">public static var model : IKeyboardContext = null;</pre>
<p><strong>3. Add the context to the <code>addShortcut</code> method</strong></p>
<p>I gave it a default value, which incidentally was &#8220;default&#8221;, so that every function added to the Dictionary would have some kind of context.  Here is the new <code>addShortcut()</code> method:</p>
<pre class="brush: as3;">
public static function addShortcut(keycode : uint, func : Function, flags : uint, context : String = &quot;default&quot;) : void {
	if (flags &gt; 0 &amp;&amp; flags &lt; 8)
		functionMap[keycode + &quot;-&quot; + flags + &quot;-&quot; + context] = func;
}
</pre>
<p><strong>4. Finally, the <code>shortcutHandler()</code> needed to incorporate the context</strong></p>
<p>I first check if the <code>IKeyboardContext</code> is null, if it is I know to use &#8220;default&#8221; as the context.  If no context was ever specified, and no <code>IKeyboardContext</code> was ever set, then it still works.  Here is the modified code:</p>
<pre class="brush: as3;">
public static function shortcutHandler(event : KeyboardEvent) : void {
	var flags : uint = getFlags(event);
	var context : String = (model!=null) ? model.keyboardContext : &quot;default&quot;;

	if ( functionMap[event.keyCode + &quot;-&quot; + flags + &quot;-&quot; + context] != null )
		(functionMap[event.keyCode + &quot;-&quot; + flags + &quot;-&quot; + context] as Function).apply();
}
</pre>
<p>And was pretty much it.  I will post the modified code, along with a demonstration application at the end of the post.  I do want to make a couple points to be aware of when actually incorporating this into a project.</p>
<p>First, your model/class will need to implement <code>IKeyboardContext</code>.  Second, you need to remember to set <code>ShortcutManager.model = myIKeyboardContext</code> somewhere.  Third, you&#8217;re on your own when it comes to maintaining your context.  Maybe the &#8220;SimpleContextManager&#8221; will be next, who knows?  And fourth, you&#8217;ll have to specify a context when you call <code>addShortcut()</code>.  Remember if you download the demo, it will need to have some kind of reference to the ShortcutManager code.</p>
<p><strong>Code</strong><br />
<a href='http://googolflex.com/wp-content/uploads/2009/08/ShortcutManager1.zip'>ShortcutManager.zip</a><br />
<a href='http://googolflex.com/wp-content/uploads/2009/08/KeyboardContextDemo.zip'>KeyboardContextDemo.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=391</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Flex ShortcutManager (revisited)</title>
		<link>http://googolflex.com/?p=386</link>
		<comments>http://googolflex.com/?p=386#comments</comments>
		<pubDate>Wed, 19 Aug 2009 23:07:01 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Flex 3]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[shortcut manager]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=386</guid>
		<description><![CDATA[I&#8217;ve put some more thought into my shortcut manager, and have decided on a way to implement the shortcut context, as well as curb the Dictionary explosion that my previous architecture would have had.
The new version uses one Dictionary to store all functions, the keyCode, combo keys, and context is all encoded into the dictionary [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve put some more thought into my shortcut manager, and have decided on a way to implement the shortcut context, as well as curb the Dictionary explosion that my previous architecture would have had.</p>
<p>The new version uses one Dictionary to store all functions, the keyCode, combo keys, and context is all encoded into the dictionary key.  To illustrate here is my new <code>addShortcut()</code> method:</p>
<pre class="brush: as3;">
public static function addShortcut(keycode : uint, func : Function, pair : String) : void {
	var flags : int = getFlagsFromString(pair);

	if (flags &gt; 0)
		functionMap[keycode + &quot;-&quot; + flags] = func;
}
</pre>
<p>It accepts the same parameters, the only difference is that the <code>pair</code> string is now a dash delimited string, containing the key combinations (i.e. &#8220;<code>ctrl</code>&#8220;, &#8220;<code>ctrl-alt</code>&#8220;, &#8220;<code>shift-ctrl</code>&#8220;, etc).  Those are parsed by a method <code>getFlagsFromString()</code> which I will probably get rid of in favor of some named constants on the <code>ShortcutManager</code> class.  That will eliminate the need for the additional method, and <code>pair</code> (which will be one of said constants) can be added to the key as-is.</p>
<p>The <code>removeShortcut()</code> is very similar.  I haven&#8217;t implemented &#8220;contexts&#8221; yet, but you can visualize how it will be done:</p>
<pre class="brush: as3;">
functionMap[keycode + &quot;-&quot; + flags + &quot;-&quot; + context] = func;
</pre>
<p>Now that there is only one Dictionary, the <code>shortcutHandler()</code> method has become almost trivial.  Here it is, in all of its 5-lined glory:</p>
<pre class="brush: as3;">
public static function shortcutHandler(event : KeyboardEvent) : void {
	var flags : uint = getFlags(event);

	if (functionMap[event.keyCode + &quot;-&quot; + flags] != null)
		(functionMap[event.keyCode + &quot;-&quot; + flags] as Function).apply();
}
</pre>
<p>Like I said&#8230; trivial.</p>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=386</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Client in C++</title>
		<link>http://googolflex.com/?p=322</link>
		<comments>http://googolflex.com/?p=322#comments</comments>
		<pubDate>Tue, 18 Aug 2009 16:15:16 +0000</pubDate>
		<dc:creator>jwd</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[client/server]]></category>
		<category><![CDATA[client server architecture]]></category>
		<category><![CDATA[sockets]]></category>

		<guid isPermaLink="false">http://googolflex.com/?p=322</guid>
		<description><![CDATA[Although a TCP client is fundamentally different from a TCP server, setting up the socket is very much the same.  For a server, we called these methods in this order:
socket()→bind()→listen()→accept(), followed by sequences of read() and write().
For a client we will call these methods in this order:
socket()→connect(), followed by sequences of write() and read() [...]]]></description>
			<content:encoded><![CDATA[<p>Although a TCP client is fundamentally different from a TCP server, setting up the socket is very much the same.  For a server, we called these methods in this order:</p>
<p><code>socket()</code>→<code>bind()</code>→<code>listen()</code>→<code>accept()</code>, followed by sequences of <code>read()</code> and <code>write()</code>.</p>
<p>For a client we will call these methods in this order:</p>
<p><code>socket()</code>→<code>connect()</code>, followed by sequences of <code>write()</code> and <code>read()</code> depending on what your server does.</p>
<p>To illustrate, I will create a simple HTTP client in C++, which has the following usage:</p>
<pre class="brush: bash;">client www.googolflex.com</pre>
<p>It will then make a connection on port 80 of the server specified at the command line, send an HTTP 1.1 request, and then dump the response to the console.</p>
<p>It uses the following variables:</p>
<pre class="brush: cpp;">
int sock;
struct sockaddr_in client;
int PORT = 80;
</pre>
<p>The first thing to do is initialize the socket structure, for this we need the IP address of the host.  You can use the <code>gethostbyname(char *)</code> function to do that, which passes back a <code>hostent</code> structure.  I have encapsulated the socket setup into a method, which shows how to extract the necessary values from the <code>hostent</code> struct, here:</p>
<pre class="brush: cpp;">
void setupSocket(char* hostname) {
	struct hostent * host = gethostbyname(hostname);

	if ( (host == NULL) || (host-&gt;h_addr == NULL) ) {
		cout &lt;&lt; &quot;Error retrieving DNS information.&quot; &lt;&lt; endl;
		exit(1);
	}

	bzero(&amp;client, sizeof(client));
	client.sin_family = AF_INET;
	client.sin_port = htons( PORT );
	memcpy(&amp;client.sin_addr, host-&gt;h_addr, host-&gt;h_length);

	sock = socket(AF_INET, SOCK_STREAM, 0);

	if (sock &lt; 0) {
		cout &lt;&lt; &quot;Error creating socket.&quot; &lt;&lt; endl;
		exit(1);
	}
}
</pre>
<p>If this method executes correctly, then we have a socket, and an address structure that we can use to call <code>connect()</code> with.  Here is my encapsulating method:</p>
<pre class="brush: cpp;">
void connectToHost(char* hostname) {
	if ( connect(sock, (struct sockaddr *)&amp;client, sizeof(client)) &lt; 0 ) {
		close(sock);
		cout &lt;&lt; &quot;Could not connect to &quot; &lt;&lt; hostname &lt;&lt; endl;
		exit(1);
	}
}
</pre>
<p>Finally, we can call <code>send()/write()</code> and <code>read()/recv()</code>.  This is where your protocol would be implemented, if you were to modify this for use with something other than simple HTTP requests.  Here my <code>sendRequest()</code> and <code>getResponse()</code> methods:</p>
<pre class="brush: cpp;">
void sendRequest(char* hostname) {
	string request = &quot;GET / HTTP/1.1\r\nHost: &quot;;
	request+= string(hostname);
	request += &quot;\r\n\r\n&quot;;

	if (send(sock, request.c_str(), request.length(), 0) != (int)request.length()) {
		cout &lt;&lt; &quot;Error sending request.&quot; &lt;&lt; endl;
		exit(1);
	}
}
</pre>
<pre class="brush: cpp;">
void getResponse() {
	char cur;
	while ( read(sock, &amp;cur, 1) &gt; 0 ) {
		cout &lt;&lt; cur;
	}
}
</pre>
<p>In <code>main()</code> I call these four methods in sequence, passing in the hostname parameter where needed.  And that&#8217;s it.  The full source is included at the end of the post.</p>
<p>Socket connections really aren&#8217;t that difficult once you&#8217;ve done it several times.  Next I will be demonstrating how to set up a simple client in Flex using a client.  Many scripting languages have abstracted away much of the difficulty surrounding socket connections (as we will see with Flex) leaving you to focus on implementing your protocol.</p>
<p>Download the source for <a href='http://googolflex.com/wp-content/uploads/2009/07/simpleclient.cpp'>simpleclient</a> here.  It can be compiled as follows:</p>
<pre class="brush: bash;">g++ -Wall -o client simpleclient.cpp</pre>
]]></content:encoded>
			<wfw:commentRss>http://googolflex.com/?feed=rss2&amp;p=322</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
