<?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>nklein software &#187; Articles</title>
	<atom:link href="http://nklein.com/articles/feed/" rel="self" type="application/rss+xml" />
	<link>http://nklein.com</link>
	<description>software development and consulting</description>
	<lastBuildDate>Thu, 22 Dec 2011 04:42:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Dusting off my Growl Library</title>
		<link>http://nklein.com/2011/12/dusting-off-my-growl-library/</link>
		<comments>http://nklein.com/2011/12/dusting-off-my-growl-library/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 04:42:24 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[cl-growl]]></category>
		<category><![CDATA[iolib]]></category>
		<category><![CDATA[ironclad]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[usocket]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1782</guid>
		<description><![CDATA[I&#8217;ve spent the last few hours dusting off my Common Lisp Growl client library. The last time I worked on it was before the Mac Growl Application supported GNTP (Growl Notification Transport Protocol). Today, working on it, I&#8217;m not quite sure what&#8217;s up, but I am not succeeding in communicating with the server using encryption. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent the last few hours dusting off my <a href="http://nklein.com/software/cl-growl/">Common Lisp Growl client library</a>.  The last time I worked on it was before the Mac Growl Application supported GNTP (Growl Notification Transport Protocol).</p>
<p>Today, working on it, I&#8217;m not quite sure what&#8217;s up, but I am not succeeding in communicating with the server using encryption.  I&#8217;ll have to look more closely.  Last time that I worked on it, I extended <a href="http://method-combination.net/lisp/ironclad/">Ironclad</a>, but I never got those changes pushed fully into Ironclad&#8217;s main line.  But, I think I&#8217;m using the same version of Ironclad that I was using when I tested against the Windows Growl Application.  *shrug*</p>
<p>I&#8217;ve also run into a snag with the Callbacks.  Essentially, your Lisp program could get a callback when the user has clicked on your Growl notification.  This actually works except for the fact that I am calling <code class="codecolorer lisp default"><span class="lisp">READ-SEQUENCE</span></code> into a buffer that is longer than the message.  The server, I believe, is supposed to close the socket after the callback.  But, it does not.  So, I am stuck waiting for more bytes that will never come.</p>
<p>Now, I either have to do one of the following:</p>
<ul>
<li>refactor it to use <code class="codecolorer lisp default"><span class="lisp">READ-LINE</span></code> instead</li>
<li>switch from using <a href="http://common-lisp.net/project/usocket/">USocket</a> to using <a href="http://common-lisp.net/project/iolib/">IOLib</a> (and hope that <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">:</span><span style="color: #555;">dont-wait</span></span></code> works as expected)</li>
<li>extend USocket to support <code class="codecolorer lisp default"><span class="lisp">SOCKET-RECEIVE</span></code> even on TCP sockets</li>
</ul>
<p>Anyone have a preference?</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/12/dusting-off-my-growl-library/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>If it quacks like a parabola&#8230;</title>
		<link>http://nklein.com/2011/09/if-it-quacks-like-a-parabola/</link>
		<comments>http://nklein.com/2011/09/if-it-quacks-like-a-parabola/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 00:20:06 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[classical mechanics]]></category>
		<category><![CDATA[differential equations]]></category>
		<category><![CDATA[intuition]]></category>
		<category><![CDATA[special relativity]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1765</guid>
		<description><![CDATA[I am working on a game idea that involves (special) relativistic mechanics instead of classical mechanics. Working out the details that I needed was easy enough if I assumed that: ships had a maximum speed at which they could move relative to my base frame ships could instantly go from stopped to maximum speed or [...]]]></description>
			<content:encoded><![CDATA[<p>I am working on a game idea that involves <a href="http://en.wikipedia.org/wiki/Special_relativity#Relativistic_mechanics">(special) relativistic mechanics</a> instead of <a href="http://en.wikipedia.org/wiki/Classical_mechanics">classical mechanics</a>.  Working out the details that I needed was easy enough if I assumed that:</p>
<ul>
<li>ships had a maximum speed at which they could move relative to my base frame</li>
<li>ships could instantly go from stopped to maximum speed or vice-versa</li>
</ul>
<p>I didn&#8217;t like those assumptions at all.  So, I started playing with the equations for relativity.  In classical mechanics, the rate-of-change of velocity equals the force you&#8217;re applying divided by your mass: <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bdv%7D%7Bdt%7D%20%3D%20%5Cfrac%7BF%7D%7Bm%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{dv}{dt} = \frac{F}{m}" style="vertical-align:-20%;" class="tex" alt="\frac{dv}{dt} = \frac{F}{m}" />.</p>
<p>In special relativity, your mass increases with velocity.  So, that equation becomes: <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7Bd%5Cleft%28%5Cfrac%7Bv%7D%7B%5Csqrt%7B1-v%5E2%7D%7D%5Cright%29%7D%7Bdt%7D%20%3D%20%5Cfrac%7BF%7D%7Bm_0%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{d\left(\frac{v}{\sqrt{1-v^2}}\right)}{dt} = \frac{F}{m_0}" style="vertical-align:-20%;" class="tex" alt="\frac{d\left(\frac{v}{\sqrt{1-v^2}}\right)}{dt} = \frac{F}{m_0}" /> (assuming units where the speed of light is 1 unit of distance per 1 unit of time and <img src="http://l.wordpress.com/latex.php?latex=m_0&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="m_0" style="vertical-align:-20%;" class="tex" alt="m_0" /> is your rest-mass).</p>
<p>For the purposes of this post, I&#8217;m going to assume the simplest initial conditions:  you start motionless and at the origin.  For ease of notation, let <img src="http://l.wordpress.com/latex.php?latex=a%20%3D%20%5Cfrac%7BF%7D%7Bm_0%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="a = \frac{F}{m_0}" style="vertical-align:-20%;" class="tex" alt="a = \frac{F}{m_0}" />.  Solving the above differential equation to get a formula for velocity and solving the resulting differential equation to get the distance <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_1a507c5494969dc6de305770cadc6630.png" title="x" style="vertical-align:-20%;" class="tex" alt="x" /> you&#8217;ve travelled in my base frame by time <img src="http://nklein.com/wp-content/plugins/easy-latex/cache/tex_4e156c4dfd6f5bd0adffc493c64bc7ca.png" title="t" style="vertical-align:-20%;" class="tex" alt="t" />, the answer comes out to:  <img src="http://l.wordpress.com/latex.php?latex=x%28t%29%20%3D%20%5Cfrac%7B1%7D%7Ba%7D%5Cleft%28-1%20%2B%20%5Csqrt%7B1%2Ba%5E2t%5E2%7D%5Cright%29&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x(t) = \frac{1}{a}\left(-1 + \sqrt{1+a^2t^2}\right)" style="vertical-align:-20%;" class="tex" alt="x(t) = \frac{1}{a}\left(-1 + \sqrt{1+a^2t^2}\right)" />.</p>
<p>I have solved this problem at least thirty times in the past two months.  Sometimes I used the simple initial conditions as above.  Sometimes I did it in all of its gory details (including the messy case where the applied force is not aligned with the current velocity).</p>
<p>I got the same answer (well, plus the extra mess when I did the full-on problem) every way that I tried it.</p>
<p>So, why did I do it over and over again?</p>
<p>If this were classical mechanics, the end equation would have been <img src="http://l.wordpress.com/latex.php?latex=x%28t%29%20%3D%20%5Cfrac%7B1%7D%7B2%7Dat%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x(t) = \frac{1}{2}at^2" style="vertical-align:-20%;" class="tex" alt="x(t) = \frac{1}{2}at^2" />.  And, I know that for low velocities, the classical mechanics answer should be almost identical to the special relativity answer.  And, there was no way that I thought <img src="http://l.wordpress.com/latex.php?latex=%5Cfrac%7B1%7D%7Ba%7D%5Cleft%28-1%20%2B%20%5Csqrt%7B1%2Ba%5E2t%5E2%7D%5Cright%29%20%5Capprox%20%5Cfrac%7B1%7D%7B2%7Dat%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="\frac{1}{a}\left(-1 + \sqrt{1+a^2t^2}\right) \approx \frac{1}{2}at^2" style="vertical-align:-20%;" class="tex" alt="\frac{1}{a}\left(-1 + \sqrt{1+a^2t^2}\right) \approx \frac{1}{2}at^2" />.</p>
<p>I knew what the graph <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%20%5Csqrt%7Ba%5E2t%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = \sqrt{a^2t^2}" style="vertical-align:-20%;" class="tex" alt="x = \sqrt{a^2t^2}" /> looked like when <img src="http://l.wordpress.com/latex.php?latex=t%20%5Cge%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="t \ge 0" style="vertical-align:-20%;" class="tex" alt="t \ge 0" />.  It is a straight line.  It doesn&#8217;t look much like the parabola <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%20%5Cfrac%7B1%7D%7B2%7Da%20t%5E2&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = \frac{1}{2}a t^2" style="vertical-align:-20%;" class="tex" alt="x = \frac{1}{2}a t^2" /> at all.  </p>
<p>My assumption was that since <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%20%5Csqrt%7Ba%5E2t%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = \sqrt{a^2t^2}" style="vertical-align:-20%;" class="tex" alt="x = \sqrt{a^2t^2}" /> was a straight line for <img src="http://l.wordpress.com/latex.php?latex=t%20%5Cge%200&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="t \ge 0" style="vertical-align:-20%;" class="tex" alt="t \ge 0" />, then <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%20%5Csqrt%7B1%20%2B%20a%5E2t%5E2%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = \sqrt{1 + a^2t^2}" style="vertical-align:-20%;" class="tex" alt="x = \sqrt{1 + a^2t^2}" /> would be a straight line shifted up one unit and bent (concave-down) a little bit like the graph of <img src="http://l.wordpress.com/latex.php?latex=x%20%3D%20%5Csqrt%7Bat%7D&#038;bg=FFFFCC&#038;fg=000000&#038;s=0" title="x = \sqrt{at}" style="vertical-align:-20%;" class="tex" alt="x = \sqrt{at}" /> is bent.</p>
<p>Boy was I wrong.  Here is a plot of the two together (created with <a href="http://fooplot.com/index.php?&#038;type0=0&#038;type1=0&#038;type2=0&#038;type3=0&#038;type4=0&#038;y0=%28-1%2Bsqrt%281%2B%28x%29%5E2%29%29&#038;y1=0.5%20*%20x%5E2&#038;y2=&#038;y3=&#038;y4=&#038;r0=&#038;r1=&#038;r2=&#038;r3=&#038;r4=&#038;px0=&#038;px1=&#038;px2=&#038;px3=&#038;px4=&#038;py0=&#038;py1=&#038;py2=&#038;py3=&#038;py4=&#038;smin0=0&#038;smin1=0&#038;smin2=0&#038;smin3=0&#038;smin4=0&#038;smax0=2pi&#038;smax1=2pi&#038;smax2=2pi&#038;smax3=2pi&#038;smax4=2pi&#038;thetamin0=0&#038;thetamin1=0&#038;thetamin2=0&#038;thetamin3=0&#038;thetamin4=0&#038;thetamax0=2pi&#038;thetamax1=2pi&#038;thetamax2=2pi&#038;thetamax3=2pi&#038;thetamax4=2pi&#038;ipw=1&#038;ixmin=0&#038;ixmax=0.3&#038;iymin=0&#038;iymax=0.05&#038;igx=0.1&#038;igy=0.01&#038;igl=1&#038;igs=0&#038;iax=1&#038;ila=1&#038;xmin=0&#038;xmax=0.3&#038;ymin=0&#038;ymax=0.05">fooplot</a>).  The red line is the classical mechanics version.  The black line is the relativistic version.  Here, the force is such that the body is accelerating at a rate of <q>the speed of light per second</q> so they&#8217;ve already gotten up to around 28,000 miles per second before you can see any separation in the graphs here.</p>
<p><a href="http://nklein.com/wp-content/uploads/2011/09/special-acceleration.png"><img src="http://nklein.com/wp-content/uploads/2011/09/special-acceleration.png" alt="distance (in light-seconds) vs. time (in seconds)" title="special-acceleration" width="500" height="300" class="aligncenter size-full wp-image-1766" /></a></p>
<p>Definitely, I can see the resemblance.  Now, to fix my intuition about square-roots.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/09/if-it-quacks-like-a-parabola/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Keeping Server and Client Separate</title>
		<link>http://nklein.com/keeping-server-and-client-separate</link>
		<comments>http://nklein.com/keeping-server-and-client-separate#comments</comments>
		<pubDate>Thu, 08 Sep 2011 19:51:33 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[client-server]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[literate programming]]></category>
		<category><![CDATA[userial]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1758</guid>
		<description><![CDATA[The problem Whenever I write client-server applications, I run into the same problem trying to separate the code. To send a message from the server to the client, the server has to serialize that message and the client has to unserialize that message. The server doesn&#8217;t need to unserialize that message. The client doesn&#8217;t need [...]]]></description>
			<content:encoded><![CDATA[<h3>The problem</h3>
<p>Whenever I write client-server applications, I run into the same problem trying to separate the code.  To send a message from the server to the client, the server has to serialize that message and the client has to unserialize that message.  The server doesn&#8217;t need to unserialize that message.  The client doesn&#8217;t need to serialize that message.</p>
<p>It seems wrong to include both the serialization code and the unserialization code in both client and server when each side will only be using 1/2 of that code.  On the other hand, it seems bad to keep the serialization and unserialization code in separate places.  You don&#8217;t want one side serializing A+B+C and the other side trying to unserialize A+C+D+B.</p>
<h3>One approach: data classes</h3>
<p>Some projects deal with this situation by making every message have its own data class.  You take all of the information that you want to be in the message and plop it into a data class.  You then serialize the data class and send the resulting bytes.  The other side unserializes the bytes into a data class and plucks the data out of the data class.</p>
<p>The advantage here is that you can have some metaprogram read the data class definition and generate a serializer or unserializer as needed.  You&#8217;re only out-of-sync if one side hasn&#8217;t regenerated since the data class definition changed.</p>
<p>The disadvantage here is that I loathe data classes.  If my top-level interface is going to be <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>send-login username password<span style="color: #66cc66;">&#41;</span></span></code>, then why can&#8217;t I just serialize straight from there without having to create a dippy data structure to hold my opcode and two strings?</p>
<h3>Another approach: suck it up</h3>
<p>Who cares if the client contains both the serialization and unserialization code?  Heck, if you&#8217;re really all that concerned, then <code class="codecolorer lisp default"><span class="lisp">fmakunbound</span></code> half the universe before you <code class="codecolorer lisp default"><span class="lisp">save-lisp-and-die</span></code>.</p>
<p>Of course, unless you&#8217;re using data classes, you&#8217;re either going to have code in your client that references a bunch of functions and variables that only exist in your server or your client and server will be identical except for:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> main <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
  #+server <span style="color: #66cc66;">&#40;</span>server-main<span style="color: #66cc66;">&#41;</span>
  #-server <span style="color: #66cc66;">&#40;</span>client-main<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Now, of course, your server is going to accidentally depend on OpenGL and OpenAL and SDL and a whole bunch of other -L&#8217;s it never actually calls.  Meanwhile, your client is going to accidentally depend on Postmodern and Portable-Threads and a whole bunch of other Po-&#8217;s it never actually calls.</p>
<h3>Another approach: tangle and weave, baby</h3>
<p>Another way that I&#8217;ve got around this is to use literate programming tools to let me write the serialiization and unserialization right next to each other in my document.  Then, anyone going to change the serialize code would be immediately confronted with the unserialize code that goes with it.</p>
<p>The advantage here is that you can tangle the client code through an entirely separate path than the server code keeping only what you need in each.</p>
<p>The disadvantage here is that now both your client code and your server code have to be in the same document or both include the same sizable chunk of document.  And, while there aren&#8217;t precisely name-capturing problems, trying to include the &#8220;serialize-and-send&#8221; chunk in your function in the client code still requires that you use the same variable names that were in that chunk.</p>
<h3>How can Lisp make this better?</h3>
<p>In Lisp, we can get the benefits of a data-definition language and data classes without needing the data classes.  Here&#8217;s a snippet of the data definition for a simple client-server protocol.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;;; protocol.lisp</span>
<span style="color: #66cc66;">&#40;</span>userial<span style="color: #66cc66;">:</span><span style="color: #555;">make-enum-serializer</span> <span style="color: #66cc66;">:</span><span style="color: #555;">opcode</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">ping</span> <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defmessage <span style="color: #66cc66;">:</span><span style="color: #555;">ping</span>     <span style="color: #66cc66;">:</span><span style="color: #555;">uint32</span> ping-payload<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defmessage <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span> <span style="color: #66cc66;">:</span><span style="color: #555;">uint32</span> ping-payload<span style="color: #66cc66;">&#41;</span></pre></div>
<p>I&#8217;ve declared there are two different types of messages, each with their own opcode.  Now, I have macros for <code class="codecolorer lisp default"><span class="lisp">define-sender</span></code> and <code class="codecolorer lisp default"><span class="lisp">define-handler</span></code> that allow me to create functions which have no control over the actual serialization and unserialization.  My functions can only manipulate the named message parameters (the value of <code class="codecolorer lisp default"><span class="lisp">ping-payload</span></code> in this case) before serialization or after unserialization but cannot change the serialization or unserialization itself.</p>
<p>With this protocol, the client side has to handle ping messages by sending ping-ack messages.  The <code class="codecolorer lisp default"><span class="lisp">define-sender</span></code> macro takes the opcode of the message (used to identify the message fields), the name of the function to create, the argument list for the function (which may include declarations for some or all of the fields in the message), the form to use for the address to send the resulting message to, and any body needed to set fields in the packet based on the function arguments before the serialization.  The <code class="codecolorer lisp default"><span class="lisp">define-handler</span></code> macro takes the opcode of the message (again, used to identify the message fields), the name of the function to create, the argument list for the function, the form to use for the buffer to unserialize, and any body needed to act on the unserialized message fields.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;;; client.lisp</span>
<span style="color: #66cc66;">&#40;</span>define-sender  <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span> send-ping-ack <span style="color: #66cc66;">&#40;</span>ping-payload<span style="color: #66cc66;">&#41;</span> *server-address*<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>define-handler <span style="color: #66cc66;">:</span><span style="color: #555;">ping</span>     handle-ping   <span style="color: #66cc66;">&#40;</span>buffer<span style="color: #66cc66;">&#41;</span> buffer
  <span style="color: #66cc66;">&#40;</span>send-ping-ack ping-payload<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>The server side has a bit more work to do because it&#8217;s going to generate the sequence numbers and track the round-trip ping times.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">;;;; server.lisp</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defvar *last-ping-payload* <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defvar *last-ping-time*    <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-sender <span style="color: #66cc66;">:</span><span style="color: #555;">ping</span> send-ping <span style="color: #66cc66;">&#40;</span>who<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>get-address-of who<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>incf *last-ping-payload*<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> *last-ping-time*    <span style="color: #66cc66;">&#40;</span>get-internal-real-time<span style="color: #66cc66;">&#41;</span>
        ping-payload        *last-ping-payload*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>define-handler <span style="color: #66cc66;">:</span><span style="color: #555;">ping-ack</span> handle-ping-ack <span style="color: #66cc66;">&#40;</span>who buffer<span style="color: #66cc66;">&#41;</span> buffer
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> ping-payload *last-ping-payload*<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>update-ping-time who <span style="color: #66cc66;">&#40;</span>- <span style="color: #66cc66;">&#40;</span>get-internal-real-time<span style="color: #66cc66;">&#41;</span> *last-ping-time*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Problems with the above</h3>
<p>It feels strange to leave compile-time artifacts like the names and types of the message fields in the code after I&#8217;ve generated the functions that I&#8217;m actually going to use.  But, I guess that&#8217;s just part of Lisp development.  You can&#8217;t (easily) unload a package.  I can <code class="codecolorer lisp default"><span class="lisp">makunbound</span></code> a bunch of stuff after I&#8217;m loaded if I don&#8217;t want it to be convenient to modify senders or handlers at run-time.</p>
<p>There is intentional name-capture going on.  The names of the message fields become names in the handlers.  The biggest problem with this is that the <code class="codecolorer lisp default"><span class="lisp">defmessage</span></code> calls really have to be in the same namespace as the <code class="codecolorer lisp default"><span class="lisp">define-sender</span></code> and <code class="codecolorer lisp default"><span class="lisp">define-handler</span></code> calls.</p>
<p>I still have some work to do on my macros to support <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>key</span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>optional</span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>aux</span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&amp;</span>rest</span></code> arguments properly.  I will post those macros once I&#8217;ve worked out those kinks.</p>
<p>Anyone care to share how they&#8217;ve tackled client-server separation before?</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/keeping-server-and-client-separate/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Blackthorn 3D &#8212; Lisp game engine using USerial</title>
		<link>http://nklein.com/2011/06/blackthorn-3d-lisp-game-engine-using-userial/</link>
		<comments>http://nklein.com/2011/06/blackthorn-3d-lisp-game-engine-using-userial/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 23:35:00 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[userial]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1751</guid>
		<description><![CDATA[Elliott Slaughter announced Blackthorn 3D, yesterday. Blackthorn 3D is a game engine crafted in Lisp using LispbuilderSDL and cl-opengl for graphics and usocket and userial for network play.]]></description>
			<content:encoded><![CDATA[<p>Elliott Slaughter announced <a href="http://elliottslaughter.com/2011/06/blackthorn-3d">Blackthorn 3D</a>, yesterday.  Blackthorn 3D is a game engine crafted in Lisp using <a href="http://code.google.com/p/lispbuilder/wiki/LispbuilderSDL">LispbuilderSDL</a> and <a href="http://common-lisp.net/project/cl-opengl/">cl-opengl</a> for graphics and <a href="http://common-lisp.net/project/usocket/">usocket</a> and <a href="http://nklein.com/software/unet/userial/">userial</a> for network play.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/06/blackthorn-3d-lisp-game-engine-using-userial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C- in MacOSX&#8217;s Terminal.app</title>
		<link>http://nklein.com/2011/06/c-in-macosxs-terminal-app/</link>
		<comments>http://nklein.com/2011/06/c-in-macosxs-terminal-app/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 06:25:24 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1747</guid>
		<description><![CDATA[After tonight&#8217;s TC Lispers Meeting, I had a renewed interest in figuring out why C-&#60;right arrow&#62; didn&#8217;t work for me in Org-Mode or Paredit. After a whole bunch of running in circles, I have discovered a combination that works (with these clues). I have my TERM variable set to xterm-color. I configured the Terminal.app using [...]]]></description>
			<content:encoded><![CDATA[<p>After tonight&#8217;s <a href="http://tclispers.org/events/june-meeting-emacs-theme">TC Lispers Meeting</a>, I had a renewed interest in figuring out why C-&lt;right arrow&gt; didn&#8217;t work for me in Org-Mode or Paredit.</p>
<p>After a whole bunch of running in circles, I have discovered a combination that works (with <a href="http://marc-abramowitz.com/archives/2006/10/05/ctrl-left-and-ctrl-right-in-bash-and-emacs/">these clues</a>).  I have my <code class="codecolorer text default"><span class="text">TERM</span></code> variable set to <code class="codecolorer text default"><span class="text">xterm-color</span></code>.  I configured the Terminal.app using its Keyboard settings to have it send the string &#8220;\033[1;5C&#8221; for C-&lt;right arrow&gt; and &#8220;\033[1;5D&#8221; for C-&lt;left arrow&gt;.  (The &#8220;\033&#8243; is the escape key.)</p>
<p>This works for me even through <code class="codecolorer text default"><span class="text">screen</span></code>.</p>
<p>Bonus.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/06/c-in-macosxs-terminal-app/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Quicklisp Win:  Weblocks</title>
		<link>http://nklein.com/2011/06/quicklisp-win-weblocks/</link>
		<comments>http://nklein.com/2011/06/quicklisp-win-weblocks/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 21:31:02 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[quicklisp]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1732</guid>
		<description><![CDATA[I was recently reminded of a presentation that I gave last year about Weblocks. Here&#8217;s one slide from that presentation: Today, I started from scratch in an empty account on an eight-year-old Linux box. I installed Quicklisp and used it to install Weblocks. I had a Weblocks server up and running in under three minutes.]]></description>
			<content:encoded><![CDATA[<p>I was recently reminded of <a href="http://nklein.com/2010/04/tc-lispers-april-presentations-online/">a presentation</a> that I gave last year about <a href="http://common-lisp.net/project/cl-weblocks/">Weblocks</a>.</p>
<p>Here&#8217;s one slide from that presentation:<br />
<a href="http://nklein.com/wp-content/uploads/2011/06/weblocks-deps.png"><img src="http://nklein.com/wp-content/uploads/2011/06/weblocks-deps.png" alt="" title="weblocks-deps" width="650" height="484" class="aligncenter size-full wp-image-1733" /></a></p>
<p>Today, I started from scratch in an empty account on an eight-year-old Linux box.  I installed <a href="http://quicklisp.org/">Quicklisp</a> and used it to install Weblocks.  I had a Weblocks server up and running in under three minutes.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/06/quicklisp-win-weblocks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Method Versions &#8212; v0.1.2011.05.18</title>
		<link>http://nklein.com/2011/05/method-versions-v0-1-2011-05-18-2/</link>
		<comments>http://nklein.com/2011/05/method-versions-v0-1-2011-05-18-2/#comments</comments>
		<pubDate>Thu, 19 May 2011 04:00:50 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[New Versions]]></category>
		<category><![CDATA[Releases]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[method-versions]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1702</guid>
		<description><![CDATA[Edit: After re-reading some of the ContextL papers, I believe that I am actually just going to use ContextL as it&#8217;s a much more flexible superset of this library. I will probably still keep this library published as an example of a non-trivial, but glarkable, method combination. I am releasing a new library that allows [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><b>Edit: </b> After re-reading some of the ContextL papers, I believe that I am actually just going to use ContextL as it&#8217;s a much more flexible superset of this library.  I will probably still keep this library published as an example of a non-trivial, but glarkable, method combination.</p></blockquote>
<p>I am releasing a new library that allows one to dispatch generic methods based on the value of a global parameter.</p>
<p>There are situations where one might like to dispatch a method on some information other than the required parameters of the method.  For many situations, it is sufficient to switch between those methods based on some external parameter.  The <a href="http://nklein.com/software/method-versions/">method-versions</a> library allows one to do just that.</p>
<h4><a name="obtaining">Obtaining</a></h4>
<ul>
<li>The home page: <a href="http://nklein.com/software/method-versions/">http://nklein.com/software/method-versions/</a></li>
<li>The tar-ball: <a href="http://nklein.com/wp-content/uploads/2011/05/method-versions_0.1.2011.05.18.tar.gz">method-versions_0.1.2011.05.18.tar.gz</a></li>
<li>The GPG signature for the tar-ball: <a href="http://nklein.com/wp-content/uploads/2011/05/method-versions_0.1.2011.05.18.tar.gz.asc">method-versions_0.1.2011.05.18.tar.gz.asc</a></li>
<li>The main git repository: <a href="http://git.nklein.com/lisp/libs/method-versions.git">http://git.nklein.com/lisp/libs/method-versions.git</a></li>
<li>A browsable mirror of the git repository: <a href="http://github.com/nklein/method-versions">http://github.com/nklein/method-versions</a></li>
</ul>
<h4><a name="internationalization">Internationalization Example</a></h4>
<p>In this example, we do a silly form of internationalization. To that end, we will use English as the default language and define some other languages.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"> <span style="color: #66cc66;">&#40;</span>method-versions<span style="color: #66cc66;">:</span><span style="color: #555;">define-method-version</span> latin<span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>method-versions<span style="color: #66cc66;">:</span><span style="color: #555;">define-method-version</span> pig-latin<span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>method-versions<span style="color: #66cc66;">:</span><span style="color: #555;">define-method-version</span> french latin<span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>method-versions<span style="color: #66cc66;">:</span><span style="color: #555;">define-method-version</span> spanish latin<span style="color: #66cc66;">&#41;</span></pre></div>
<p>We will prepare a language parameter and a welcome method that is versioned on the language.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"> <span style="color: #66cc66;">&#40;</span>declaim <span style="color: #66cc66;">&#40;</span>special *language*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>defparameter *language* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
 <span style="color: #66cc66;">&#40;</span>defgeneric welcome <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">method-combination</span> method-versions<span style="color: #66cc66;">:</span><span style="color: #555;">method-version-method-combination</span>
                        *language*<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>And, we define welcome methods for the various languages (accidentally forgetting <code class="codecolorer lisp default"><span class="lisp">spanish</span></code>).</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"> <span style="color: #66cc66;">&#40;</span>defmethod welcome <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">welcome</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>defmethod welcome <span style="color: #66cc66;">:</span><span style="color: #555;">latin</span>     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">velkominum</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>defmethod welcome <span style="color: #66cc66;">:</span><span style="color: #555;">pig-latin</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">elcomeway</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">&#40;</span>defmethod welcome <span style="color: #66cc66;">:</span><span style="color: #555;">french</span>    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">bonjour</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Then, we will try each of the languages in turn.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapcar</span> #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>ll<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>*language* ll<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
               <span style="color: #66cc66;">&#40;</span>welcome<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         '<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span> <span style="color: #66cc66;">:</span><span style="color: #555;">latin</span> <span style="color: #66cc66;">:</span><span style="color: #555;">pig-latin</span> <span style="color: #66cc66;">:</span><span style="color: #555;">french</span> <span style="color: #66cc66;">:</span><span style="color: #555;">spanish</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
 <span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">welcome</span> <span style="color: #66cc66;">:</span><span style="color: #555;">velkominum</span> <span style="color: #66cc66;">:</span><span style="color: #555;">elcomeway</span> <span style="color: #66cc66;">:</span><span style="color: #555;">bonjour</span> <span style="color: #66cc66;">:</span><span style="color: #555;">velkominum</span><span style="color: #66cc66;">&#41;</span></pre></div>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/05/method-versions-v0-1-2011-05-18-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Delayed Evaluation Across Packages</title>
		<link>http://nklein.com/2011/04/delayed-evaluation-across-packages/</link>
		<comments>http://nklein.com/2011/04/delayed-evaluation-across-packages/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 02:17:08 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[unet]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1666</guid>
		<description><![CDATA[For my networking layer library, I wanted to provide ubiquitous logging. At the same time, I did not want to tie the application to my choice of logging library. I wanted the user to be able to pass me a function where I could give them a logging category and something to log. &#40;in-package :unet&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>For my <a href="http://nklein.com/software/unet/">networking layer library</a>, I wanted to provide ubiquitous logging.  At the same time, I did not want to tie the application to my choice of logging library.  I wanted the user to be able to pass me a function where I could give them a logging category and something to log.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>in-package <span style="color: #66cc66;">:</span><span style="color: #555;">unet</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defvar *logger-<span style="color: #b1b100;">function</span>* <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> log-it <span style="color: #66cc66;">&#40;</span>category thing-to-log<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> *logger-<span style="color: #b1b100;">function</span>*
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> *logger-<span style="color: #b1b100;">function</span>* <span style="color: #66cc66;">,</span>category <span style="color: #66cc66;">,</span>thing-to-log<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>This seems simple enough, right?  Now, throughout my code, I can do things like:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>log-it <span style="color: #66cc66;">:</span><span style="color: #555;">incoming-packet</span> packet<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>log-it <span style="color: #66cc66;">:</span><span style="color: #555;">list-of-unacked-packets</span> <span style="color: #66cc66;">&#40;</span>get-list-of-unacked-packets<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>etc<span style="color: #66cc66;">&#41;</span></pre></div>
<p>The application can register a <code class="codecolorer lisp default"><span class="lisp">*logger-<span style="color: #b1b100;">function</span>*</span></code> something like this:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> app-log-network-msgs <span style="color: #66cc66;">&#40;</span>category thing-to-log<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>cl-log<span style="color: #66cc66;">:</span><span style="color: #555;">log-message</span> category thing-to-log<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Here&#8217;s the problem though:  most (all?) logging libraries are good about not evaluating any arguments beyond the <code class="codecolorer text default"><span class="text">category</span></code> unless something is actually listening for messages of that category.  This makes it reasonable to do stuff like this and only take the speed hit when it&#8217;s actually important to do so:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>log-it <span style="color: #66cc66;">:</span><span style="color: #555;">excruciating-detail</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">mapcar</span> #'get-excrutiating-detail <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">append</span> everyone everything<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>With my macro above, I have no way of knowing whether something is listening on a particular category or not.  Further, most logging libraries don&#8217;t offer a way to query that sort of information.</p>
<h3>What to do?</h3>
<p>What I wanted to do was to pass a macro from the application package into my networking library instead of passing a function.  I spent way too long trying to find a way to make this work (especially considering I ran into the same trouble in <a href="http://nklein.com/2009/10/lisp-troubles-fabricating-a-closure/">October, 2009</a> trying to use some combination of <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span>macroexpand <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> and <code class="codecolorer lisp default"><span class="lisp"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">eval</span> <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span></span></code> to let the caller decide which forms to execute).</p>
<p>All it took was posting to <a href="http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/a63a10394350cc7e/c06e26bb6dc1a20e#c06e26bb6dc1a20e">comp.lang.lisp</a> to answer my own question:  <b>Closures</b>.  I changed my macro to create a closure:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> log-it <span style="color: #66cc66;">&#40;</span>category thing-to-log<span style="color: #66cc66;">&#41;</span>
  `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> *logger-<span style="color: #b1b100;">function</span>*
     <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> *logger-<span style="color: #b1b100;">function</span>* <span style="color: #66cc66;">,</span>category #'<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">,</span>thing-to-log<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Now, the application&#8217;s logger function changes slightly and my forms are only evaluated when the logging library evaluates them:</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> app-log-network-msgs <span style="color: #66cc66;">&#40;</span>category thing-to-log-generator<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>cl-log<span style="color: #66cc66;">:</span><span style="color: #555;">log-message</span> category <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">funcall</span> thing-to-log-generator<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Hopefully, I will remember next time I run into this.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/04/delayed-evaluation-across-packages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Binary Logging with CL-Log</title>
		<link>http://nklein.com/2011/04/binary-logging-with-cl-log/</link>
		<comments>http://nklein.com/2011/04/binary-logging-with-cl-log/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 03:47:30 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[quicklisp]]></category>
		<category><![CDATA[unet]]></category>
		<category><![CDATA[userial]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1640</guid>
		<description><![CDATA[One of the things that my current work does better than anywhere I&#8217;ve worked before is logging. When something goes wrong, there is a log file that you can dig through to find all kinds of information about what you were doing and how things were going. As I move forward programming a game with [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that my current work does better than anywhere I&#8217;ve worked before is logging.  When something goes wrong, there is a log file that you can dig through to find all kinds of information about what you were doing and how things were going.</p>
<p>As I move forward programming a game with my <a href="http://nklein.com/software/unet/">UNet</a> library, I want to make sure that I can easily log all the network traffic during testing runs at least.</p>
<p>In looking through the various <a href="http://cliki.net/logging">Lisp logging</a> packages out there, I decided on Nick Levine&#8217;s <a href="http://www.nicklevine.org/cl-log/">cl-log</a> library.</p>
<p>I installed it in no time with <a href="http://quicklisp.org/">quicklisp</a>.</p>
<p>Then, I set to work trying to figure out how I could use it to log binary data.</p>
<p>Here&#8217;s what I ended up with.  If you want to do something similar, this should give you a good starting point.</p>
<h3>Serializing, unserializing, and categorizing</h3>
<p>With my <a href="http://nklein.com/software/unet/userial/">USerial</a> library, I defined a serializer to keep track of the different categories of log messages.  And, I made corresponding categories in cl-log.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>make-enum-serializer <span style="color: #66cc66;">:</span><span style="color: #555;">log-category</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">packet</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">error</span> <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">:</span><span style="color: #555;">info</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #555;">packet</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #b1b100;">error</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">error</span> <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defcategory <span style="color: #66cc66;">:</span><span style="color: #555;">info</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">or</span> <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">:</span><span style="color: #555;">info</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Specializing the classes</h3>
<p>There are two major classes that I specialized: <b>base-message</b> and <b>base-messenger</b>.  For my toying around, I didn&#8217;t end up adding any functionality to the <b>base-message</b> class.  I will show it here though so that you know you can do it.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defclass serialized-message <span style="color: #66cc66;">&#40;</span>base-message<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defclass serialized-messenger <span style="color: #66cc66;">&#40;</span>base-messenger<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>filename <span style="color: #66cc66;">:</span><span style="color: #555;">initarg</span> <span style="color: #66cc66;">:</span><span style="color: #555;">filename</span> <span style="color: #66cc66;">:</span><span style="color: #555;">reader</span> serialized-messenger-filename<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Then, I overrode the <b>messenger-send-message</b> generic function to create a binary header with my <a href="http://nklein.com/software/unet/userial/">USerial</a> library and then write the header and the message out.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>defmethod messenger-send-message <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>messenger serialized-messenger<span style="color: #66cc66;">&#41;</span>
                                   <span style="color: #66cc66;">&#40;</span>message serialized-message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>header <span style="color: #66cc66;">&#40;</span>make-buffer <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>serialize* <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">uint64</span> <span style="color: #66cc66;">&#40;</span>timestamp-universal-time
                              <span style="color: #66cc66;">&#40;</span>message-timestamp message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">:</span><span style="color: #555;">log-category</span> <span style="color: #66cc66;">&#40;</span>message-category message<span style="color: #66cc66;">&#41;</span>
                 <span style="color: #66cc66;">:</span><span style="color: #555;">uint64</span> <span style="color: #66cc66;">&#40;</span>buffer-<span style="color: #b1b100;">length</span> <span style="color: #66cc66;">:</span><span style="color: #555;">buffer</span> <span style="color: #66cc66;">&#40;</span>message-description message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">:</span><span style="color: #555;">buffer</span> header<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>with-open-file <span style="color: #66cc66;">&#40;</span>stream <span style="color: #66cc66;">&#40;</span>serialized-messenger-filename messenger<span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">direction</span> <span style="color: #66cc66;">:</span><span style="color: #555;">output</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">if-does-not-exist</span> <span style="color: #66cc66;">:</span><span style="color: #555;">create</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">if-exists</span> <span style="color: #66cc66;">:</span><span style="color: #b1b100;">append</span>
                            <span style="color: #66cc66;">:</span><span style="color: #555;">element-type</span> '<span style="color: #66cc66;">&#40;</span>unsigned-byte <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>write-sequence header stream<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>write-sequence <span style="color: #66cc66;">&#40;</span>message-description message<span style="color: #66cc66;">&#41;</span> stream<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Using it</h3>
<p>To get things going, I then made a log manager that accepts my <b>serialized-message</b> type and started one of my <b>serialized-messenger</b> instances.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">setf</span> <span style="color: #66cc66;">&#40;</span>log-manager<span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>make-instance 'log-manager
                     <span style="color: #66cc66;">:</span><span style="color: #555;">message-class</span> 'serialized-message<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>start-messenger 'serialized-messenger <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span> <span style="color: #ff0000;">&quot;binary-logger&quot;</span>
                                       <span style="color: #66cc66;">:</span><span style="color: #555;">filename</span> <span style="color: #ff0000;">&quot;/tmp/binary-log.dat&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<p>Once these were started, I made a little utility function to make it easy for me to make test messages and then invoked <b>log-message</b> a few times.</p>
<div class="codecolorer-container lisp blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><pre class="lisp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> make-info <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span>serialize <span style="color: #66cc66;">:</span><span style="color: #555;">string</span> string <span style="color: #66cc66;">:</span><span style="color: #555;">buffer</span> <span style="color: #66cc66;">&#40;</span>make-buffer<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>log-message <span style="color: #66cc66;">:</span><span style="color: #555;">warning</span> <span style="color: #66cc66;">&#40;</span>make-info <span style="color: #ff0000;">&quot;Warning&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>log-message <span style="color: #66cc66;">:</span><span style="color: #555;">info</span> <span style="color: #66cc66;">&#40;</span>make-info <span style="color: #ff0000;">&quot;This is info&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div>
<h3>Conclusions</h3>
<p>In all, it has taken me about four times as long to write blog post as it did to install cl-log with quicklisp, peek through the cl-log documentation and source code enough to figure out how to do this, and write all of the code.</p>
<p>To really use this, I will probably separate out the category of a message from the serialized type of the message.  This will probably involve adding a field to the <b>serialized-message</b> class to track the message type, adding an <b>initialize-instance :before</b> method for that class to look through the <b>arguments</b> to pull out the type, and then adding the type as an extra argument to <b>log-message</b>.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/04/binary-logging-with-cl-log/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Wayback Popularity</title>
		<link>http://nklein.com/2011/03/wayback-popularity/</link>
		<comments>http://nklein.com/2011/03/wayback-popularity/#comments</comments>
		<pubDate>Mon, 28 Mar 2011 13:30:42 +0000</pubDate>
		<dc:creator>pat</dc:creator>
				<category><![CDATA[Articles]]></category>

		<guid isPermaLink="false">http://nklein.com/?p=1638</guid>
		<description><![CDATA[Wow. A post that I made in February of 2009 just hit Hacker News yesterday and this one from June of 2009 either hit as well or came along for the ride. Suddenly, I have 3655 hits on a two year old articles. Wacky.]]></description>
			<content:encoded><![CDATA[<p>Wow.  <a href="http://nklein.com/2009/02/sapir-whorf-wit-programming-languages/">A post that I made in February of 2009</a> just hit <a href="http://news.ycombinator.com/">Hacker News</a> yesterday and <a href="http://nklein.com/2009/06/optimizing-lisp-some-more/">this one from June of 2009</a> either hit as well or came along for the ride.  Suddenly, I have 3655 hits on a two year old articles.  Wacky.</p>
]]></content:encoded>
			<wfw:commentRss>http://nklein.com/2011/03/wayback-popularity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

