<?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>OpenBaseMovil &#187; Tutorials</title>
	<atom:link href="/category/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://openbasemovil.org</link>
	<description>J2ME Framework, J2ME database and more</description>
	<lastBuildDate>Mon, 25 May 2009 08:32:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using the networking library openbasemovil-net (II)</title>
		<link>http://openbasemovil.org/2008/05/24/using-openbasemovil-net-ii/</link>
		<comments>http://openbasemovil.org/2008/05/24/using-openbasemovil-net-ii/#comments</comments>
		<pubDate>Fri, 23 May 2008 23:39:05 +0000</pubDate>
		<dc:creator>Narciso Cerezo</dc:creator>
				<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://openbasemovil.org/?p=45</guid>
		<description><![CDATA[We will now address how to create a servlet to handle RemoteCalls from our device.
The first piece of code is a very generic servlet that takes the binary data, handles it to another class, and sends back the result. That second class is the one that will do the parsing. This way is easier to [...]]]></description>
			<content:encoded><![CDATA[<p>We will now address how to create a servlet to handle RemoteCalls from our device.</p>
<p>The first piece of code is a very generic servlet that takes the binary data, handles it to another class, and sends back the result. That second class is the one that will do the parsing. This way is easier to perform unit testing, for example.</p>
<p>Here is the code for the servlet:</p>
<pre class="java"><span style="color: #808080; font-style: italic;">/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
&nbsp;
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
&nbsp;
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
*/</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RPCServlet
    <span style="color: #000000; font-weight: bold;">extends</span> HttpServlet
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</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: #aaaadd; font-weight: bold;">String</span></a> infoPage;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> init<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">final</span> ServletConfig config <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        infoPage = config.<span style="color: #006600;">getInitParameter</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;informationPage&quot;</span> <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doGet<span style="color: #66cc66;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest    request,
            <span style="color: #000000; font-weight: bold;">final</span> HttpServletResponse response
    <span style="color: #66cc66;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> ServletException,
                   <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: #aaaadd; font-weight: bold;">IOException</span></a>
    <span style="color: #66cc66;">&#123;</span>
        request.<span style="color: #006600;">getRequestDispatcher</span><span style="color: #66cc66;">&#40;</span> infoPage <span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">forward</span><span style="color: #66cc66;">&#40;</span> request, response <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> doPost<span style="color: #66cc66;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest     request,
            <span style="color: #000000; font-weight: bold;">final</span> HttpServletResponse   response
    <span style="color: #66cc66;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> ServletException,
                   <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: #aaaadd; font-weight: bold;">IOException</span></a>
    <span style="color: #66cc66;">&#123;</span>
        processRequest<span style="color: #66cc66;">&#40;</span> request, response <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> processRequest<span style="color: #66cc66;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">final</span> HttpServletRequest request,
            <span style="color: #000000; font-weight: bold;">final</span> HttpServletResponse   response
    <span style="color: #66cc66;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> ServletException
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Read all the data into a byte array</span>
            <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedInputStream</span></a> bis = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedInputStream</span></a><span style="color: #66cc66;">&#40;</span>
                    request.<span style="color: #006600;">getInputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a> baos = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #993333;">int</span> c = bis.<span style="color: #006600;">read</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span> c != <span style="color: #cc66cc;">-1</span> <span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                baos.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span> c <span style="color: #66cc66;">&#41;</span>;
                c = bis.<span style="color: #006600;">read</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> data = baos.<span style="color: #006600;">toByteArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// Handle the data to the RPCBroker class</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> result = RPCBroker.<span style="color: #006600;">handle</span><span style="color: #66cc66;">&#40;</span> data <span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// Return the result to the device</span>
            response.<span style="color: #006600;">setContentType</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;application/octet-stream&quot;</span> <span style="color: #66cc66;">&#41;</span>;
            response.<span style="color: #006600;">setContentLength</span><span style="color: #66cc66;">&#40;</span> result.<span style="color: #006600;">length</span> <span style="color: #66cc66;">&#41;</span>;
            response.<span style="color: #006600;">getOutputStream</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">write</span><span style="color: #66cc66;">&#40;</span> result <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#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: #aaaadd; font-weight: bold;">Exception</span></a> e <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> ServletException<span style="color: #66cc66;">&#40;</span> e <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now the RPCBroker has to parse the data and make the actual call. We will not be doing here the call, you can do what suits you better: from a simple if-else structure to calling objects using reflection. If you use reflection, please be careful that you allow calls to the objects you want to be called and not a generic approach, since that could lead to big security problems.</p>
<pre class="java">&nbsp;
<span style="color: #808080; font-style: italic;">/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
&nbsp;
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
&nbsp;
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see .
*/</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> RPCBroker
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Log log = LogFactory.<span style="color: #006600;">getLog</span><span style="color: #66cc66;">&#40;</span> RPCBroker.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <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;">final</span> <span style="color: #993333;">int</span> RC_SUCCESS              = <span style="color: #cc66cc;">0</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;">final</span> <span style="color: #993333;">int</span> RC_INVALID_OBJECT       = <span style="color: #cc66cc;">1</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;">final</span> <span style="color: #993333;">int</span> RC_INVALID_METHOD       = <span style="color: #cc66cc;">2</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;">final</span> <span style="color: #993333;">int</span> RC_NOT_ALLOWED          = <span style="color: #cc66cc;">3</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;">final</span> <span style="color: #993333;">int</span> RC_INVALID_ARGUMENTS    = <span style="color: #cc66cc;">4</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;">final</span> <span style="color: #993333;">int</span> RC_INVOKATION_EXCEPTION = <span style="color: #cc66cc;">5</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;">final</span> <span style="color: #993333;">int</span> RC_INVALID_CRC          = <span style="color: #cc66cc;">6</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> handle<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> data <span style="color: #66cc66;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">throws</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: #aaaadd; font-weight: bold;">IOException</span></a>,
               SerializationException
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> data == <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> sendError<span style="color: #66cc66;">&#40;</span> RC_INVALID_CRC, <span style="color: #ff0000;">&quot;Null data&quot;</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayInputStream</span></a> bais = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayInputStream</span></a><span style="color: #66cc66;">&#40;</span> data <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> ChecksumedInputStream cis = <span style="color: #000000; font-weight: bold;">new</span> ChecksumedInputStream<span style="color: #66cc66;">&#40;</span> bais <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> SerializerInputStream in = <span style="color: #000000; font-weight: bold;">new</span> SerializerInputStream<span style="color: #66cc66;">&#40;</span> cis <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span>      version     = in.<span style="color: #006600;">readByte</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span>      encryption  = in.<span style="color: #006600;">readByte</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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: #aaaadd; font-weight: bold;">String</span></a>    deviceGuid  = in.<span style="color: #006600;">readString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span>      deviceId    = in.<span style="color: #006600;">readLong</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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: #aaaadd; font-weight: bold;">String</span></a>    objectName  = in.<span style="color: #006600;">readString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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: #aaaadd; font-weight: bold;">String</span></a>    methodName  = in.<span style="color: #006600;">readString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>  parameters;
            <span style="color: #000000; font-weight: bold;">try</span>
            <span style="color: #66cc66;">&#123;</span>
                parameters = <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> in.<span style="color: #006600;">readNullableObject</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span> SerializationException e <span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</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: #aaaadd; font-weight: bold;">IOException</span></a><span style="color: #66cc66;">&#40;</span> e.<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> crc = cis.<span style="color: #006600;">getCRC</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</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: #aaaadd; font-weight: bold;">String</span></a>    endOfData   = in.<span style="color: #006600;">readString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> endOfData.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;END_OF_DATA&quot;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span> originalCRC = in.<span style="color: #006600;">readLong</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
                <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> originalCRC != crc <span style="color: #66cc66;">&#41;</span>
                <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">return</span> sendError<span style="color: #66cc66;">&#40;</span> RC_INVALID_CRC, <span style="color: #ff0000;">&quot;Invalid CRC&quot;</span> <span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #66cc66;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">return</span> invoke<span style="color: #66cc66;">&#40;</span>
                            version,
                            deviceGuid,
                            deviceId,
                            objectName,
                            methodName,
                            parameters
                    <span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span>
            <span style="color: #66cc66;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #66cc66;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> sendError<span style="color: #66cc66;">&#40;</span> RC_INVALID_ARGUMENTS, <span style="color: #ff0000;">&quot;Wrong signature&quot;</span> <span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span> SerializationException e <span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> sendError<span style="color: #66cc66;">&#40;</span> RC_INVALID_CRC, e.<span style="color: #006600;">getMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> invoke<span style="color: #66cc66;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span>      version,
            <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: #aaaadd; font-weight: bold;">String</span></a>    deviceGuid,
            <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">long</span>      deviceId,
            <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: #aaaadd; font-weight: bold;">String</span></a>    objectName,
            <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: #aaaadd; font-weight: bold;">String</span></a>    methodName,
            <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>  parameters
    <span style="color: #66cc66;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</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: #aaaadd; font-weight: bold;">IOException</span></a>,
                   SerializationException
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Here is where you actually look up the object</span>
        <span style="color: #808080; font-style: italic;">// make the call and return the value</span>
&nbsp;
        <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AObject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Object</span></a> retval;
        <span style="color: #808080; font-style: italic;">/*
        1. Lookup object
&nbsp;
        if object is not found: return sendError( RC_INVALID_OBJECT, &quot;error message&quot; );
&nbsp;
        2. Check security, if applicable
&nbsp;
        if not allowed to call: return sendError( RC_NOT_ALLOWED, &quot;error message&quot; );
&nbsp;
        3. Make the call
&nbsp;
        store the return value in the retval variable.
        if an error raises: return sendError( RC_INVOKATION_EXCEPTION, &quot;error message&quot; );
        */</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// You should perform the appropriate try-catch</span>
        <span style="color: #808080; font-style: italic;">// The following code should be placed into a try block,</span>
        <span style="color: #808080; font-style: italic;">// when the call has been successful</span>
       <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a> baos = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
       <span style="color: #000000; font-weight: bold;">final</span> SerializerOutputStream out = <span style="color: #000000; font-weight: bold;">new</span> SerializerOutputStream<span style="color: #66cc66;">&#40;</span> baos <span style="color: #66cc66;">&#41;</span>;
       out.<span style="color: #006600;">writeInt</span><span style="color: #66cc66;">&#40;</span> RC_SUCCESS <span style="color: #66cc66;">&#41;</span>;
       out.<span style="color: #006600;">writeString</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;OK&quot;</span> <span style="color: #66cc66;">&#41;</span>;
       out.<span style="color: #006600;">writeNullableObject</span><span style="color: #66cc66;">&#40;</span> retval <span style="color: #66cc66;">&#41;</span>;
       out.<span style="color: #006600;">flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
       <span style="color: #000000; font-weight: bold;">return</span> baos.<span style="color: #006600;">toByteArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> sendError<span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> code, <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: #aaaadd; font-weight: bold;">String</span></a> message <span style="color: #66cc66;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</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: #aaaadd; font-weight: bold;">IOException</span></a>,
                   SerializationException
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a> baos = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AByteArrayOutputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ByteArrayOutputStream</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">final</span> SerializerOutputStream out = <span style="color: #000000; font-weight: bold;">new</span> SerializerOutputStream<span style="color: #66cc66;">&#40;</span> baos <span style="color: #66cc66;">&#41;</span>;
        out.<span style="color: #006600;">writeInt</span><span style="color: #66cc66;">&#40;</span> code <span style="color: #66cc66;">&#41;</span>;
        out.<span style="color: #006600;">writeString</span><span style="color: #66cc66;">&#40;</span> message != <span style="color: #000000; font-weight: bold;">null</span> ? message : <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">&#41;</span>;
        out.<span style="color: #006600;">flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">return</span> baos.<span style="color: #006600;">toByteArray</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>With this second article you should have almost everything you need to perform remote calls.</p>
<p>Please, leave your comments and suggestions.</p>
]]></content:encoded>
			<wfw:commentRss>http://openbasemovil.org/2008/05/24/using-openbasemovil-net-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the networking library openbasemovil-net</title>
		<link>http://openbasemovil.org/2008/05/22/using-openbasemovil-net/</link>
		<comments>http://openbasemovil.org/2008/05/22/using-openbasemovil-net/#comments</comments>
		<pubDate>Thu, 22 May 2008 20:28:48 +0000</pubDate>
		<dc:creator>Narciso Cerezo</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[openbasemovil]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://openbasemovil.org/?p=44</guid>
		<description><![CDATA[The Networking library in OpenBaseMovil (openbasemovil-net) provides some very useful networking methods that you can use in your applications.
One of the most useful ones is the RemoteCall class. As you can guess it is a remote call procedure, and it is a binary one. If your target platform supports the remote call or webservice optional [...]]]></description>
			<content:encoded><![CDATA[<p>The Networking library in OpenBaseMovil (openbasemovil-net) provides some very useful networking methods that you can use in your applications.</p>
<p>One of the most useful ones is the RemoteCall class. As you can guess it is a remote call procedure, and it is a binary one. If your target platform supports the remote call or webservice optional APIs, you can use them, but we wanted to provide an approach that could be used on any device (many of the current devices in the market do not support those optional APIs). We also decided to use a binary protocol since it is more compact and more efficient for binary data, instead of XML.</p>
<p>We also wanted to control how the network was reached and do it in a very simple way.</p>
<p>The library contains three main blocks: Net, Binding and RemoteCall.</p>
<p>Net has all the needed constants, and also some networking wide configuration settings. It also has a checkNetwork method that will allow you to check if you have signal and if your remote server is up and running.</p>
<p>The synchronization engine uses the deviceGuid and deviceId settings to perform simple authentication. Those settings are sent from the server to the device when the user successfully authenticates in our system using the company name, user name and password.<br />
You can use them for your own purposes, or you can ignore them, they are not really needed. If not set, they will be sent as an empty string and 0 respectively to the server in the remote call protocol.</p>
<p>The checkNetwork method provides also a small example of RemoteCall usage, as it calls a remote Object called &#8220;LoginHandler&#8221; to a method called &#8220;ping&#8221;. That checks if our server is up and running. If it can not make the call, it checks if it can read the home page of yahoo (you could put any other well known service). That is used to check if the previous error was due to the server being down or to the lack of network connectivity (we assume yahoo is up <img src='/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARemoteCall+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">RemoteCall</span></a> rc = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARemoteCall+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">RemoteCall</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">&quot;LoginHandler&quot;</span>, <span style="color: #808080; font-style: italic;">// remote object name</span>
    <span style="color: #ff0000;">&quot;ping&quot;</span>,         <span style="color: #808080; font-style: italic;">// remote object method name</span>
    <span style="color: #cc66cc;">0</span>               <span style="color: #808080; font-style: italic;">// number of parameters</span>
<span style="color: #66cc66;">&#41;</span>;
rc.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre>
<p>The RemoteCall class allows you to set a default url to call, so you can later just call the no argument version of invoke, but you can also provide an url for each call using the other version of invoke.<br />
In the previous example, the Net class assumes that you have previously set the defaultURL property in the RemoteCall class. That should be done at application startup.</p>
<p>The RemoteCall class sends a binary stream to a remote url, using a the POST method through the Binding class. RemoteCall will try to invoke the remote method up to MAX_RETRIES (currently 5) times if there&#8217;s a network error (not if the server returns an error), providing you with a level of network security (mobile networks can be very error prone).</p>
<p>The protocol is very simple, and it uses the openbasemovil-core serialization tools to send and receive the data, so complex objects can be sent and received (anything that can be serialized through the SerializerOutputStream).<br />
It is already present as a comment in the code, but I will put it here:</p>
<pre class="java">Device request:
&nbsp;
<span style="color: #66cc66;">&#40;</span>data:
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">byte</span>       version<span style="color: #66cc66;">&#125;</span>        <span style="color: #808080; font-style: italic;">// RPC version, currently 2</span>
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">byte</span>       encryption<span style="color: #66cc66;">&#125;</span>     <span style="color: #808080; font-style: italic;">// Encryption: 0 - none, 1 - Blowfish</span>
<span style="color: #66cc66;">&#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: #aaaadd; font-weight: bold;">String</span></a>     deviceGuid<span style="color: #66cc66;">&#125;</span>     <span style="color: #808080; font-style: italic;">// Device Globally Unique Identifier</span>
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">long</span>       deviceId<span style="color: #66cc66;">&#125;</span>       <span style="color: #808080; font-style: italic;">// Device remote id</span>
<span style="color: #66cc66;">&#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: #aaaadd; font-weight: bold;">String</span></a>     object<span style="color: #66cc66;">&#125;</span>         <span style="color: #808080; font-style: italic;">// Name of object to call</span>
<span style="color: #66cc66;">&#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: #aaaadd; font-weight: bold;">String</span></a>     method<span style="color: #66cc66;">&#125;</span>         <span style="color: #808080; font-style: italic;">// Object method to call</span>
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>     parameters<span style="color: #66cc66;">&#125;</span>     <span style="color: #808080; font-style: italic;">// Parameters serialized using</span>
                            <span style="color: #808080; font-style: italic;">// SerializerOutputStream</span>
                            <span style="color: #808080; font-style: italic;">// writeNullableObject method</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#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: #aaaadd; font-weight: bold;">String</span></a>     <span style="color: #ff0000;">&quot;END_OF_DATA&quot;</span><span style="color: #66cc66;">&#125;</span>      <span style="color: #808080; font-style: italic;">// Constant text to identify</span>
                                <span style="color: #808080; font-style: italic;">// data boundary</span>
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">long</span>       CRC<span style="color: #66cc66;">&#125;</span>                <span style="color: #808080; font-style: italic;">// Adler32 CRC code of the data</span>
&nbsp;
Server response:
&nbsp;
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">int</span>        responseCode<span style="color: #66cc66;">&#125;</span>       <span style="color: #808080; font-style: italic;">// Result of the method call</span>
<span style="color: #66cc66;">&#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: #aaaadd; font-weight: bold;">String</span></a>     responseMessage<span style="color: #66cc66;">&#125;</span>    <span style="color: #808080; font-style: italic;">// Result message</span>
<span style="color: #66cc66;">&#123;</span><span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>     responseData<span style="color: #66cc66;">&#125;</span>       <span style="color: #808080; font-style: italic;">// An object serialized using</span>
                                <span style="color: #808080; font-style: italic;">// SerializerOutputStream</span>
                                <span style="color: #808080; font-style: italic;">// writeNullableObject method</span>
&nbsp;
responseCode can be one of:
&nbsp;
<span style="color: #cc66cc;">0</span> - RC_SUCCESS
Everything went fine
<span style="color: #cc66cc;">1</span> - RC_INVALID_OBJECT
The requested object is invalid
<span style="color: #cc66cc;">2</span> - RC_INVALID_METHOD
The object has no method with the requested name
<span style="color: #cc66cc;">3</span> - RC_NOT_ALLOWED
The caller is not allowed to execute the requested method
<span style="color: #cc66cc;">4</span> - RC_INVALID_ARGUMENTS
The arguments passed are invalid
<span style="color: #cc66cc;">5</span> - RC_INVOKATION_EXCEPTION
The method produced an exception when invoked
<span style="color: #cc66cc;">6</span> - RC_INVALID_CRC
The CRC of the data sent is invalid</pre>
<p>So you can code a Servlet (or whatever) in your server, handle the post request, and parse the data sent using the openbasemovil-core SerializerInputStream class. Then is your turn to locate the object, the method, make sure you want the device make that call, make the call, and finally send the response back using the SerializerOutputStream class.<br />
For the response, the responseCode and responseMessage are mandatory, and if the call is successful then the responseData is also mandatory.<br />
responseData will be read by the device using SerializerInputStream.readNullableObject.</p>
<p>Your server side code should check that after the incomming data the END_OF_STREAM string si found and also the CRC code. And you should also perform the CRC checking using the openbasemovil-core Adler32 class, to make sure that everything arrived ok.</p>
<p>A bit more complex example of RemoteCall use, calling a remote object called &#8220;MyServer&#8221;, to a method called &#8220;sendImage&#8221; which receives three paremeters, a String, a byte array, and a Vector of String objects.</p>
<pre class="java"><span style="color: #808080; font-style: italic;">// Somewhere in your code at startup you will set the default URL</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARemoteCall+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">RemoteCall</span></a>.<span style="color: #006600;">setDefaultURL</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">&quot;http://myserver/rpc&quot;</span> <span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// Now we will make the call</span>
<span style="color: #808080; font-style: italic;">/**
 * Send an image to the server.
 * @param name image file name
 * @param data image raw data
 * @param attributes image attributes, a collection of Strings
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ALong+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Long</span></a> sendImage<span style="color: #66cc66;">&#40;</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: #aaaadd; font-weight: bold;">String</span></a> name,
                 <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">byte</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> data,
                 <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AVector+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Vector</span></a> attributes
<span style="color: #66cc66;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">throws</span> RemoteCallException
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARemoteCall+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">RemoteCall</span></a> rc = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ARemoteCall+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">RemoteCall</span></a><span style="color: #66cc66;">&#40;</span>
                                   <span style="color: #ff0000;">&quot;MyServer&quot;</span>,
                                   <span style="color: #ff0000;">&quot;sendImage&quot;</span>,
                                   <span style="color: #cc66cc;">3</span>
    <span style="color: #66cc66;">&#41;</span>;
    rc.<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">0</span>, name <span style="color: #66cc66;">&#41;</span>;
    rc.<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1</span>, data <span style="color: #66cc66;">&#41;</span>;
    rc.<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2</span>, attributes <span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ALong+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Long</span></a><span style="color: #66cc66;">&#41;</span> rc.<span style="color: #006600;">invoke</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>The RemoteCall method uses the Binding class to actually call the remote server with a POST method.</p>
<p>The Binding class provides an easy and extensible way to abstract you from the real method used to make the call. It also controls all the open bindings, allowing you to close them all at once to make sure that there&#8217;re no connections open.</p>
<p>Binding is an abstract class that by default uses the provided sublcass HttpBinding. As you can imagine, the HttpBinding class uses the javax.microedition.connection to actually create an http or https connection.</p>
<p>With the Binding framework you could create other Binding subclasses and use other means for connecting to the network. For example, in future versions we will provide a BluetotthBinding and a Java SE Proxy. With them you will be able to connect your phone through bluetooth using the proxy running on a PC. And everything will work exactly the same.</p>
<p>To create a new Binding class and use it you simply need to subclass Binding, implement the needed methods, and then set it as the binding factory with:</p>
<pre class="java"><span style="color: #808080; font-style: italic;">// For CLDC 1.0</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABinding+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Binding</span></a>.<span style="color: #006600;">setFactory</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> BluetoothBinding<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;</pre>
<p>or</p>
<pre class="java"><span style="color: #808080; font-style: italic;">// For CLDC 1.1</span>
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABinding+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Binding</span></a>.<span style="color: #006600;">setFactory</span><span style="color: #66cc66;">&#40;</span> BluetoothBinding.<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #66cc66;">&#41;</span>;</pre>
<p>This introduction to the networking part of OpenBaseMovil will be included in the next revisions of the documentation.<br />
We look forward for your comments to enhance and clarify it before it is actually included.</p>
]]></content:encoded>
			<wfw:commentRss>http://openbasemovil.org/2008/05/22/using-openbasemovil-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
