<?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>AmmarkoV  `s  Personal Website &#187; RemoteVariables</title>
	<atom:link href="http://ammar.gr/?feed=rss2&#038;tag=remotevariables" rel="self" type="application/rss+xml" />
	<link>http://ammar.gr</link>
	<description>I would love to change the world , but they won`t give me the source code</description>
	<lastBuildDate>Thu, 25 Oct 2012 11:53:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>A difficult bug to trace..!</title>
		<link>http://ammar.gr/?p=421</link>
		<comments>http://ammar.gr/?p=421#comments</comments>
		<pubDate>Mon, 18 Jun 2012 20:19:16 +0000</pubDate>
		<dc:creator>Ammar Qammaz</dc:creator>
				<category><![CDATA[Post]]></category>
		<category><![CDATA[News]]></category>
		<category><![CDATA[RemoteVariables]]></category>

		<guid isPermaLink="false">http://ammar.gr/?p=421</guid>
		<description><![CDATA[During the last 2 weeks some major things happened in Greece including the second round of elections etc. I spent most of the time stuck and trying to trace a bug in RemoteVariables ( which I am currently developing ).. For testing the under construction library I wrote a test suite which performed 100 variable [...]]]></description>
			<content:encoded><![CDATA[<p>During the last 2 weeks some major things happened in Greece including the second round of elections etc.<br />
I spent most of the time stuck and trying to trace a bug in <a href="https://github.com/AmmarkoV/RemoteVariable">RemoteVariables</a> ( which I am currently developing )..</p>
<p>For testing the under construction library I wrote a test suite which performed 100 variable changes from peer to peer and finally exited when all of the handshakes where complete..<br />
The test suite was then debugged using valgrind and standard gdb and despite some early mixups and architecture problems that were very evident with the debugger output after they were gradually fixed some very strange behaviour ensued as the test suite would &#8220;randomly&#8221; stop at some point and the program &#8220;seemed&#8221; to stop acknowledging and transmitting changes on the variables without an apparent reason..</p>
<p>Adding to the complexity of identifying the problem was the fact that a total of 6 threads spanning in two procedures could potentially cause a deadlock somewhere ( despite the mutex protection ) .. </p>
<p>After 5 or 6 days of trying to fix the problem I started to get desperate going line by line and almost rewriting all the critical segments until I figured it out..</p>
<p>The problematic code was the following :<br />
<code><br />
unsigned long GetVariableHash(struct VariableShare * vsh,void * ptr,unsigned int size_of_ptr)<br />
{<br />
if (size_of_ptr<sizeof(unsigned long)) { //unsigned long so no hash is required<br />
                                         unsigned long * stacklongptr = ptr;<br />
                                         unsigned long stacklong = *stacklongptr;<br />
                                         return stacklong;<br />
                                       }else<br />
                                       {<br />
                                         return rvhash(ptr,size_of_ptr);<br />
                                       }<br />
return 0;<br />
}<br />
</code></p>
<p>Each pointer (void * ptr) points to a memory block of variable size (unsigned int size_of_ptr ) and the hash values get stored on an unsigned long ..<br />
All is good for variables that are longer than an unsigned long since they are casted to a char * "string" and then a hash function converts them to a reasonably unique  unsigned long.. But .. for smaller sizes of pointers such as unsigned ints my code directly casted the 4byte payload of the pointer to the 8byte accomodation of the unsigned long leaving 4 bytes uninitialized..</p>
<p>What was very funny was that in all the fprintf(stderr,"hash %u",(unsigned int) hash); calls I had added to review the function the results appeared completely normal but when two hashes where compared they failed the equal if operation even if they seemed to carry the same value when fprintf'ed </p>
<p>To better illustrate a simplified version , what I saw was an instance of</p>
<p><code>fprintf(stderr,"old hash %u , new hash %u\n",(unsigned int) oldhash,(unsigned int) newhash);<br />
if (oldhash==newhash) { fprintf(stderr,"They are the same\n"); } else<br />
                      { fprintf(stderr,"They are not the same\n"); } </p>
<p> </code></p>
<p>where I got output like :<br />
old hash 5 , new hash 5<br />
They are not the same</p>
<p>Needless to say valgrind ( which is a great tool and this wasnt its fault <img src='http://ammar.gr/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )  has an initialization check that did not report the unitialized bytes , the possible problem of unitialized bytes didnt even cross my mind while hastly writting the function @ 3 AM 2 years ago ( <a href="https://github.com/AmmarkoV/RemoteVariable/commit/05aafff60a9ba88f5c81d160fb5050d344740df8">the problematic commit</a> ) and 2 weeks of my life were "kind of" thrown away ..</p>
<p>It is true that you can sometimes shoot yourself in the foot when using tools that give you this kind of freedom , and this might be just such a case , but I frankly found it enlighting..<br />
C is an awesome language!     </p>
<p><a href="https://github.com/AmmarkoV/RemoteVariable/commit/01451c105350353943b2f675d5991f43cf47d06b">The fix commit</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://ammar.gr/?feed=rss2&#038;p=421</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RemoteVariables!</title>
		<link>http://ammar.gr/?p=409</link>
		<comments>http://ammar.gr/?p=409#comments</comments>
		<pubDate>Wed, 30 May 2012 10:36:46 +0000</pubDate>
		<dc:creator>Ammar Qammaz</dc:creator>
				<category><![CDATA[Post]]></category>
		<category><![CDATA[RemoteVariables]]></category>

		<guid isPermaLink="false">http://ammar.gr/?p=409</guid>
		<description><![CDATA[During the last weeks I finally found time to work on one of my older ideas ( 2 years old ) about a networking library that wouldn&#8217;t expose any network calls to the programmer..! From my experience writing small games and web server services network connectivity using sockets is very nice and efficient indeed , [...]]]></description>
			<content:encoded><![CDATA[<p>During the last weeks I finally found time to work on one of my older ideas ( 2 years old <img src='http://ammar.gr/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  ) about a networking library that wouldn&#8217;t expose any network calls to the programmer..!</p>
<p>From my experience writing small games and web server services network connectivity using sockets is very nice and efficient indeed , but it has a lot of programming overhead to basically do simple things like passing values or strings from one host to the other..</p>
<p>With RemoteVariables what I am doing is writing a framework that takes care of all the protocol issues and then assumes the role of synchronizing specific elements of the address space of the first computer to the second one..!   </p>
<p>So instead of recv/send ing , worrying about half read messages or making a new mini wrapper protocol for every new application the idea goes like this ..</p>
<p>/* CLIENT CODE */<br />
struct VariableShare * vsh = ConnectToRemote_VariableSharing(&#8220;SHARE2&#8243;,&#8221;127.0.0.1&#8243;,12345,&#8221;password&#8221;);<br />
static volatile int SHARED_VAR=0; Add_VariableToSharingList(vsh,&#8221;SHARED_VAR&#8221;,7,&#038;SHARED_VAR,sizeof(SHARED_VAR));</p>
<p>/* SERVER CODE*/<br />
struct VariableShare * vsh = Start_VariableSharing(&#8220;SHARE2&#8243;,&#8221;127.0.0.1&#8243;,12345,&#8221;password&#8221;);<br />
static volatile int SHARED_VAR=0;<br />
Add_VariableToSharingList(vsh,&#8221;SHARED_VAR&#8221;,7,&#038;SHARED_VAR,sizeof(SHARED_VAR));</p>
<p>from there on variable SHARED_VAR is &#8220;shared&#8221; meaning that when it changes on one host we know that the second host will get it.<br />
The change is &#8220;sensed&#8221; by a second thread monitoring the values of the address space and transmiting them when they change.. It could be the case that the compiler could spit in a Sync(&#038;SHARED_VAR) operation after each change for maximum efficiency .. Some one can also disable automatic &#8220;sensing&#8221; and perform the Sync call manually to avoid  the overheads of the extra thread on the background..</p>
<p>Another example is for lets say a Tic Tac Toe game where we will have<br />
/* CLIENT CODE */<br />
struct VariableShare * vsh = ConnectToRemote_VariableSharing(&#8220;TICSHARE&#8221;,&#8221;127.0.0.1&#8243;,12345,&#8221;password&#8221;);<br />
static volatile int OURMOVE=0,OPPONENTMOVE=0;<br />
Add_VariableToSharingList(vsh,&#8221;HOST_MOVE&#8221;,7,&#038;OPPONENTMOVE,sizeof(OPPONENTMOVE));<br />
Add_VariableToSharingList(vsh,&#8221;SHARED_VAR&#8221;,7,&#038;OURMOVE,sizeof(OURMOVE));</p>
<p>/* SERVER CODE */<br />
struct VariableShare * vsh = Start_VariableSharing(&#8220;TICSHARE&#8221;,&#8221;127.0.0.1&#8243;,12345,&#8221;password&#8221;);<br />
static volatile int OURMOVE=0,OPPONENTMOVE=0;<br />
Add_VariableToSharingList(vsh,&#8221;SHARED_VAR&#8221;,7,&#038;OPPONENTMOVE,sizeof(OPPONENTMOVE));<br />
Add_VariableToSharingList(vsh,&#8221;HOST_MOVE&#8221;,7,&#038;OURMOVE,sizeof(OURMOVE));</p>
<p>Notice the twisted assignment of the SHARED_VAR , HOST_MOVE ..<br />
This way we could litterally write a network tictac toe program by just checking the OURMOVE , OPPONENTMOVE variables just like writing a two player hot seat game , no more extra code , maybe just for checking if the Client has dropped something that doesnt really happen on hot seat games  ..</p>
<p>Needless to say <a href="http://ammar.gr/myloader/vfile.php?i=5056effeeaff0d339d010bff174c3ccd-not_over_yet.ogg">its not over yet</a> but I am making good progress..</p>
<p>The <a href="https://github.com/AmmarkoV/RemoteVariable">Repository is here ( https://github.com/AmmarkoV/RemoteVariable )</a>..</p>
<p>To test it issue :<br />
git clone git://github.com/AmmarkoV/RemoteVariable.git<br />
./apt-get-dependencies.sh<br />
./make<br />
and you can then test a Score4 Game by issuing<br />
./TestGameMasterAndClone.sh</p>
<p>or test the standalone library via a unit test by issuing<br />
./TestMasterAndClone.sh<br />
of<br />
./MemTestMasterAndClone.sh<br />
for valgrind analysis too..</p>
]]></content:encoded>
			<wfw:commentRss>http://ammar.gr/?feed=rss2&#038;p=409</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
