<?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>Toni Westbrook dot Com &#187; Miscellaneous</title>
	<atom:link href="http://www.toniwestbrook.com/archives/category/miscellaneous/feed" rel="self" type="application/rss+xml" />
	<link>http://www.toniwestbrook.com</link>
	<description>Sharing Software Development Knowledge With You</description>
	<lastBuildDate>Mon, 12 Sep 2011 03:35:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>My Newest Robotic Buddy, Vincent (CompuRobot)</title>
		<link>http://www.toniwestbrook.com/archives/335</link>
		<comments>http://www.toniwestbrook.com/archives/335#comments</comments>
		<pubDate>Sat, 18 Sep 2010 22:22:08 +0000</pubDate>
		<dc:creator>Toni</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://www.toniwestbrook.com/?p=335</guid>
		<description><![CDATA[It&#8217;s not uncommon if you love programming, computers, tinkering, and all things electronic, that you also have a love for robots. I am not unique in this regard! I&#8217;m a fan of programmable bots in general (I have a few of the Lego mindstorm robots, including the newer NXT). Recently, my friend Ryan gave me [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not uncommon if you love programming, computers, tinkering, and all things electronic, that you also have a love for robots.  I am not unique in this regard!  I&#8217;m a fan of programmable bots in general (I have a few of the Lego mindstorm robots, including the newer NXT).  Recently, my friend Ryan gave me this little guy:</p>
<p><center><a href="http://www.toniwestbrook.com/wp-content/uploads/2010/09/1.jpg"><img src="http://www.toniwestbrook.com/wp-content/uploads/2010/09/1.jpg" alt="" title="Vincent 1" style="width:80%; margin-bottom:20px" ></a></center></p>
<p>He bears the name &#8220;COMPUROBOT&#8221;, and as can be seen, shows a striking resemblance to Vincent from Disney&#8217;s The Black Hole.  For this reason, that&#8217;s the name I&#8217;ve chosen for him.<a href="http://www.toniwestbrook.com/wp-content/uploads/2010/09/vincent.jpg"><img src="http://www.toniwestbrook.com/wp-content/uploads/2010/09/vincent.jpg" alt="" title="vincent 3" style="width:25%; margin:10px 10px 7px 0" align="left" /></a>At first glance, Vincent looks like a simple toy &#8211; he has a fun shape, features LEDs for eyes, flashing bulb in his belly, colorful stickers, and a sturdy design.  He is driven by two independent wheels below and supported in the front by two smaller wheels.  However, there is quite a bit more at play than a simple toy that runs around the room.  Examining the top of Vincent reveals a 5&#215;5 Matrix of buttons, each with an icon indicating its function.  It turns out Vincent, the Compurobot, is a programmable robot!</p>
<p><center><a href="http://www.toniwestbrook.com/wp-content/uploads/2010/09/2.jpg"><img src="http://www.toniwestbrook.com/wp-content/uploads/2010/09/2.jpg" style="width:80%; margin-bottom:20px" ></a></center></p>
<p>Though some of the icons seem to suggest movement, I wasn&#8217;t quite sure what all of them meant, or in what the protocol for using them was, so I luckily was able to find the manual online.  </p>
<p><b> Capabilities </b></p>
<p>Vincent features a 4-bit processor and a small amount of RAM that can hold up to 48 commands.  Though its volatile and erases after turning him off, the procedure is very simple for programming.  Simply hit the button of the action (forward, backward, turn, play noise, etc), and then the number of seconds you wish him to perform it.  E.g. hitting FORWARD, 4, LEFT, 3, BACK, 6 and then START (green) would cause him to go forward for 3 seconds, turn left for 3 seconds, then back for 6 seconds.  Sound can be played simultaneously (you turn it on and off with 1 and 3, respectively), so Vincent can make cool noises while charging along.  He even features a 3-gear module with a 9400 RPM motor, which can be programmed as well (icon with the circle and 3 connected lines).  </p>
<p>Though the CPU appears to be very simple without the ability to perform conditional processing (which also leaves out the possibility of looping), a neat little feature it does have is the ability to multiply time amounts.  The X button on the control panel is multiplication &#8211; so if you&#8217;d like Vincent to go forward 48 seconds, you can hit 6 X 8.   Just a nice little added feature!</p>
<p><B> The Atari Connection </B></p>
<p>What could be most neat of all about this little guy?  He was produced by Axlon &#8211; which you may not have heard of.  But Axlon was a company created by Nolan Bushnell, founder of Atari.  Axlon had produced a few such robots, but they never took off like his earlier company did.  </p>
<p>All in all, a very cool present!  Thanks Ryan!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.toniwestbrook.com/archives/335/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun Ways to Sharpen Your CS Skills</title>
		<link>http://www.toniwestbrook.com/archives/285</link>
		<comments>http://www.toniwestbrook.com/archives/285#comments</comments>
		<pubDate>Wed, 08 Sep 2010 02:34:38 +0000</pubDate>
		<dc:creator>Toni</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Thoughts]]></category>

		<guid isPermaLink="false">http://www.toniwestbrook.com/?p=285</guid>
		<description><![CDATA[If you&#8217;re anything like me, once you learned how to code, you would take any chance you could get to write little programs for fun. I remember once I finally &#8220;got&#8221; BASIC on my Commodore 64 growing up, I would spend hours writing the cheeziest (looking back) programs. A favorite of mine was writing countless [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re anything like me, once you learned how to code, you would take any chance you could get to write little programs for fun.  I remember once I finally &#8220;got&#8221; BASIC on my Commodore 64 growing up, I would spend hours writing the cheeziest (looking back) programs.  A favorite of mine was writing countless &#8220;Question and Answer&#8221; programs, where the computer would ask &#8220;How are you?&#8221; and depending on your answer, the program would issue a different (and probably inappropriate given my age at the time) response.  </p>
<p><b> Time Marches On </b></p>
<p>As time went on though, and I got better at programming, learned the fundamentals, studied Computer Science &#8211; things started to change.  I still loved to program, but my goals became larger and more complex.  Pet projects would take days to complete, then weeks, then months.  Once I started doing it professionally, that added a whole new level, where the projects were for money, and project management, sustainability, fiscal viability, etc, all became factors.  I had to specialize and focus on specific areas to remain competitive.  And the technology changed &#8211; whereas once I was communicating directly with the processor and memory I/O, now I was communicating 17 levels up to a a COM object or framework API.  It was just a different ballgame &#8211; which isn&#8217;t necessarily a bad thing, but sometimes working at such a high level for different purposes can make you lose site of the underlying CS.  Sometimes it&#8217;s important to keep your CS skills as fresh and sharp as your software engineering ones.</p>
<p><b> Some Fun Ways to Up Your CS Game </b></p>
<p>Luckily, there are some sites out there that are awesome for keeping those little grey CS cells active in your head.  </p>
<p><b><a href="http://projecteuler.net/">Project Euler</a></b><br />
This is by far my favorite one.  Euler offers a large number of problems (currently 300) that require a computer program to solve.  They are generally geared toward mathematics of different levels and areas (generally the higher the problem, the more difficult it is), and you can solve them using any method or programming language you wish.  The website keeps track of how many you&#8217;ve solved and how you&#8217;re faring with the rest of the members, but really you&#8217;re competing with yourself to write the best program you can.  As you get into later problems, even your efficiency matters, as your first solution might take 3 days to complete, whereas the better one takes .25 seconds.  They have discussion forums for each problem as well (once you&#8217;ve solved it), where people show their solutions and help each other out.  Some example problems on the site:</p>
<p>1. Add all the natural numbers below one thousand that are multiples of 3 or 5.<br />
7. Find the 10001st prime.<br />
15. Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?<br />
109. How many distinct ways can a player checkout in the game of darts with a score of less than 100?<br />
157. Solving the diophantine equation 1/a+1/b= p/10n</p>
<p>As you can see, there are a large range of problems targeting different areas and algorithms.  I&#8217;ve solved 34 to date &#8211; sometimes I&#8217;ll spend a lunch hour working on a problem, they&#8217;re great fun and you can do them at your own pace &#8211; and learn new techniques in the process.</p>
<p><b><a href="http://www.hackthissite.org/">Hack This Site!</a></b><br />
No, that wasn&#8217;t an invitation!  Hack This Site . Org is an interesting site that offers a number of security, reverse engineering, and application development missions.  While I&#8217;m actually against the practice of unauthorized computer access (especially being a IT Manager by day) &#8211; penetration testing is a great thing for a network administrator to know, and reverse engineering is a fantastic thing for a programmer to know.  In the high-level development world we now live in, getting back to the processor and memory level is definitely a plus &#8211; and studying the binary structure of an executable certainly achieves that.  Not only does it strengthen your machine language skills, but it also gives you great insight into how compilers work, how operating systems link DLLs, management memory, and achieve IPC.</p>
<p><b> Many More </b></p>
<p>Project Euler and Hack This Site are the two I focus on (and between the two of them, there are enough problems to keep me busy for years), but if they aren&#8217;t your cup of tea, here are a list of other programming problem related sites:</p>
<p><a href="http://bright-shadows.net/">Bright Shadows</a><br />
<a href="http://www.caesum.com/game/">Electrica</a><br />
<a href="http://www.programming-challenges.com">Programming Challenges</a><br />
<a href="http://www.pythonchallenge.com/">Python Challenge</a><br />
<a href="http://www.topcoder.com/">TopCoder</a></p>
<p>Have fun, and get your CS in shape!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.toniwestbrook.com/archives/285/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ping Failover Daemon for Linux</title>
		<link>http://www.toniwestbrook.com/archives/184</link>
		<comments>http://www.toniwestbrook.com/archives/184#comments</comments>
		<pubDate>Thu, 25 Feb 2010 04:16:28 +0000</pubDate>
		<dc:creator>Toni</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Network Administration]]></category>

		<guid isPermaLink="false">http://www.toniwestbrook.com/archives/184</guid>
		<description><![CDATA[Overview I wanted to make available a GPL daemon I developed for Linux called the &#8220;ping failover daemon&#8221;, or pfailover. It is designed for hosts with two or more network interfaces, with the goal of rerouting traffic over the secondary interface when the primary fails. It achieves this by monitoring a host over the primary [...]]]></description>
			<content:encoded><![CDATA[<p><b> Overview </b></p>
<p>I wanted to make available a GPL daemon I developed for Linux called the &#8220;ping failover daemon&#8221;, or pfailover.  It is designed for hosts with two or more network interfaces, with the goal of rerouting traffic over the secondary interface when the primary fails.  It achieves this by monitoring a host over the primary connection via ping, and changing the route tables when it doesn&#8217;t receive a response.  In this way, it is smart enough to reroute if any hop along the way fails, as opposed to rerouting only under the circumstances of a link-loss.  When it starts receiving responses to the host over the primary interface again, it restores the route tables, thereby activating the primary connection again.</p>
<p>The daemon also runs scripts whenever a connection is changed, so you can insert any functionality you want, such as sending out a warning email to the IT team saying something&#8217;s up.</p>
<p>Additionally, it allows you to setup as many monitors as you&#8217;d like, if you have complex setups with 3 or more network interfaces, or reroute in a different fashion depending on which monitored host goes down.</p>
<p>For programmers and scripters, it also allows full monitoring and control via the command line and shared memory, allowing other programs to integrate its functionality.</p>
<p><b> The Reason for Development </b></p>
<p>A few months back, we ran into an interesting situation at work.  We had only had T1s for an Internet connection for the longest time, but we decided since broadband was so cheap for the bandwidth, we would also get a cable modem &#8211; mainly to be used for staff web traffic.  At the same time, it was a great opportunity to setup a proxy server as the gateway to this new, speedy connection.  Not only would this give us an additional speed boost due to caching, but would also allow us to do some management over web use.  </p>
<p>As anyone with a cable modem knows (well, at least with Comcast) &#8211; connection loss and downtime are not questions of if, but rather when &#8211; and when I say &#8220;when&#8221;, I really mean how many times a week.  Which is fine &#8211; there is a reason why organizations still go with T-carriers and not just broadband connections &#8211; they&#8217;re more expensive, but more reliable as well.  </p>
<p>Anyway, with this in mind, we knew that it was just a matter of time before the proxy server lost its connection to the Internet via the cable modem, and staff would start complaining about Internet loss.  And at the airport, uptime is a big deal, which is especially difficult being a 24&#215;7 operation.  While there are a few strategies on how to handle this, I decided I wanted a simple solution &#8211; the proxy server would simply reroute its web requests back out the internal network connection to the T1s, instead of to the cable modem connection.  Then when the cable modem came back online, it would start routing back out that interface again.</p>
<p>I found some other packages to do this, but they were all very robust, complex, and just too big for what I wanted &#8211; I wanted a lightweight daemon with scripting ability, so I could start out simple, and grow it complex if necessary.  So I decided it would be a fun project to code one up in C++ &#8211; I rarely get to write any C++ code anymore, so I take the opportunity when I can.</p>
<p><b> Installation and Usage </b></p>
<p>You will need the lastest version of the boost libraries to compile pfailover.  The installer includes sample conf and script files to aid in setup &#8211; plus it&#8217;s fairly straightforward and should only take a few minutes to configure.  You can see all the options by typing &#8220;pfailover &#8211;help&#8221;.  Normally, after configuring it, you&#8217;ll want to run it as a daemon with the &#8220;pfailover -d&#8221; command.  Once running, you can check the current status at any time by typing &#8220;pfailover -s=get:0&#8243;.</p>
<p><a href='http://www.toniwestbrook.com/wp-content/uploads/2010/02/pfailover-041.tgz' title='pfailover 0.4.1'>Download pfailover 0.4.1</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.toniwestbrook.com/archives/184/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dynamically Truncate SQL Parameters in .NET</title>
		<link>http://www.toniwestbrook.com/archives/59</link>
		<comments>http://www.toniwestbrook.com/archives/59#comments</comments>
		<pubDate>Mon, 10 Mar 2008 04:25:10 +0000</pubDate>
		<dc:creator>Toni</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://www.toniwestbrook.com/archives/59</guid>
		<description><![CDATA[[SQL Server] String or Binary data would be truncated. Run into this error before? The cause is usually fairly clear, we’re attempting to insert or update a field in a table with more data than it can hold. E.g. we’ve executed a SQL insert statement that attempts to assign a string 100 characters in length [...]]]></description>
			<content:encoded><![CDATA[<p>[SQL Server] String or Binary data would be truncated.</p>
<p>Run into this error before?  The cause is usually fairly clear, we’re attempting to insert or update a field in a table with more data than it can hold.  E.g. we’ve executed a SQL insert statement that attempts to assign a string 100 characters in length to a varchar field that can hold 80 characters max.  Microsoft SQL Server complains, and we receive a nice error in our .NET application.</p>
<p><strong>Solution 1: Don’t send too much data</strong></p>
<p>This is an easy solution (and not the purpose of the article) and not always possible, but it is completely valid and often times desirable if you are writing an application that works with a SQL database in a static manner where the fields you’re updating are known ahead of time.  There are a number of ways to ensure you don’t exceed the maximum size of a field.</p>
<p>If your data is coming from a form item, such as a text box, ensure the maximum size of the text box doesn’t exceed the maximum size of the field in the database.  If you’re working with items that cannot be limited, simply take a substring of the data and manually truncate it to a size allowed by the field in the database.  Many times we know what data and what sizes we’re working with at design time.</p>
<p><strong>Solution 2: Read field sizes from database and dynamically truncate data</strong></p>
<p>There are many times when solution 1 is not an option, mostly in situations where the database we’re working with is unknown, whether partially or fully, at design time.  I’ve worked on a number of ETL tools and interoperability oriented applications where I can’t know the field size in the database at design time, it must be discovered at runtime.  </p>
<p>One of the great parts about ADO.NET is the ease in which it deals with parameters.  At the core of executing SQL statements is the SqlCommand (or OleDbCommand, or odbcCommand) class.  After obtaining an instance associated with a database connection, we assign our SQL statement to it and all our parameters and values to it.  In our case, the size of the parameter values we assign to it may exceed the maximum size of our receiving database fields.  We need a method of iterating through each parameter and ensuring the value assigned isn’t too big.</p>
<p><strong>Reading table schema</strong></p>
<p>The heart of this routine is detecting which table you’re dealing with, then reading the schema of that table, then discovering the max size of each field.  </p>
<p>I start by declaring a sub procedure that takes a SqlCommand as argument by reference.</p>
<pre lang="vbnet">
        Sub TruncateParameters(ByRef passCommand As SqlCommand)
</pre>
<p>I then declared the following variables</p>
<pre lang="vbnet">
        Dim targetTable As String
        Dim LCV As Integer
        Dim tableCommand As SqlCommand
        Dim tableReader As SqlDataReader
        Dim tempRow As DataRow
</pre>
<p>targetTable is the name of the table we’ll be checking.  I read it from the actual SQL statement in the SqlCommand – but I was dealing with fairly simple SQL statements.  You may want to manually pass the table names in if you’re dealing with SQL statements that will be difficult to parse.</p>
<p>tableCommand, tableReader, and tempRow will be used to read in the schema of our targetTable.</p>
<p>Next I parse the SQL statement inside of passCommand to obtain the targetTable name, but again, you may want to manually pass the name of the table ahead of time if you’re dealing with difficult to parse SQL statements.  Also, if you are familiar with regular expressions, you can pare this routine down quite a bit – I present it here completely algorithmically for people who aren’t familiar with regular expressions.</p>
<pre lang="vbnet">
       If passCommand.CommandText.ToLower().IndexOf("update") > -1 Then
             'UPDATE command
             targetTable = passCommand.CommandText.Substring(passCommand.CommandText.ToLower().IndexOf("update") + 7)
       Else
             'INSERT command
             targetTable = passCommand.CommandText.Substring(passCommand.CommandText.ToLower().IndexOf("insert") + 12)
       End If

       targetTable = targetTable.Substring(0, targetTable.IndexOf(" "))
</pre>
<p>Next we read (up to) 1 row from our target table to obtain the schema.</p>
<pre lang="vbnet">
        ' Open Data Reader for schema table
        tableCommand = New SqlCommand("SELECT TOP 1 * FROM " &#038; targetTable, passCommand.Connection)
        tableReader = tableCommand.ExecuteReader()
</pre>
<p>This is where the magic happens – we iterate through each parameter in our passCommand, examine the field matching the parameter, get its max length, then truncate the value assigned to each respective parameter to the max size of the field.  This routine assumes that you’ve used the same parameter names as field names.  If you haven’t, you would need to do further parsing of the SQL statement to deduce a parameter name -> field name mapping.  This routine is also targeted for strings, but would work just as well for binary data assuming you had a method of truncating such data.  Also, there are a lot of shortcuts for doing case insensitive compares, my style is to convert to lower case and compare as I can never remember what is case sensitive and what is not.  Feel free to use your own style.</p>
<pre lang="vbnet">
        ' Loop through each parameter

        For LCV = 0 To passCommand.Parameters.Count - 1
            For Each tempRow In tableReader.GetSchemaTable().Rows
                If "@" &#038; tempRow("ColumnName").ToString().ToLower() = passCommand.Parameters.Item(LCV).ParameterName.ToLower() Then
                    If tempRow("DataType").ToString().ToLower() = "system.string" Then
                        If passCommand.Parameters.Item(LCV).Value.ToString().Length > tempRow("ColumnSize") Then
                            passCommand.Parameters.Item(LCV).Value = passCommand.Parameters.Item(LCV).Value.ToString().Substring(0, tempRow("ColumnSize"))
                        End If
                        Exit For
                    End If
                End If
            Next
        Next
</pre>
<p>Finally, we close out our connection and end the sub procedure.</p>
<pre lang="vbnet">
        tableReader.Close()

End Sub
</pre>
<p>There is some work involved, but the idea of the above routine can be customized to fit your situation.  Once you have a solid parameter truncating routine in your arsenal, you can call it for any SqlCommand object, and ensure you never get a “String or Binary data would be truncated.” error message again!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.toniwestbrook.com/archives/59/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

