<?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>Florent Clairambault &#187; M2M</title>
	<atom:link href="http://florent.clairambault.fr/tag/m2m/feed" rel="self" type="application/rss+xml" />
	<link>http://florent.clairambault.fr</link>
	<description></description>
	<lastBuildDate>Wed, 28 Jul 2010 20:31:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>The M2MP protocol</title>
		<link>http://florent.clairambault.fr/the-m2mp-protocol?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=the-m2mp-protocol</link>
		<comments>http://florent.clairambault.fr/the-m2mp-protocol#comments</comments>
		<pubDate>Sat, 12 Dec 2009 20:00:10 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[M2M]]></category>
		<category><![CDATA[M2MP]]></category>
		<category><![CDATA[Protocol]]></category>
		<category><![CDATA[TC65]]></category>
		<category><![CDATA[TCP]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2165</guid>
		<description><![CDATA[Today, I&#8217;m going to talk about a little protocol that I&#8217;ve implemented in few softwares (on TC65, .Net client and .Net server). It won&#8217;t be very interesting for most of you (99.99%). But still, I wanted to write it because it has been very useful and it might prevent you from writing the next stupid [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I&#8217;m going to talk about a little protocol that I&#8217;ve implemented in few softwares (on TC65, .Net client and .Net server). It won&#8217;t be very interesting for most of you (99.99%). But still, I wanted to write it because it has been very useful and it might prevent you from writing the next <a href="#stupid">stupid M2M protocol specifications</a>.</p>
<h2>Motivations</h2>
<p>I made this simple protocol to solve very simple and common problems on M2M equipments, we want to :</p>
<ul>
<li>Transmit and receive in real-time</li>
<li>Transmit as few data as possible</li>
<li>Send and receive any kind of data</li>
</ul>
<p>So the basic ideas are to :</p>
<ul>
<li>Keep a TCP connection open all the time by sending regular keep-alive frames.</li>
<li>Define named channels that are only transmitted once</li>
<li>Send byte array, and two-dimensionnal byte arrays on these channels</li>
</ul>
<p>We rely on the TCP protocol for packet checksuming, packet ordering and every little things it does very well.</p>
<p>Basically, we send these kind of frames :</p>
<ul>
<li>Identification frames</li>
<li>Channel name to id definition</li>
<li>Data transmission of single byte arrays (Byte[]).</li>
<li>Data transmission of two-dimentionnal byte arrays (Byte[][]).</li>
</ul>
<h2>The protocol</h2>
<p><b>Identification</b><br />
The client ask for authentication :</p>

<div class="wp_codebox"><table><tr id="p216519"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2165code19"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Identification header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x05 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Size of the identification</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">'h'</span>, <span style="color: #0000ff;">'e'</span>, <span style="color: #0000ff;">'l'</span>, <span style="color: #0000ff;">'l'</span>, <span style="color: #0000ff;">'o'</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Identifier</span></pre></td></tr></table></div>

<p>The server replies &#8220;ok&#8221; or &#8220;not ok&#8221;</p>

<div class="wp_codebox"><table><tr id="p216520"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2165code20"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Identification request</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// OK, 0x00 for not ok</span></pre></td></tr></table></div>

<p><strong>Ping / Keep alives</strong><br />
Client sends a ping request :</p>

<div class="wp_codebox"><table><tr id="p216521"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2165code21"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x02 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// client ping request header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x15 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// the number (here 0x15) can be incremented or random</span></pre></td></tr></table></div>

<p>Server answers the ping request :</p>

<div class="wp_codebox"><table><tr id="p216522"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2165code22"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x02 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// client ping server response header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x15 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 0x15 is the number of the ping request</span></pre></td></tr></table></div>

<p>Server sends a ping request :</p>

<div class="wp_codebox"><table><tr id="p216523"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2165code23"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// server ping request header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x16 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// the number (here 0x16) can be incremented or random</span></pre></td></tr></table></div>

<p>Client answers the ping request :</p>

<div class="wp_codebox"><table><tr id="p216524"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p2165code24"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// server ping client response header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x16 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 0x16 is the 0x16 number of the ping request</span></pre></td></tr></table></div>

<p><strong>Defining a named channel</strong><br />
<em>This applies to both client to server and server to client communcation.</em></p>

<div class="wp_codebox"><table><tr id="p216525"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2165code25"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x20 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// channel definition frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x0D <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 13 is the size of the following message</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">12</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">'c'</span>, <span style="color: #0000ff;">'h'</span>, <span style="color: #0000ff;">'a'</span>, <span style="color: #0000ff;">'n'</span>, <span style="color: #0000ff;">'n'</span>, <span style="color: #0000ff;">'e'</span>, <span style="color: #0000ff;">'l'</span>, <span style="color: #0000ff;">' '</span>, <span style="color: #0000ff;">'n'</span>, <span style="color: #0000ff;">'a'</span>, <span style="color: #0000ff;">'m'</span>, <span style="color: #0000ff;">'e'</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// the name of the channel : &quot;channel name&quot;</span></pre></td></tr></table></div>

<p><strong>Sending some data</strong><br />
<em>This applies to both client to server and server to client communcation.</em></p>
<p>For 0 to 254 sized messages :</p>

<div class="wp_codebox"><table><tr id="p216526"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2165code26"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x21 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// one byte sized data transmission frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x06 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the data</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x02, 0x03, 0x04, 0x05 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// data transmitted on the channel</span></pre></td></tr></table></div>

<p>For 255 to 65534 sized data messages :</p>

<div class="wp_codebox"><table><tr id="p216527"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2165code27"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x41 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// two bytes sized data transmission frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x06 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the data</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x02, 0x03, 0x04, 0x05 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// data transmitted on the channel</span></pre></td></tr></table></div>

<p>For 65 535 octets to 4 294 967 294 sized data messages :</p>

<div class="wp_codebox"><table><tr id="p216528"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2165code28"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x61 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// two bytes sized data transmission frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x00, 0x00, 0x06 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the data</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x02, 0x03, 0x04, 0x05 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// data transmitted on the channel</span></pre></td></tr></table></div>

<p>For data arrray transmission, it&#8217;s the same except frame header are 0&#215;22, 0&#215;42, 0&#215;62 instead of 0&#215;21, 0&#215;41, 0&#215;61. If you transmit data array like this one Byte[][] data = { { 0&#215;01, 0&#215;02 }, { 0&#215;03, 0&#215;04, 0&#215;05 }, { 0&#215;06, 0&#215;07, 0&#215;08, 0&#215;09 } } :</p>

<div class="wp_codebox"><table><tr id="p216529"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2165code29"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x22 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// one byte sized data transmission frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x0D <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the data</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x02 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the first array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x02 <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the second array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03, 0x04, 0x05 <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x04 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the third array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x06, 0x07, 0x08, 0x09 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>For bigger arrays, you need to define :</p>

<div class="wp_codebox"><table><tr id="p216530"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2165code30"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x42 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// one byte sized data transmission frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x10 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the data (16)</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x02 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the first array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x02 <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the second array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03, 0x04, 0x05 <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x04 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the third array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x06, 0x07, 0x08, 0x09 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>and so on :</p>

<div class="wp_codebox"><table><tr id="p216531"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p2165code31"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x62 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// one byte sized data transmission frame header</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x16 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the data (22)</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// where 3 is the id of the channel</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x00, 0x00, 0x02 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the first array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x02 <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x00, 0x00, 0x03 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the second array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x03, 0x04, 0x05 <span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x00, 0x00, 0x00, 0x04 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// size of the third array</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x06, 0x07, 0x08, 0x09 <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Sample data types transmitted</h2>
<p><strong>Strings</strong><br />
Easy&#8230; Just the bytes value of the string</p>
<p><strong>Position</strong><br />
when moving :</p>

<div class="wp_codebox"><table><tr id="p216532"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p2165code32"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Atimestamp+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Timestamp</span></a> <span style="color: #339933;">:</span> UInt32 <span style="color: #009900;">&#40;</span>since <span style="color: #cc66cc;">1970</span> or <span style="color: #cc66cc;">2009</span> <span style="color: #000000; font-weight: bold;">if</span> you wish<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> Longitude <span style="color: #339933;">:</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afloat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Float</span></a>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> Latitude <span style="color: #339933;">:</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afloat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Float</span></a>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> Speed <span style="color: #339933;">:</span> UInt16
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> Altitude <span style="color: #339933;">:</span> UInt16</pre></td></tr></table></div>

<p>= 16 bytes</p>
<p>when stopped :</p>

<div class="wp_codebox"><table><tr id="p216533"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p2165code33"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Atimestamp+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Timestamp</span></a> <span style="color: #339933;">:</span> UInt32 <span style="color: #009900;">&#40;</span>since <span style="color: #cc66cc;">1970</span> or <span style="color: #cc66cc;">2009</span> <span style="color: #000000; font-weight: bold;">if</span> you wish<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> Longitude <span style="color: #339933;">:</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afloat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Float</span></a>
<span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">4</span> <span style="color: #009900;">&#93;</span> Latitude <span style="color: #339933;">:</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Afloat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Float</span></a></pre></td></tr></table></div>

<p>= 12 bytes</p>
<p><strong>Battery</strong><br />
Percentage :</p>

<div class="wp_codebox"><table><tr id="p216534"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2165code34"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#93;</span> Percentage <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abyte+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Byte</span></a><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Voltage :</p>

<div class="wp_codebox"><table><tr id="p216535"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p2165code35"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#93;</span> Voltage in mV <span style="color: #009900;">&#40;</span>UInt16<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<h2>Sample communication</h2>
<p>This is just to show how is working a sample communication</p>

<div class="wp_codebox"><table><tr id="p216536"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p2165code36"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">--&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x04, 0x01, 0x02, 0x03, 0x04 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// identification frame</span>
<span style="color: #339933;">&lt;--</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x01, 0x01 <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// identification accepted</span>
<span style="color: #339933;">--&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x20, 0x08, 0x00, <span style="color: #0000ff;">'b'</span>, <span style="color: #0000ff;">'a'</span>, <span style="color: #0000ff;">'t'</span>, <span style="color: #0000ff;">'t'</span>, <span style="color: #0000ff;">'e'</span>, <span style="color: #0000ff;">'r'</span>, <span style="color: #0000ff;">'y'</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// We define the channel &quot;battery&quot; with id 0x00</span>
<span style="color: #339933;">--&gt;</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span> 0x21, 0x02, 0x00, <span style="color: #cc66cc;">70</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// We send 70 (meaning 70%) on the &quot;battery&quot; channel.</span></pre></td></tr></table></div>

<h2>On top of that</h2>
<p>On top of that, I added some settings management and capacities specifications.</p>
<p><strong>Capacities</strong><br />
If the server send &#8220;?&#8221; on the &#8220;_cap&#8221; (like capacities) channel, the client replies on the same channel with an data array containing the capacities it has (like &#8220;positionTracking&#8221;, &#8220;smsSending&#8221;, &#8220;sms&#8221; ).</p>
<p><strong>Settings management</strong><br />
The server can get and set some settings on the &#8220;_set&#8221; channel.<br />
If it&#8217;s a two-dimentionnal array and the first element is &#8220;s&#8221; (like &#8220;set&#8221;) it means that it sets some settings, the next elements will be &#8220;setting1=value1&#8243;, &#8220;setting2=value2&#8243;, etc.<br />
If it&#8217;s a two-dimentionnal array and the first element is &#8220;g&#8221; (like &#8220;get&#8221;) it means that it needs the client to report the value of some settings, like &#8220;setting1&#8243;, &#8220;setting2&#8243;<br />
If it&#8217;s a single dimentionnal array that contains &#8220;ga&#8221; (like &#8220;get all&#8221;) it means that the server wants the client to report the value of every settings.</p>
<p><strong>Gateway acting and sub equipements</strong><br />
Any equipment can act as a gateway by encapsulating data of an sub-equipment in a channel name like this &#8220;_eq/[sub equipment identifier]&#8221; but this has never been used for the moment.</p>
<h2>Stupid protocols</h2>
<p>I&#8217;ve seen a lot of protocols from big companies doing some really stupid things like <a name="stupid">:</a></p>
<ul>
<li>Fixed size frames for not fixed size data.</li>
<li>4 zero filled bytes preamble. Why the hell would we need preamble in a TCP connection ?</li>
<li>8 bytes timestamp in millisecond when data has a 1 second precision.</li>
<li>4 bytes integer for specifying some number that never exceed 32.</li>
<li>Checksums on top of the TCP checksumming mechanism.</li>
<li>Redundant data at the beginning and the end.</li>
<li>Disconnecting very frequently (TCP establishment + identification costs a lot).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/the-m2mp-protocol/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TC65 : Settings management</title>
		<link>http://florent.clairambault.fr/tc65-settings-management?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=tc65-settings-management</link>
		<comments>http://florent.clairambault.fr/tc65-settings-management#comments</comments>
		<pubDate>Fri, 11 Dec 2009 20:00:03 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[M2M]]></category>
		<category><![CDATA[M2MP]]></category>
		<category><![CDATA[Protocol]]></category>
		<category><![CDATA[TC65]]></category>
		<category><![CDATA[TCP]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2146</guid>
		<description><![CDATA[Someone recently asked me and the javacint group how we should handle settings. So I&#8217;ll give you two answers : ejw&#8217;s reply : You should use some simple DataInputStream and DataOutputStream objects. Mine : If you only store simple values (numbers and text), you should use a simple text file. It enables you to easily [...]]]></description>
			<content:encoded><![CDATA[<p>Someone recently asked me and the javacint group <a href="http://groups.google.com/group/javacint/browse_thread/thread/4cfb9c3754334291">how we should handle settings</a>. So I&#8217;ll give you two answers :</p>
<ul>
<li><a href="http://groups.google.com/group/javacint/msg/f226ea32c02d6d68">ejw&#8217;s reply</a> : You should use some simple DataInputStream and DataOutputStream objects.</li>
<li>Mine : If you only store simple values (numbers and text), you should use a simple text file. It enables you to easily see and modify settings outside the TC65.</li>
</ul>
<p>So, my little gift of today will be a simple settings management class. The idea is that this file is in a very simple format (that can be read in any PC) and it only stores settings that have changed. This is very important, it allows you to change the default behavior at next software update but also enable you to override some of the settings for each chip (like the last IMSI identifier of the SIM card).</p>

<div class="wp_codebox"><table><tr id="p214638"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
</pre></td><td class="code" id="p2146code38"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">Common</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.microedition.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.siemens.icm.io.file.*</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Settings management class
 * @author Florent Clairambault
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Settings <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// The cached settings</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a> _settings<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// File of the settings</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> _fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;a:/settings.txt&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Singleton instance of the settings</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Settings _instance<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Get Default instance of the Settings class
	 * @return The default instance of the Settings class
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">synchronized</span> Settings getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> _instance <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
			_instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Settings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> _instance<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Free the singleton instance
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> freeInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		_instance <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Load settings
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> Load<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringbuffer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">StringBuffer</span></a> buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringbuffer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">StringBuffer</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a> settings <span style="color: #339933;">=</span> getDefaultSettings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
			FileConnection fc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FileConnection<span style="color: #009900;">&#41;</span> Connector.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;file:///&quot;</span> <span style="color: #339933;">+</span> _fileName, Connector.<span style="color: #006633;">READ</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> is <span style="color: #339933;">=</span> fc.<span style="color: #006633;">openInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> is.<span style="color: #006633;">available</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">int</span> c <span style="color: #339933;">=</span> is.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> c <span style="color: #339933;">==</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					Load_TreatLine<span style="color: #009900;">&#40;</span> settings, buffer.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					buffer <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astringbuffer+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">StringBuffer</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span>
					buffer.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#41;</span> c <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			is.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fc.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> ex <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// The exception we shoud have is at first launch : </span>
			<span style="color: #666666; font-style: italic;">// There shouldn't be any file to read from</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Logger.<span style="color: #006633;">E_CRITICAL</span> <span style="color: #009900;">&#41;</span>
				Logger.<span style="color: #006633;">Log</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">19</span>, <span style="color: #0000ff;">&quot;Settings.Load&quot;</span>, ex <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		_settings <span style="color: #339933;">=</span> settings<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Treat each line of the file
	 * @param def Default settings
	 * @param line Line to parse
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> Load_TreatLine<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a> settings, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> line <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Logger.<span style="color: #006633;">E_VERBOSE</span> <span style="color: #009900;">&#41;</span>
			Logger.<span style="color: #006633;">Log</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">78</span>, <span style="color: #0000ff;">&quot;Load_TreatLine( [...], <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> line <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> );&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> spl <span style="color: #339933;">=</span> Common.<span style="color: #006633;">strSplit</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'='</span>, line <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> key <span style="color: #339933;">=</span> spl<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value <span style="color: #339933;">=</span> spl<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// If default settings hashTable contains this key</span>
		<span style="color: #666666; font-style: italic;">// we can use this value</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> settings.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			settings.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			settings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> key, value <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Get default settings
	 * @return Default settings Hashtable
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a> getDefaultSettings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a> defaultSettings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// General M2MSoft settings :</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;code&quot;</span>, <span style="color: #0000ff;">&quot;8888&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;servers&quot;</span>, <span style="color: #0000ff;">&quot;87.106.206.30:3000&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;apn&quot;</span>, <span style="color: #0000ff;">&quot;gprs,m2minternet,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,,0&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;imsi&quot;</span>, <span style="color: #0000ff;">&quot;0000&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;watchdogtimer&quot;</span>, <span style="color: #0000ff;">&quot;20&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;version&quot;</span>, <span style="color: #0000ff;">&quot;0&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		defaultSettings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;phoneManager&quot;</span>, <span style="color: #0000ff;">&quot;+33686955405&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> defaultSettings<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Reset everything
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> ResetErything<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			FileConnection fc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FileConnection<span style="color: #009900;">&#41;</span> Connector.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;file:///&quot;</span> <span style="color: #339933;">+</span> _fileName, Connector.<span style="color: #006633;">READ_WRITE</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> fc.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
				fc.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        _settings <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Logger.<span style="color: #006633;">E_CRITICAL</span> <span style="color: #009900;">&#41;</span>
				Logger.<span style="color: #006633;">Log</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">16725</span>, <span style="color: #0000ff;">&quot;Settings.ResetEverything&quot;</span>, ex <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** 
	 * Save setttings
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> Save<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// If there's no settings, we shouldn't have to save anything</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> _settings <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ahashtable+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Hashtable</span></a> defSettings <span style="color: #339933;">=</span> getDefaultSettings<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aenumeration+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Enumeration</span></a> e <span style="color: #339933;">=</span> defSettings.<span style="color: #006633;">keys</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			FileConnection fc <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FileConnection<span style="color: #009900;">&#41;</span> Connector.<span style="color: #006633;">open</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;file:///&quot;</span> <span style="color: #339933;">+</span> _fileName, Connector.<span style="color: #006633;">READ_WRITE</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> fc.<span style="color: #006633;">exists</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
				fc.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//fc = (FileConnection) Connector.open(&quot;file:///&quot; + _fileName, Connector.READ_WRITE);</span>
			fc.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aoutputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">OutputStream</span></a> os <span style="color: #339933;">=</span> fc.<span style="color: #006633;">openOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span> e.<span style="color: #006633;">hasMoreElements</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> key <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> e.<span style="color: #006633;">nextElement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> _settings.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> defValue <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> defSettings.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #666666; font-style: italic;">// if there is a default value</span>
					defValue <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #666666; font-style: italic;">// and</span>
					<span style="color: #666666; font-style: italic;">// the value isn't the same as the default value</span>
					defValue.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span> value <span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> line <span style="color: #339933;">=</span> key <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> value <span style="color: #339933;">+</span> <span style="color: #0000ff;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #339933;">;</span>
&nbsp;
					<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Logger.<span style="color: #006633;">E_DEBUG</span> <span style="color: #009900;">&#41;</span>
						Logger.<span style="color: #006633;">Log</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">131</span>, <span style="color: #0000ff;">&quot;Settings.Save.line = <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">+</span> line <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
					os.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span> line.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #009900;">&#125;</span>
			os.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			os.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			fc.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> Logger.<span style="color: #006633;">E_CRITICAL</span> <span style="color: #009900;">&#41;</span>
				Logger.<span style="color: #006633;">Log</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">131</span>, <span style="color: #0000ff;">&quot;Settings.Save&quot;</span>, ex <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Init (and ReInit) method
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> CheckLoad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> _settings <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
			Load<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Get a setting's value as a String
	 * @param key Key Name of the setting
	 * @return String value of the setting
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> GetSetting<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> key <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		CheckLoad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> _settings.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#41;</span> _settings.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Sets a setting
	 * @param key Setting to set
	 * @param value Value of the setting
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">void</span> SetSetting<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> key, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		CheckLoad<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> _settings.<span style="color: #006633;">containsKey</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			_settings.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		_settings.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span> key, value <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		OnSettingChanged<span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Get a setting's value as an int
	 * @param key Key Name of setting
	 * @return Integer value of the setting
	 * @throws java.lang.NumberFormatException When the int cannot be parsed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> GetSettingInt<span style="color: #009900;">&#40;</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> key <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Anumberformatexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">NumberFormatException</span></a> <span style="color: #009900;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> value <span style="color: #339933;">=</span> GetSetting<span style="color: #009900;">&#40;</span> key <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> value <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span> value <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Latter on, I added some settings consumer capacities. It allows to use this class in some sort of &#8220;third party&#8221; developed components. Each component had to register itself to the settings management class, the settings management class could then request the default value settings it will provide, and it could set/get settings. The settings event class also launched an event when a setting was changed.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/tc65-settings-management/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The waspmote</title>
		<link>http://florent.clairambault.fr/the-waspmote?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=the-waspmote</link>
		<comments>http://florent.clairambault.fr/the-waspmote#comments</comments>
		<pubDate>Wed, 25 Nov 2009 08:32:59 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[Embedded]]></category>
		<category><![CDATA[M2M]]></category>
		<category><![CDATA[WaspMote]]></category>
		<category><![CDATA[ZigBee]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=2052</guid>
		<description><![CDATA[For those of you that take an interest in M2M and/or embedded things. You should look at the Waspmote. This is a generic hardware that can be customized to fit any need. Communication : GPRS / Wifi / ZigBee / USB Location : GPS Power usage : Low consuption, battery or solar as power input [...]]]></description>
			<content:encoded><![CDATA[<p>For those of you that take an interest in <a href="http://florent.clairambault.fr/tag/m2m">M2M</a> and/or embedded things. You should look at <a href="http://www.libelium.com/products/waspmote">the Waspmote</a>.</p>
<div style="positon: relative; float: right">
<img src="http://i.mivi.fr/76b96e6c71cd1b10c284a6080f4eee07e5ce71a3.x.200.jpg" />
</div>
<p>This is a generic hardware that can be customized to fit any need. </p>
<ul>
<li>Communication : GPRS / Wifi / ZigBee / USB</li>
<li>Location : GPS</li>
<li>Power usage : Low consuption, battery or solar as power input</li>
<li>Storage : SD Card (2GB), no other storage memory</li>
<li>Sensors : Everything you might need (really impressive set of sensors)</li>
</ul>
<p>I looked at <a href="http://www.libelium.com/development/waspmote">the API</a> and it&#8217;s in C (pretty low level, you have to free everything) but everything seems to be handled at a high level. It&#8217;s sometimes better to have a smart C API rather than a dumb Java API. Look at the examples, they are really easy to read, you don&#8217;t need to be a skilled C developer to understand everything.</p>
<p>If the API seems great, the IDE doesn&#8217;t. But still I have just downloaded and tested it very quickly. It made me feel I was back on <a href="http://www.solarnavigator.net/sponsorship/sponsorship_images/Amstrad_PC_micro_computer_64x.jpg">an AMSTRAD</a> except that I didn&#8217;t have to put line numbers.</p>
<p>It seems the WaspMote is sold $100 for each card when bought in a package of 5. </p>
<p>Note : I know this look like some stupid buzz syndrome or planned viral advertisement but it&#8217;s not. It&#8217;s just a product that I&#8217;ve been interested in for years. This is the perfect product if you&#8217;re an M2M company. What ever you or your client wants, you can do it with this product.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/the-waspmote/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.Net, Flex and WebORB.Net</title>
		<link>http://florent.clairambault.fr/net-flex-and-weborb-net?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=net-flex-and-weborb-net</link>
		<comments>http://florent.clairambault.fr/net-flex-and-weborb-net#comments</comments>
		<pubDate>Sat, 24 Oct 2009 07:47:41 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[IIS]]></category>
		<category><![CDATA[M2M]]></category>
		<category><![CDATA[TC65]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WebORB]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1825</guid>
		<description><![CDATA[I&#8217;ve been working on a project where we had to use Flex on a .Net environment and it had to be realtime. I was a little worried at first that it could be tricky to set up. But with WebORB.Net it&#8217;s pretty easy. We used the integrated RTMP (Real Time Messaging Protocol) messaging server. It&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a project where we had to use Flex on a .Net environment and it had to be realtime. I was a little worried at first that it could be tricky to set up. But with <a href="http://www.themidnightcoders.com/products/weborb-for-net/overview.html">WebORB.Net</a> it&#8217;s pretty easy. We used the integrated RTMP (Real Time Messaging Protocol) messaging server. It&#8217;s almost like using WCF. The most important differences are that, by default, objects are transmitted as Hashtable and calls can&#8217;t be synchronous. We can bind the object to right .Net object within the WebOrb management console but we decided to do it ourself using reflection (because we don&#8217;t like to depend too much on the management console).</p>
<p><strong>RTMP</strong><br />
The result is pretty impressive as it makes a really powerful real-time management system. And it&#8217;s freaking fast. Like WCF, it can handle concurrent calls. It&#8217;s in fact easier because everything is asynchronous but still, it does manage that. These calls are received on parallel threads.</p>
<p>The only problem with WebORB.Net is that you don&#8217;t have too much examples in the documentation. Our biggest problem was that we didn&#8217;t even knew how to detect what was the current calling connection on a method. This is pretty easy but you can&#8217;t exactly guess it. This is a dummy sample to show you how it&#8217;s done :</p>

<div class="wp_codebox"><table><tr id="p182540"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code" id="p1825code40"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FlexConnectionsServer<span style="color: #008000;">:</span>ApplicationAdapter <span style="color: #008000;">&#123;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> _connectionIdCounter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> Dictionary<span style="color: #008000;">&lt;</span>IConnection,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> _connectionsToId <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Dictionary<span style="color: #008000;">&lt;</span>IConnection,<span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Returns the CURRENT connection (must be used from a method called by the flex client)</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> IConnection CurrentConnection <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> ConnectionHub<span style="color: #008000;">.</span><span style="color: #0000FF;">getConnectionLocal</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> CurrentID <span style="color: #008000;">&#123;</span> get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> _connectionsToId<span style="color: #008000;">&#91;</span> CurrentConnection <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// When the RTMP service is loaded, we start the time dispatcher thread</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> appStart<span style="color: #008000;">&#40;</span> IScope app <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #008000;">&#40;</span> Thread_TimeDispatcher <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This method is called when a client connects</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">bool</span> appConnect<span style="color: #008000;">&#40;</span> IConnection conn, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> parms <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span> _connectionsToId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_connectionsToId<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> conn, _connectionIdCounter<span style="color: #008000;">++</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This method is called when a client disconnects</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">void</span> appDisconnect<span style="color: #008000;">&#40;</span> IConnection conn <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span> _connectionsToId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_connectionsToId<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span> conn <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This is a sample method call</span>
	<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">String</span> GetMyName<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Client{0}&quot;</span>, CurrentId <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// This thread dispatches the current time to every client</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> Thread_TimeDispatcher<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">while</span><span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			var t <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">UtcNow</span><span style="color: #008000;">;</span>
			SendCommand<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;CurrentTime&quot;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> t <span style="color: #008000;">&#125;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">1000</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Call a remote method on each client connection</span>
	<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> SendCommand<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">String</span> command, <span style="color: #6666cc; font-weight: bold;">Object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">lock</span> <span style="color: #008000;">&#40;</span> _connectionsToId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span> var kvp <span style="color: #0600FF; font-weight: bold;">in</span> _connectionsToId <span style="color: #008000;">&#41;</span>
				<span style="color: #008000;">&#40;</span> kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span> <span style="color: #0600FF; font-weight: bold;">as</span> IServiceCapableConnection <span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span> command, <span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s a little bit like using WCF in Single instance mode.</p>
<p><strong>IIS 7.0 Setup</strong><br />
There was something really weird that happened with my IIS 7.0 on my Windows 7 x64. Using the RTMP connection, I frequently had null return values on .Net method calls. We firstly find a quick way to solve it : Recall each time we get a null value. It&#8217;s really dirty, but It worked as a very temporary solution.<br />
Then, when debugging on the IIS process, I noticed a lot of NullReferenceException exceptions displayed on the debugger console. They were about the WebORB &#8220;LicenseManager&#8221; class, and I had as much exceptions as I had null values on the Flex client. I tried a lot of things that didn&#8217;t do anything. And then, I changed the &#8220;Managemd pipeline mode&#8221; of the Application Pool from &#8220;Integrated&#8221; to &#8220;Classic&#8221;, and the problem was solved. The really weird stuff here is that <a href="http://www.themidnightcoders.com/products/weborb-for-net/developer-den/technical-articles/installing-weborb-on-vista-iis-7-install.html">WebORB recommends to use the &#8220;Integrated&#8221; mode</a>.</p>
<p><strong>MidnightCoders support</strong><br />
We asked MidnightCoders to send us a community edition license, and they instantly answered. We got our license within the 48 hours after our first mail (I was the one slowing it down).</p>
<p><strong>Quick note on the project</strong><br />
To give you a better view of the project I&#8217;m talking about :<br />
GPRS Equipment <--(<a href="http://florent.clairambault.fr/the-m2mp-protocol">M2MP protocol</a>)&#8211;> [Generic M2M Server] <--(WCF)--> [Business logic software] <--(WCF)--> [Flex connections server] <--(RTMP)--> Flex<br />
We&#8217;ve got a little equipment (<a href="http://florent.clairambault.fr/tag/tc65">TC65</a> based as you can guess) that connects to a generic server using our own (real-time and bandwidth optimized) protocol. A business logic software connects to this server using a WCF service. And then we have the WebORB.Net / IIS that connects to this business logic software. </p>
<p>WCF helps make very interesting .Net applications because it simplifies all the communication but can also create deadlocks you might not have seen. Take this example : You have a lock on the WCF Server on the clients list object, you make a call to the WCF client and this client makes a call to the WCF Server that also requires a lock. If you were doing all this client/server calls on the same process this wouldn&#8217;t be a problem. But as you use WCF client-to-server and server-to-client calls are made on differents threads. You create a deadlock (unless you set the OperationContract attribute to IsOneWay and/or you fix the deadlock source).</p>
<p>If you&#8217;re doing real-time applications, I would recommend to define all the frequently used methods with the &#8220;IsOneWay=true&#8221; option. </p>
<p><em>06/11/2009 Update : I added a sample server-to-client code because some people were interested by it.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/net-flex-and-weborb-net/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>NetEventServer</title>
		<link>http://florent.clairambault.fr/neteventserver?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=neteventserver</link>
		<comments>http://florent.clairambault.fr/neteventserver#comments</comments>
		<pubDate>Wed, 17 Jun 2009 18:00:32 +0000</pubDate>
		<dc:creator>Florent Clairambault</dc:creator>
				<category><![CDATA[English]]></category>
		<category><![CDATA[C# .Net]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[M2M]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://florent.clairambault.fr/?p=1101</guid>
		<description><![CDATA[I talked some time ago about a library I made to take advantage of the kernel network events. I now release it and explain how to use it. It can help people to do some little network softwares without knowing where to start from. I built it for network servers made to communicate with remotely [...]]]></description>
			<content:encoded><![CDATA[<p>I talked <a href="http://florent.clairambault.fr/kernel-event-to-improve-network-performance-on-net">some time ago</a> about a library I made to take advantage of the kernel network events. I now release it and explain how to use it. It can help people to do some little network softwares without knowing where to start from.</p>
<p>I built it for network servers made to communicate with remotely connected embedded chips. I wanted to be able to always stay in touch with a huge number of chips without any real cost. So, my very personal goal was to built a server network layer for massive M2M applications.<br />
I also made a little web server with it supporting Keep-Alive and partial file download (wit the &#8220;Range&#8221; header) and an other little library to send serialized objects.</p>
<p>I made this little network library to accomplish two main goals :</p>
<ul>
<li>Simplify network server development</li>
<li>Be able to support a lot of connections</li>
</ul>
<p>It is actually able to support a lot of connections : On a little Linux server using Mono (with 512 MB memory with swap deactivated), I easily managed to listen to 60 000 simultaneous connections without consuming more than 40% of the server&#8217;s memory.</p>
<p>And it allows to create network servers in only few line of code :</p>

<div class="wp_codebox"><table><tr id="p110143"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
</pre></td><td class="code" id="p1101code43"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">SoftIngenia.NetEventServer</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> TestServer <span style="color: #008000;">&#123;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
	<span style="color: #008080; font-style: italic;">/// My little server</span>
	<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
	<span style="color: #6666cc; font-weight: bold;">class</span> MyServer <span style="color: #008000;">&#123;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">String</span> BytesToString<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">Byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			var sb <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;[ {0} ] {{ &quot;</span>, data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i <span style="color: #008000;">&#41;</span>
				sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot; 0x{0:X02}&quot;</span>, data<span style="color: #008000;">&#91;</span> i <span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			sb<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot; }&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #0600FF; font-weight: bold;">return</span> sb<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span>
		<span style="color: #008080; font-style: italic;">/// My server view of the client</span>
		<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span>
		<span style="color: #6666cc; font-weight: bold;">class</span> MyClient <span style="color: #008000;">&#123;</span>
			<span style="color: #0600FF; font-weight: bold;">public</span> MyClient<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">uint</span> id <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				Id <span style="color: #008000;">=</span> id<span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">uint</span> Id <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #0600FF; font-weight: bold;">private</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> NbMessagesReceived <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Treat<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;{0}.Treat( {1} );&quot;</span>, <span style="color: #0600FF; font-weight: bold;">this</span>, BytesToString<span style="color: #008000;">&#40;</span> data <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
				NbMessagesReceived<span style="color: #008000;">++;</span>
			<span style="color: #008000;">&#125;</span>
&nbsp;
			<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
				<span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Client{{Id={0}}}&quot;</span>, Id <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #008000;">&#125;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> TcpEventServer _server<span style="color: #008000;">;</span>
		<span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">uint</span>, MyClient<span style="color: #008000;">&gt;</span> _clients <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">uint</span>, MyClient<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> MyServer<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">int</span> portNumber <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_server <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TcpEventServer<span style="color: #008000;">&#40;</span> portNumber <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			_server<span style="color: #008000;">.</span><span style="color: #0000FF;">ClientConnected</span> <span style="color: #008000;">+=</span> server_ClientConnected<span style="color: #008000;">;</span>
			_server<span style="color: #008000;">.</span><span style="color: #0000FF;">ClientDisconnected</span> <span style="color: #008000;">+=</span> server_ClientDisconnected<span style="color: #008000;">;</span>
			_server<span style="color: #008000;">.</span><span style="color: #0000FF;">BinaryDataReceivedFromClient</span> <span style="color: #008000;">+=</span> server_BinaryDataReceivedFromClient<span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> StartListening<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_server<span style="color: #008000;">.</span><span style="color: #0000FF;">StartListening</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #6666cc; font-weight: bold;">void</span> server_BinaryDataReceivedFromClient<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">uint</span> clientId, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			_clients<span style="color: #008000;">&#91;</span> clientId <span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Treat</span><span style="color: #008000;">&#40;</span> data <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #6666cc; font-weight: bold;">void</span> server_ClientDisconnected<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">uint</span> clientId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Client {0} disconnected !&quot;</span>, clientId <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			_clients<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span> clientId <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
		<span style="color: #6666cc; font-weight: bold;">void</span> server_ClientConnected<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">uint</span> clientId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Client {0} connected from {1} !&quot;</span>, clientId, _server<span style="color: #008000;">.</span><span style="color: #0000FF;">RemoteEndPoint</span><span style="color: #008000;">&#40;</span> clientId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			_clients<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span> clientId, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MyClient<span style="color: #008000;">&#40;</span> clientId <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #6666cc; font-weight: bold;">class</span> Program <span style="color: #008000;">&#123;</span>
		<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
			var myserver <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MyServer<span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">3000</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			myserver<span style="color: #008000;">.</span><span style="color: #0000FF;">StartListening</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;Listening...&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
			Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>This app launched gives you something like that :</p>

<div class="wp_codebox"><table><tr id="p110144"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1101code44"><pre class="txt" style="font-family:monospace;">Listening...
Client 1 connected from 127.0.0.1:53792 !
Client{Id=1}.Treat( [ 5 ] { 0x68 0x65 0x6C 0x6C 0x6F } ); // &quot;hello&quot;
Client{Id=1}.Treat( [ 2 ] { 0x0D 0x0A } ); // '&lt;CR&gt;' '&lt;LF&gt;'
Client 1 disconnected !</pre></td></tr></table></div>

<p>The library also enables you to receive data as text. You just have to subscribe to the &#8220;ReceivedLine&#8221; event. There&#8217;s no performance cost if you don&#8217;t subscribe to the event.</p>
<p>For network server, you still need to do some frame recognition. I usually instantiate a FrameParsing class into every client on the server side. </p>
<p>You can <a href="http://florent.clairambault.fr/downloads/SoftIngenia.NetEventServer.zip">download the NetEventServer library</a> with its XML and PDB files.</p>
]]></content:encoded>
			<wfw:commentRss>http://florent.clairambault.fr/neteventserver/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 2.356 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-07-29 16:38:54 -->
