<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>One Minute Distraction</title>
	<atom:link href="http://oneminutedistraction.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://oneminutedistraction.wordpress.com</link>
	<description>Experiments with XMPP and Java EE</description>
	<lastBuildDate>Fri, 10 May 2013 08:32:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='oneminutedistraction.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>One Minute Distraction</title>
		<link>http://oneminutedistraction.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://oneminutedistraction.wordpress.com/osd.xml" title="One Minute Distraction" />
	<atom:link rel='hub' href='http://oneminutedistraction.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Implementing a Multi-Player XMPP Game &#8211; Part 2</title>
		<link>http://oneminutedistraction.wordpress.com/2013/03/26/implementing-a-multi-player-xmpp-game-part-2/</link>
		<comments>http://oneminutedistraction.wordpress.com/2013/03/26/implementing-a-multi-player-xmpp-game-part-2/#comments</comments>
		<pubDate>Tue, 26 Mar 2013 01:47:28 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[game]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[javaee6]]></category>
		<category><![CDATA[Vorpal]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=257</guid>
		<description><![CDATA[In my previous blog, I looked at implementing a multi-player set game. Barring any bugs, the engine is working fine. We now turn our attention to writing the client and I’m faced with the following 2 questions: Firstly what are we going to use to develop the client? What technology, language and platform are we [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=257&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In my <a href="http://oneminutedistraction.wordpress.com/2013/01/29/implementing-a-multi-player-xmpp-game-part-1/">previous blog</a>, I looked at implementing a multi-player set game. Barring any bugs, the engine is working fine. We now turn our attention to writing the client and I’m faced with the following 2 questions:</p>
<p>Firstly what are we going to use to develop the client? What technology, language and platform are we going to implement this in? </p>
<p>Second question: once we&#8217;ve written the client, how are we going to get people to install and use it. </p>
<p>The answer I think is quite obvious: the web. By the web, we mean HTML(5), JavaScript and the browser.&#160; So if this is the current thinking, is there a way that we can apply that here? </p>
<p>As you know, Vorpal applications are naturally web in the sense that they are masquerading as web application in a <a href="http://en.wikipedia.org/wiki/WAR_file_format_%28Sun%29">WAR file</a> deployed into a web container. So far we have not really exploited this schizophrenic nature of Vorpal. I have flirted with it in a previous blog on <a href="http://oneminutedistraction.wordpress.com/2012/09/12/using-server-sent-events-with-vorpal/">Server Sent Events</a>. It occurred to me that I could I could write the client for the Set game as a web application and deploy it along with server/engine component just like a regular web application. </p>
<p>The next question is how do you write do you write a HTML application that does XMPP? There are lots of JavaScript libraries that does this really well eg. <a href="http://strophe.im/strophejs/">Strophe.js</a>. While these are excellent techniques for developing XMPP application on the web, they strike me as something of an add-on viz. running XMPP inside a browser whose primary conversation of choice is HTTP. </p>
<p>What I was thinking of was is there a XMPP client that has a browser component where the primary protocol is XMPP.&#160; I could not find what I wanted so I wrote a prototype using <a href="http://www.oracle.com/technetwork/java/javafx/overview/index.html">JavaFX’s</a> <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebView.html">WebView</a>. Here&#8217;s how it would work: </p>
<p><a href="http://oneminutedistraction.files.wordpress.com/2013/03/web_interface_11.png"><img title="web_interface_1" style="border-top:0;border-right:0;border-bottom:0;border-left:0;display:inline;" border="0" alt="web_interface_1" src="http://oneminutedistraction.files.wordpress.com/2013/03/web_interface_1_thumb.png?w=970&#038;h=448" width="970" height="448" /></a> </p>
<p>A user would login to a XMPP server using this XMPP client (shown above on the left). The XMPP client performs the usual service discovery by sending&#160; <font face="Courier New">disco#info</font> packet over to the server. If any Vorpal component (external component) has web interface enabled, then it’ll reply with the <font face="Courier New">disco#info</font> packet with the following as one of its <font face="Courier New">&lt;feature&gt;</font></p>
<blockquote><p><font face="Courier New">&lt;feature var=&quot;urn:web:interface&quot;/&gt;</font></p>
</blockquote>
<p>Web interface is enabled on the component by adding<font face="Courier New"> vorpal.component.web.interface</font> property to the <font face="Courier New">xep-0114.xml</font> file. This property points to the URL of the landing page for the component (like the <a href="http://docs.oracle.com/cd/E14571_01/web.1111/e13712/web_xml.htm#i1026979">welcome page</a> in <font face="Courier New">web.xml</font>). For example in the Set game we have the following setting</p>
<blockquote><p><font style="background-color:#ffffff;" face="Courier New"><a href="http://someserver:8080/" rel="nofollow">http://someserver:8080/</a>{__context__}/set.html</font></p>
</blockquote>
<p>In the tradition of Vorpal, the <font face="Courier New">{__context__}</font> is a macro for the <a href="http://docs.oracle.com/javaee/6/tutorial/doc/bnadx.html#bnaeb">context root</a> (or application context)&#160; of the WAR. Remember that even though Vorpal is not a web application, it&#160; is packaged in a WAR file for deployment. The application server will treat it as a web application with a valid application context. So <font face="Courier New">{__context__}</font> will be replaced with the application context. The&#160; unfortunate thing is that there is no way for me to use standard Servlet API to find out about the server portion (<font face="Courier New"><a href="http://someserver:8080" rel="nofollow">http://someserver:8080</a></font>) at startup, so you’ll have to key this part in.</p>
<p>Once the XMPP client discovers that an external component supports web interface, the client will now send a <font face="courier new ">&lt;query xmlns=”urn:web:interface”/&gt;</font> to get the the URL of the interface. The component will now reply with the the URL. Once the client gets the URL, it then sends a HTTP GET to get the web page. See diagram above.</p>
<h3>The Web Interface</h3>
<p>The client exports a JavaScript object into the web interface call <font face="Courier New">xmppConnection</font>. As the name implies, this is the XMPP connection to the server. The <font face="courier new ">xmppConnection</font> object provides the following methods</p>
<p><font face="courier new">xmppConnection.registerHandler(type, JavaScript_method_name)</font> – the first parameter specifies the type of XMPP message that the JavaScript method (second parameter) should handle. The valid values for type are “message”, “presence”, “iq” and “selector”. For the second parameter, you can just enter the name of the JavaScript method name. The name has to be a String (sorry, limitation of the <font face="courier new">WebView</font>). Say you have the following</p>
<blockquote><p><font face="Courier New">xmppConnection.registerHandler(&quot;message&quot;, &quot;handleMessage&quot;)</font></p>
</blockquote>
<p>then whenever the client receives a XMPP message type, <font face="courier new ">handleMessage</font> method will be called. All JavaScript message handler has the following signature</p>
<blockquote><p><font face="courier new ">function handlerName(msg)</font></p>
</blockquote>
<p>where the <font face="courier new ">msg</font> is the XMPP message in String. I have written a few utilities to convert the String to XML. One of these is <font face="courier new ">text2xml()</font>. See Set Game example.</p>
<p>Every message send between the client and the external component is marked with the web interface’s name. This allows the XMPP client to know to which view a packet should be routed. In some cases packets arriving from some other sources that are not marked, then XMPP client will let the web view client decide if it should handle the packet. This is the “selector”. The selector behaves exactly like a message handler, has the same signature except that a message selector method returns a boolean telling the client if it wants to handle the message. If it is a true, then message will be routed to the appropriate handler.</p>
<p><font face="courier new ">xmppConnection.send(pkt)</font> – sends a packet. The <font face="courier new ">send()</font> behaves like Vorpal client. It’ll try to use the default if certain information is missing from the packet eg. if you send a packet without the from, then it’ll use the current user’s name. There are also some macro from Vorpal for that you can use: <font face="courier new ">{__domain__}</font>, <font face="courier new ">{__subdomain__}</font>, <font face="courier new ">{__self__}</font>, <font face="courier new ">{__username__}</font> and <font face="courier new ">{__resource__}</font>.</p>
<p><font face="courier new">xmppConnection.send(pkt, JavaScript_method_name)</font> – If you’re sending an IQ packet and you want a specific method to handle the reply, then use this method to send. Otherwise the IQ reply will be routed to a general handler described above.</p>
<p>Finally, if your web page has a JavaScript function <font face="courier new ">initVorpal()</font>, then this function will be called after the HTML document have been loaded viz. if you’re using jQuery, then after <font face="courier new ">$(document).ready()</font>. <font face="courier new ">initVorpal()</font> is used to perform the web interface initialization, most notably registering message handlers. You cannot use <font face="courier new">xmppConnection</font> until after the document has been loaded. </p>
<h3>Set Game Web Interface</h3>
<p>So lets look at how we go about writing a web interface by looking at an example. You can get the source <a href="http://java.net/projects/xmppcontainer/downloads/directory/miscellaneous">here</a> (Multiplayer Set Game). If you expand the zip file, the important files are</p>
<ul>
<li><font face="courier new ">set.html</font> – this the interface/web view for the set game</li>
<li><font face="courier new ">resources/set.js</font> – most of the behaviour </li>
<li><font face="courier new ">resources/set.css</font> – the stylesheet</li>
</ul>
<p>I’ve also used</p>
<ul>
<li>jQuery – for handling interaction. As it turns out jQuery is also extremely useful in manipulating and extracting information from XMPP packets</li>
<li>jStrophe – for creating XMPP message</li>
<li>vorpalutils – a set of homegrown utilities with functions culled from the Internet</li>
</ul>
<ul>Lets walk through <font face="courier new ">set.js</font> </ul>
<p><font face="courier new ">$(document).ready()</font> – get jQuery to bind the action handlers to the clear, submit and refresh button. The clear button clears the Set selection; submit button sends the 3 cards that you’ve selected to the set game server in the external component and the refresh button, refreshes the 12 cards in your view. We also setup 12 blank cards</p>
<p><font face="courier new ">initVorpal()</font> – does 2 things here. The first is that we setup the various message handlers. </p>
<p>The second is to constructs a <font face="courier new ">disco#items</font> to the JID of the subcomponent. If you recall, the <a href="http://oneminutedistraction.wordpress.com/2013/01/29/implementing-a-multi-player-xmpp-game-part-1/">first part</a> of this describes the set game room’s name is discoverable under the external component’s JID. We set a reply handler for this IQ packet. When the reply returns (<font face="courier new ">handleRoomName(msg)</font>), the client enters the room by sending a presence packet. We get the room’s name using jQuery and then construct a presence packet with jStrophe.</p>
<p>Once we have joined the game room (<font face="courier new ">handlePresence(msg)</font>), we setup the <font face="courier new ">set.html</font> by displaying the number of players and also by sending a <font face="courier new ">get_board</font> message (see <a href="http://oneminutedistraction.wordpress.com/2013/01/29/implementing-a-multi-player-xmpp-game-part-1/">first part</a>) to get the 12 cards to fill the view.</p>
<p>I’ve recorded a screencast of this </p>
<div id="scid:5737277B-5D6D-4f48-ABFC-DD9C333F4C5D:5712227e-bda3-4109-9380-06283335dd30" class="wlWriterEditableSmartContent" style="float:none;margin:0;display:inline;padding:0;">
<div><span class='embed-youtube' style='text-align:center; display: block;'><iframe class='youtube-player' type='text/html' width='630' height='385' src='http://www.youtube.com/embed/r8hYY55I0rE?version=3&#038;rel=1&#038;fs=1&#038;showsearch=0&#038;showinfo=1&#038;iv_load_policy=1&#038;wmode=transparent' frameborder='0'></iframe></span></div>
</div>
<p>&#160;</p>
<p>The latest Vorpal along with the experimental XMPP client can be downloaded from <a href="http://java.net/projects/xmppcontainer/downloads">here</a>. The Set Multiplayer can be downloaded from <a href="http://java.net/projects/xmppcontainer/downloads/directory/miscellaneous">here</a>. To run the XMPP client, type <font face="courier new ">bin/lglass</font> . Yeah its a bash script. </p>
<p>Let me know what you think.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/257/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=257&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2013/03/26/implementing-a-multi-player-xmpp-game-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2013/03/web_interface_1_thumb.png" medium="image">
			<media:title type="html">web_interface_1</media:title>
		</media:content>
	</item>
		<item>
		<title>Implementing a Multi-Player XMPP Game &#8211; Part 1</title>
		<link>http://oneminutedistraction.wordpress.com/2013/01/29/implementing-a-multi-player-xmpp-game-part-1/</link>
		<comments>http://oneminutedistraction.wordpress.com/2013/01/29/implementing-a-multi-player-xmpp-game-part-1/#comments</comments>
		<pubDate>Tue, 29 Jan 2013 05:29:55 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[game]]></category>
		<category><![CDATA[Vorpal]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[vorpal2 xmpp game multiplayer muc]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=253</guid>
		<description><![CDATA[In most of my previous blogs, I have focused on what are the new features in the Vorpal framework. Vorpal has now reach a stage of development where I feel that I can stop adding new features and let the existing one stabilizes. One aspect that really concerns me is how useful are those features [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=253&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><font size="2">In most of my previous blogs, I have focused on what are the new features in the </font><a href="http://oneminutedistraction.wordpress.com/2011/05/31/vorpal-framework-a-second-try/"><font size="2">Vorpal</font></a><font size="2"> framework. Vorpal has now reach a stage of development where I feel that I can stop adding new features and let the existing one stabilizes. One aspect that really concerns me is how useful are those features that I’ve added?</font></p>
<p><font size="2">So for the next few blogs I’ll focus on using Vorpal to develop a moderately complex (okay hello_world++) XMPP application; the aim is to show how to use Vorpal to develop something useful (as oppose to code snippets) and also the larger issue of using XMPP in your application. </font></p>
<p><font size="2">I’m no XMPP expert and mostly what I know comes from reading the <a href="http://xmpp.org">standards</a>, books (<a href="http://shop.oreilly.com/product/9780596521271.do">one book</a> in particular) and this excellent series of blogs on the design of Chesspark (part <a href="http://metajack.im/2008/11/21/chesspark_design_details_part_1_why_xmpp/">1</a>, <a href="http://metajack.im/2008/11/24/chesspark-design-details-part-2-session-handling/">2</a> and <a href="http://metajack.im/2008/11/25/chesspark-design-details-part-3-game-design/">3</a>).</font></p>
<h2>Set Game</h2>
<p><font size="2"><img style="display:inline;margin-left:0;margin-right:0;" border="1" align="right" src="http://www.setgame.com/images/setcards/small/65.gif" />The <a href="http://setgame.com/set/main_page.htm">Set game</a> is a real-time, party style card game. 12 cards are laid out in 4 rows of 3 on the table. Each card has the following four attributes: colour, number, symbol and shading; each attribute has 3 different values. The example card shown on the right has the following attributes: colour: red, number: 2, symbol: diamond, shading: open. </font></p>
<p>The game is very simple; you are to pick 3 cards that have the same 3 attributes; this constitute a set. Alternatively you can pick 3 cards, all with different attributes. You can see some examples <a href="http://setgame.com/set/rules_set.htm">here</a>. The cards are replenished from the deck after you have removed your set. Game ends when the deck runs out of card and there are no more set on the table. The winner is the player with the most set.</p>
<p><font size="2">Detailed rules and examples can be found <a href="http://setgame.com/set/rules_basic.htm">here</a>.</font></p>
<h2>Design</h2>
<p><font size="2">We will implement the Set game using <a href="http://xmpp.org/extensions/xep-0045.html">MUC</a> (multi-user chat); the reason is that since this is a real-time, multi-player party style game, we think that MUC would be a good fit. The other option is to use <a href="http://xmpp.org/extensions/xep-0060.html">pubsub</a>. Every MUC set game room has a ‘dealer’. The dealer is the entity that is responsible for keeping score, checking if you have a set, replenishing cards, etc. The dealer is our Set game component.</font></p>
<p><font size="2">Players join the game by entering the game room. Once they’re in the game room, they </font></p>
<ul>
<li><font size="2">will be notified of who else is in the game room. Also when player leaves, you will also be notified. All these are supported through MUC.</font> </li>
<li><font size="2">find the game room by sending a <font face="Courier">disco#items</font> to the set game service</font> </li>
<li><font size="2">get the state of the board from the from the dealer</font> </li>
<li><font size="2">try to take a set of 3 cards by informing the dealer. This is between the dealer and the player only (‘chat’ message)</font> </li>
<li><font size="2">dealer will inform everyone if a player is successful in taking a set (‘groupchat’ message)</font> </li>
</ul>
<h2>Game Protocol</h2>
<p><font size="2">The game protocol can be roughly broken up into 3 parts</font></p>
<p> <font size="2">
<ol>
<li><font size="2">A player entering and leaving the game room – these are standard MUC messages</font> </li>
<li>Game specific messages – these are game messages. We will use <font face="Courier">&lt;message&gt;</font> for all Set game moves. Game messages from the dealer are also <font face="Courier">&lt;message&gt;</font> type. </li>
<li>Game room membership – messages sent out by MUC about players joining and leaving the game room </li>
</ol>
<p> </font><br />
<h2>Implementation</h2>
<h3>Setting up the Game Room</h3>
<p>When the game starts up, the first thing that it’ll do is to setup the game room. In this implementation, we only support one game room; you are welcome to take the code and modify to support more. We do this by listening to the <font face="Courier New">ComponentPostStart</font> event which is after the Set game component has successfully connect to the XMPP server; see <a href="http://oneminutedistraction.wordpress.com/2012/03/23/lifecycle-handling-errors-service-discovery-registration-and-response-context-2/">here</a> for more details on lifecycle events.</p>
<blockquote><p><font face="Courier New">private void postStart(@Observes ComponentPostStart postStartEvt) {        <br />&#160;&#160; String confRoom = randomString(16) + &quot;@conference.&quot; </font><font face="Courier New">+ postStartEvt.getDomain();        <br />&#160;&#160; <strong><font color="#008000">gameState.setRoomName(confRoom);</font></strong>         <br />&#160;&#160; <strong>JoinRoom join = new JoinRoom(Constants.DEALER + &quot;@&quot; </strong></font><font face="Courier New"><strong>+ postStartEvt.getSubdomain(), confRoom + &quot;/&quot; + Constants.DEALER);</strong>         <br />&#160;&#160;&#160; try {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; postStartEvt.getComponentContext().send(join);         <br />&#160;&#160;&#160; } catch (ComponentException ex) { }         <br />}</font> </p>
</blockquote>
<p>The game room’s name, called <font face="Courier New">confRoom</font> above, is randomly generated. The Set game component then joins the room with the nick name <font face="Courier New">dealer</font>. The following message will be sent out.</p>
<blockquote><p><font size="2" face="Courier">&lt;presence from=&quot;dealer@set.batcomputer&quot;      <br />&#160;&#160;&#160;&#160;&#160; to=somerandomstringname@conference.batcomputer/dealer&gt;       <br />&#160;&#160; &lt;x xmlns=&quot;</font><font face="cour"><a href="http://jabber.org/protocol/muc&quot;/" rel="nofollow">http://jabber.org/protocol/muc&quot;/</a></font><font face="Courier">&gt;      <br />&lt;/presence&gt;</font></p></blockquote>
<p>We perform the reverse when the component shuts down by destroying the room. </p>
<p><font face="Courier New">gameState</font> is an <font face="Courier New">@ApplicationScoped</font> CDI managed object. It stores information like the game room, card deck, scores, etc. As you can see above, once we have generated the game room, we save that in the <font face="Courier New">gameState</font> object.</p>
<p>Once we have send the join room message, we now have to wait for a confirmation from the XMPP server. The message will come in the form of a <font face="Courier New">&lt;presence&gt;</font> message.</p>
<blockquote><p><font face="Courier New"><strong><font color="#800000">@Presence            <br />@From(&quot;{room}@conference.&quot; + PredefinedBindings.PARAMETER_DOMAIN + &quot;/{ignore}&quot;)             <br /></font></strong>public class ConfirmChatRoom { </font></p>
<p><font face="Courier New">&#160;&#160; @Inject GameState gameState;        <br />&#160;&#160; @Inject ComponentContext compCtx; </font></p>
<p><font face="Courier New">&#160;&#160; <strong><font color="#800000">@XmlPath(namespace={&quot;n&quot;, Constants.MUC_USER}, path=&quot;/presence/n:x/n:item[@role='moderator']&quot;)            <br /></font></strong>&#160;&#160; private ResponseContext roomCreated() { </font></p>
<p><font face="Courier New">&#160;&#160;&#160;&#160;&#160; ResponseContext respCtx = new ResponseContext(ResponseContext.Type.IQ);        <br /><strong><font color="#008000">&#160;&#160;&#160;&#160;&#160; respCtx.add(IQ.Type.set)            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .add(new IQChildElement(&quot;query&quot;, Constants.MUC_OWNER))             <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .add(DataFormBuilder.create(DataForm.Type.submit));</font></strong>         <br /></font></p>
<p><font face="Courier New"><strong><font color="#0000ff">&#160;&#160;&#160;&#160;&#160; compCtx.add(new ItemSpecification(gameState.getRoomName(), &quot;Set game room name&quot;));            <br />&#160;&#160;&#160;&#160;&#160; compCtx.add(new FeatureSpecification(PredefinedBindings.DISCO_ITEMS));</font></strong> </font></p>
<p><font face="Courier New">&#160;&#160;&#160;&#160;&#160; return (respCtx);        <br />&#160;&#160;&#160; }         <br />}</font></p>
</blockquote>
<p>We look for the confirmation message be examining the <font face="Courier New">&lt;presence&gt;</font> message with an <font face="Courier New">&lt;item&gt;</font> tag in the MUC <a href="http://xmpp.org/registrar/namespaces.html">namespace</a>; furthermore the <font face="Courier New">&lt;item&gt;</font> must have the value <font face="Courier New">moderator</font> in the <font face="Courier New">role</font> attribute. This is our cue; the role moderator tell us that we are the room owner. So when we received this message, we can now enter the room.</p>
<p>To enter the room, we create an empty data form of Submit type and send that back to the room in an IQ-set message. This is shown in <font color="#008000">green</font>.</p>
<p>The other thing that we need to do is to make our game room discoverable by <font face="Courier New">disco#info</font> and <font face="Courier New">disco#item</font>. Since the game room’s name is not going to change throughout the life time of the Set game component, we use static registration (in <font color="#0000ff">blue</font>). We add <font face="Courier New">disco#item</font> feature to our Set game component JID; then we add the game room’s JID as in <font face="Courier New">&lt;item&gt;</font>.</p>
<p>Note on using <font face="Courier New">@XmlPath</font> annotation for matching; I’ve often <font face="Courier New">@XmlElement</font>. <font face="Courier New">@XmlElement</font> is the simpler of the 2. If you want to either match immediate child under the packet’s root (<font face="Courier New">&lt;message&gt;</font>, <font face="Courier New">&lt;presence&gt;</font> or <font face="Courier New">&lt;iq&gt;</font>) use <font face="Courier New">@XmlElement</font>. Use <font face="Courier New">@XmlPath</font>, based on <a href="http://www.w3schools.com/xpath/default.asp">XPath</a>, gives you more control and matching capability at the expense of simplicity.</p>
<h3>Handling Player Membership</h3>
<p><font size="2">As a result of using MUC, our component don’t really have to do that much when players join or leave the game room. Since the Set component is in the game room, it will receive <font face="Courier New">&lt;presence&gt;</font> message for every player that joins or leave the room. Furthermore, the MUC will also send notification of the new player to every other participant in the game room. Nice!</font></p>
<p><font size="2">The following is an example message of a player joining the game room</font></p>
<blockquote><p><font face="Courier New">&lt;presence from=”fred@batcomputer/pidgin”      <br />&#160;&#160;&#160;&#160;&#160; to=”somerandomstringname@conference.batcomputer/fred”&gt;       <br />&#160;&#160; &lt;x xmlns=”http://jabber.org/protocol/muc#user”&gt;       <br />&#160;&#160;&#160;&#160;&#160; <strong><font color="#0080ff">&lt;item affiliation=”member” role=”participant”/&gt;</font></strong>       <br />&#160;&#160; &lt;/x&gt;       <br />&lt;/presence&gt;</font> </p></blockquote>
<p><font size="2">The following presence message handler handles both players joining and leaving the game room.</font></p>
<blockquote><p><font face="Courier New"><strong><font color="#804000">@Presence</font>           <br /><font color="#008000">@From(&quot;{roomName}@conference.&quot; + PredefinedBindings.PARAMETER_DOMAIN + &quot;/{alias}&quot;)</font></strong>         <br />public class PlayerMembershipHandler { </font></p>
<p><font face="Courier New">&#160;&#160; @Inject GameState gameState; </font></p>
<p><font face="Courier New"><strong><font color="#0080ff">&#160;&#160; @XmlPath(namespace={&quot;n&quot;, Constants.MUC_USER}, path=&quot;/presence/n:x/n:item[@role='participant']&quot;)</font></strong>         <br />&#160;&#160; private void playerJoining(<strong><font color="#008000">@Named(&quot;alias&quot;)</font></strong> String player) {         <br />&#160;&#160;&#160;&#160;&#160; gameState.createPlayer(player);         <br />&#160;&#160; } </font></p>
<p><font face="Courier New"><strong><font color="#cc0000">&#160;&#160; @XmlPath(namespace={&quot;n&quot;, Constants.MUC_USER}, path=&quot;/presence/n:x/n:item[@role='none']&quot;)</font></strong>         <br />&#160;&#160; private void playerLeaving(<strong><font color="#008000">@Named(&quot;alias&quot;)</font></strong> String player) {         <br />&#160;&#160;&#160;&#160;&#160; gameState.removePlayer(player);         <br />&#160;&#160; }</font></p>
<p><font face="Courier New">}</font></p>
</blockquote>
<p><font size="2">The way that these membership messages are matched is exactly the same as confirming the chat room. We can actually combine the <font face="Courier New">PlayerMembershipHandler</font> and <font face="Courier New">ConfirmChatRoom</font> by assigning the <font face="Courier New">@XmlPath</font> element to a capture parameter of the type <font color="#555555" face="Courier New"><a href="http://dom4j.sourceforge.net/dom4j-1.6.1/apidocs/org/dom4j/Element.html">Element</a></font>. I’ll leave this as an exercise to the reader.</font></p>
<h3>Playing Set </h3>
<p>The Set implementation have 2 types of game message both using <font face="Courier New">&lt;message&gt;</font> as envelop. The first of these 2 message is the get_board message. This message is for a new participant to get the current state of the board. The message is send directly to the dealer.</p>
<blockquote><p><font face="Courier New">&lt;message from=&quot;fred@batcomputer/pidgin&quot;        <br />&#160;&#160;&#160;&#160;&#160; to=”somerandomstringname@conference.batcomputer/dealer”&#160; type=&quot;chat&quot;&gt;         <br />&#160;&#160; &lt;set xmlns=&quot;uri:game:set&quot; command=&quot;get_board&quot;/&gt;         <br />&lt;/message&gt;</font></p>
<p>   <font style="background-color:#ffffff;"></font></p></blockquote>
<p>The handler for <font face="Courier New">get_board</font> message is as follows</p>
<blockquote><p><font face="Courier New">@Message @XmlElement(tag=Constants.SET_TAG)      <br />@To(Constants.DEALER + &quot;@&quot; + PredefinedBindings.PARAMETER_SUBDOMAIN)       <br />public class BoardLayoutCommandHandler {       <br />&#160;&#160; @Inject GameState gameState;       <br />&#160;&#160; @XmlPath(namespace={&quot;n&quot;, Constants.SET_NAMESPACE}, path=&quot;/message/n:set[@command='get_board']&quot;)       <br />&#160;&#160; @From(&quot;{from}&quot;)       <br />&#160;&#160; public List&lt;Object&gt; handle(@Named(“from”) String from) {       <br />&#160;&#160;&#160;&#160;&#160; //Construct and return the board – see source       <br />&#160;&#160; } </font></p></blockquote>
<p>which generates the following message</p>
<blockquote><p><font face="Courier New">&lt;message type=&quot;chat&quot; to=somerandomstringname@conference.batcomputer/fred&#160; <br />&#160;&#160;&#160;&#160; from=”somerandomstringname@conference.batcomputer/dealer“&gt;         <br />&#160;&#160; &lt;ns2:set xmlns:ns2=&quot;uri:game:set&quot; command=&quot;get_board&quot;&gt;         <br />&#160;&#160;&#160;&#160;&#160; &lt;ns2:data&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board_layout&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;0&quot;&gt;22&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;1&quot;&gt;33&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;2&quot;&gt;78&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;3&quot;&gt;43&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; …         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;9&quot;&gt;16&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;10&quot;&gt;59&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;11&quot;&gt;69&lt;/board&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/board_layout&gt;         <br />&#160;&#160;&#160;&#160;&#160; &lt;/ns2:data&gt;         <br />&#160;&#160; &lt;/ns2:set&gt;         <br />&lt;/message&gt;</font></p>
</blockquote>
<p>The second message is a<font face="Courier New"> try_take</font> message</p>
<blockquote><p><font face="Courier New">&lt;message to=&quot;somerandomstringname@setgame.batcomputer/dealer&quot; type=&quot;chat&quot;        <br />&#160;&#160;&#160;&#160;&#160; from=”</font><font face="Courier New">fred@batcomputer/pidgin”</font><font face="Courier New">&gt;       <br />&#160;&#160; &lt;set xmlns=&quot;uri:game:set&quot; command=&quot;try_take&quot;&gt;        <br />&#160;&#160;&#160;&#160;&#160; &lt;data&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;cards_taken&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;9&quot;/&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;7&quot;/&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;10&quot;/&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/cards_taken&gt;        <br />&#160;&#160;&#160;&#160;&#160; &lt;/data&gt;        <br />&#160;&#160; &lt;/set&gt;        <br />&lt;/message&gt;</font></p>
</blockquote>
<p>As the name implies, a <font face="Courier New">try_take</font> is where a player tries to take 3 card to form a set. The cards are identified by their board position. Like the <font face="Courier New">get_board</font> message, the <font face="Courier New">try_take</font> command is sent directly to the dealer (message type is <font face="Courier New">chat</font>) and not broadcast to the game room.</p>
<p>The message handler for try_take is very similar to get_board, so I’m not going to list it here. Look at the source if you’re interested. </p>
<p>Finally if the try_take is successful, dealer will send a <font face="Courier New">take_card</font> message into the game room (message type is groupchat) notifying everyone of the new cards and the score</p>
<blockquote><p><font face="Courier New">&lt;message type=&quot;groupchat&quot; to=&quot;somerandomstringname@conference.batcomputer&quot;        <br />&#160;&#160;&#160;&#160;&#160; from=”somerandomstringname@conference.batcomputer/dealer”&gt;        <br />&#160;&#160; &lt;ns2:set xmlns:ns2=&quot;uri:game:set&quot; command=&quot;take_card&quot;&gt;        <br />&#160;&#160;&#160;&#160;&#160; &lt;ns2:data&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;cardsInDeck&gt;66&lt;/cardsInDeck&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;cards_taken&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;9&quot;&gt;42&lt;/board&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;7&quot;&gt;16&lt;/board&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;10&quot;&gt;20&lt;/board&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/cards_taken&gt;        <br /></font><font face="Courier New"><strong><font color="#008000">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;cards_new&gt;           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;9&quot;&gt;08&lt;/board&gt;            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;7&quot;&gt;60&lt;/board&gt;            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;board id=&quot;10&quot;&gt;44&lt;/board&gt;            <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/cards_new&gt;</font></strong>        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;name&gt;fred&lt;/name&gt;        <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;score&gt;1&lt;/score&gt;        <br /><strong><font color="#0000ff">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;taken&gt;true&lt;/taken&gt;</font></strong>        <br />&#160;&#160;&#160;&#160;&#160; &lt;/ns2:data&gt;        <br />&#160;&#160; &lt;/ns2:set&gt;        <br />&lt;/message&gt;</font></p>
</blockquote>
<p>If <font face="Courier New">try_take</font> is not successful, the <font face="Courier New">take_card</font> message will not have the <font face="Courier New">&lt;cards_new&gt;</font> section and the value in <font face="Courier New">&lt;taken&gt;</font> element will be <font face="Courier New">false</font>.</p>
<p>I’ve implemented most of the game except for the end game; at the moment the component does not known when the game ends and declares a winner. I’ll try fixing this in the future. </p>
<p>You can find the source code of this in <a href="http://java.net/projects/playground/sources/source-code-repository/show/setgame">playground</a>. Let me know what you think.</p>
<p>My next blog talk about writing a client for this Set game component. </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/253/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/253/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=253&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2013/01/29/implementing-a-multi-player-xmpp-game-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>

		<media:content url="http://www.setgame.com/images/setcards/small/65.gif" medium="image" />
	</item>
		<item>
		<title>Vorpal2 Client</title>
		<link>http://oneminutedistraction.wordpress.com/2012/12/14/vorpal2-client/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/12/14/vorpal2-client/#comments</comments>
		<pubDate>Fri, 14 Dec 2012 02:41:59 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[vorpal2 xmpp java client]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=249</guid>
		<description><![CDATA[Since rewriting Vorpal2 core framework, I&#8217;ve made a number of changes to the client side. I first introduce Vorpal client here; in Vorpal2 client, the programming model remains the same but bootstrapping and login in XMPP server have been change. Lets look at these changes. Component Class Every Vorpal application, whether its server or client [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=249&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Since <a target="_blank" href="http://oneminutedistraction.wordpress.com/2012/10/08/new-beginnings-again/">rewriting Vorpal2</a> core framework, I&#8217;ve made a number of changes to the client side. I first introduce Vorpal client <a target="_blank" href="http://oneminutedistraction.wordpress.com/2012/05/01/yet-another-xmpp-client-library/">here</a>; in Vorpal2 client, the programming model remains the same but bootstrapping and login in XMPP server have been change. Lets look at these changes.<br />
<h3>Component Class</h3>
<p>Every Vorpal application, whether its server or client side, requires a configuration file for startup. This is an XML file. The only change here is the <font face="monospace">component-class</font> entry. This has changed from <font face="monospace">com.kenai.jabberwocky.framework.client.JabberwockyClientComponent</font> to <font face="monospace">c.k.j.f.c.XMPPClientComponent</font>. If you have written a client application, the following in <font color="#CC0000">red</font> is what you need to change in your configuration file</p>
<blockquote><p><font face="monospace">&lt;connection port=”5222″ domain=”batcomputer”&gt;<br />&nbsp;&nbsp; &lt;component-class name=”<font color="#CC0000"><b>com.kenai.jabberwocky.framework.client.XMPPClientComponent</b></font>”/&gt;<br />&nbsp;&nbsp; &lt;classes-root&gt;classes&lt;/classes-root&gt;<br />&nbsp;&nbsp; &lt;packages&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;package name=”com.acme.xmpp.client”/&gt;<br />&nbsp;&nbsp; &lt;/packages&gt;<br />&nbsp;&nbsp; &lt;properties&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=”vorpal.component.description” value=”A component to query the customer’s database”/&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;property name=”vorpal.component.name” value=”Customer Query Client”/&gt;<br />&nbsp;&nbsp; &lt;/properties&gt;<br />&lt;/connection&gt;</font></p>
</blockquote>
<h3>Bootstrapping Vorpal2 Client</h3>
<p>A typical Vorapl2 client starts in 2 different phases; the first is to start <a target="_blank" href="http://docs.oracle.com/javaee/6/tutorial/doc/giwhl.html">CDI</a> (using <a target="_blank" href="http://seamframework.org/Weld">Weld</a>). You can read about starting CDI in Java SE <a target="_blank" href="http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#weld-se">here</a>. The second phase is to start Vorpal2 framework which will in turn start your Vorpal2 Client application. This is typically done by capturing the <font face="monospace"><a target="_blank" href="http://www.jarvana.com/jarvana/view/org/jboss/weld/se/weld-se-core/1.1.5.Final/weld-se-core-1.1.5.Final-javadoc.jar!/org/jboss/weld/environment/se/events/ContainerInitialized.html">ContainerInitialized</a></font> event. </p>
<p>The following code snippet shows how this is done.<br />
<blockquote><font face="monospace"><font color="#009900"><b>@Inject ClientConnectionManager mgr;</b></font></p>
<p>private void containerInitialized(<font color="#CC0000"><b>@Observes ContainerInitialized</b></font> ciEvt) {<br />&nbsp;&nbsp; String username = &#8230;<br />&nbsp;&nbsp; String password = &#8230;<br /><font color="#000099"><b>&nbsp;&nbsp; ClientConnectionManager.Connection conn = mgr.createConnection();<br />&nbsp;&nbsp; conn.load(new File(&#8220;client.xml&#8221;));<br />&nbsp;&nbsp; if (!conn.login(username, password)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Authentication failed<br />&nbsp;&nbsp; }</b></font><br />&nbsp;&nbsp; //Vorpal2 client is now started<br />}</p>
<p>private void containerShutdown(<font color="#CC0000"><b>@Observes BeforeShutdown</b></font> bsEvt) {<br /><font color="#000099"><b>&nbsp;&nbsp; mgr.getConnection().logout();</b></font><br />}</font></p></blockquote>
<p>Once we got the <font face="monospace">ContainerInitialized</font> event, you can create a connection from <font face="monospace">ClientConnectionManager</font>. The <font face="monospace">ClientConnectinManager</font> is an <font face="monospace"><a target="_blank" href="http://docs.oracle.com/javaee/6/api/javax/enterprise/context/ApplicationScoped.html">ApplicationScoped</a></font> object which is injected into your startup class. The next step is to load the configuration file. The <font face="monospace">load()</font> method accepts the configuration either as an <font face="monospace">InputStream</font>, a <font face="monospace">File</font> or a <font face="monospace">ClientComponentConfiguration</font> object. The <font face="monospace">ClientComponentConfiguration</font> is the object representation of the configuration file; it is <a target="_blank" href="http://www.oracle.com/technetwork/articles/javase/index-140168.html">JAXB</a> enabled so you can use JAXB to unmarshall a XML configuration file. Alternatively you can also use the helper method <font face="monospace">ClientComponentConfiguration.read()</font> to return an instance of <font face="monospace">ClientComponentConfiguration</font>. <font face="monospace">read()</font> takes either a <font face="monospace">File</font> or an <font face="monospace">InputStream</font> as parameter.</p>
<p>After successfully loading the configuration, you can now attempt to login. The <font face="monospace">login()</font> method will return a <font face="monospace">false</font> if it fails to login. You can optionally pass a <a target="_blank" href="http://xmpp.org/extensions/xep-0029.html#sect-id147456">resource</a> into the <font face="monospace">login()</font> method.</p>
<p>To close a connection, retrieve the default connection from <font face="monospace">ClientConnectionManager.getConnection()</font> and call the <font face="monospace">logout()</font> method.<br />
<h3>Running Vorpal2 Client</h3>
<p>Vorpal2 client applications are dependent on the following 4 runtime JAR files</p>
<ul>
<li><font face="monospace">vorpal2.jar</font></li>
<li><font face="monospace">weld-se-core.jar</font></li>
<li><font face="monospace">weld-se.jar</font></li>
<li><font face="monospace">jabberwocky_support2.jar</font></li>
</ul>
<p>Due to classloading issues with log4j, <font face="monospace">jabberwocky_support2.jar</font> <u><b>MUST</b></u> the the last JAR file in your <font face="monospace">CLASSPATH</font>; otherwise Weld will fail initialization.</p>
<p>Client support currently is still very basic. Only clear connections are supported; SSL and other features are planned.</p>
<p>To try Vorpal2 client support, download the latest bundle from <a href="http://java.net/projects/xmppcontainer/downloads" target="_blank">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=249&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/12/14/vorpal2-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>
	</item>
		<item>
		<title>NetBeans Plugin for Vorpal2</title>
		<link>http://oneminutedistraction.wordpress.com/2012/11/28/netbeans-plugin-for-vorpal2/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/11/28/netbeans-plugin-for-vorpal2/#comments</comments>
		<pubDate>Wed, 28 Nov 2012 06:58:34 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[vorpal xmpp java tools netbeans]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=247</guid>
		<description><![CDATA[I’ve finally manage to get some time to update NetBeans plugin for Vorpal2. One major change the plugin is that as Vorpal2 no longer supports Component and AbstractComponent, the plugin is now reflects this. Lets step through the new tool. You can also treat this as a tutorial for creating a Vorpal2 application. Step 1. [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=247&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><font size="3"><a href="http://oneminutedistraction.files.wordpress.com/2012/11/tools-belts.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;float:right;padding-top:0;border-width:0;" title="tools-belts" border="0" alt="tools-belts" align="right" src="http://oneminutedistraction.files.wordpress.com/2012/11/tools-belts_thumb.jpg?w=104&#038;h=105" width="104" height="105" /></a>I’ve finally manage to get some time to update NetBeans plugin for Vorpal2. One major change the plugin is that as Vorpal2 no longer supports <font face="Courier New"><a href="http://www.igniterealtime.org/builds/tinder/docs/latest/javadoc/org/xmpp/component/Component.html">Component</a></font> and <font face="Courier New"><a href="http://www.igniterealtime.org/builds/tinder/docs/latest/javadoc/org/xmpp/component/AbstractComponent.html">AbstractComponent</a></font>, the plugin is now reflects this. </font></p>
<p><font size="3">Lets step through the new tool. You can also treat this as a tutorial for creating a Vorpal2 application.</font></p>
<hr />
<p><font size="3"><a href="http://oneminutedistraction.files.wordpress.com/2012/11/screen0.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;float:left;padding-top:0;border-width:0;" title="screen0" border="0" alt="screen0" align="left" src="http://oneminutedistraction.files.wordpress.com/2012/11/screen0_thumb.png?w=244&#038;h=168" width="244" height="168" /></a><strong>Step 1</strong>. Create a web application</font></p>
<p> 
<p><font size="3"></font></p>
<hr />
<p><font size="3"><a href="http://oneminutedistraction.files.wordpress.com/2012/11/screen1.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;float:right;padding-top:0;border-width:0;" title="screen1" border="0" alt="screen1" align="right" src="http://oneminutedistraction.files.wordpress.com/2012/11/screen1_thumb.png?w=244&#038;h=168" width="244" height="168" /></a><strong>Step 2</strong>. Give a name to your web application. The XMPP component will be packaged in a WAR</font></p>
<p> <br />
<hr />
<p><font size="3"><a href="http://oneminutedistraction.files.wordpress.com/2012/11/screen2.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;float:left;padding-top:0;border-width:0;" title="screen2" border="0" alt="screen2" align="left" src="http://oneminutedistraction.files.wordpress.com/2012/11/screen2_thumb.png?w=244&#038;h=155" width="244" height="155" /></a><strong>Step 3</strong>. Make sure that you’re using Java EE 6. You can either use Web Profile or the full EE 6 platform. You must also enable CDI. Vorpal2 relies on this heavily.</font></p>
<p><font size="3">If you’ve forgotten to select CDI here, you can manually create a <font face="Courier New"><a href="http://docs.oracle.com/javaee/6/tutorial/doc/gjbnz.html">beans.xml</a></font> under <font face="Courier New">WEB-INF</font> directory.</font></p>
<p> <br />
<hr />
<p><font size="3"><a href="http://oneminutedistraction.files.wordpress.com/2012/11/screen3.png"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;float:right;padding-top:0;border-width:0;" title="screen3" border="0" alt="screen3" align="right" src="http://oneminutedistraction.files.wordpress.com/2012/11/screen3_thumb.png?w=244&#038;h=162" width="244" height="162" /></a><strong>Step 4</strong>. Select Jabberwocky from the Frameworks list. Then enter all the parameters of your component. </font></p>
<p><font size="3">Note: I’ll talk about Web Interface in a separate blog</font></p>
<p> <br />
<hr />
<p><font size="3"><a href="http://oneminutedistraction.files.wordpress.com/2012/11/screen4.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;float:left;border-top:0;border-right:0;padding-top:0;" title="screen4" border="0" alt="screen4" align="left" src="http://oneminutedistraction.files.wordpress.com/2012/11/screen4_thumb.png?w=244&#038;h=162" width="244" height="162" /></a><strong>Step 5</strong>. Click on Properties tab. You can provide a name and a description for your component. You can also customize the category and type of your component. See <a href="http://xmpp.org/registrar/disco-categories.html">this document</a> for some pre defined categories and types.</font></p>
<p><font size="3">You can also add your own properties to be used by your component. </font></p>
<p><font size="3">Click Finish when you are done.</font></p>
<hr /><a href="http://oneminutedistraction.files.wordpress.com/2012/11/screen5.png"><img style="background-image:none;border-bottom:0;border-left:0;padding-left:0;padding-right:0;display:inline;border-top:0;border-right:0;padding-top:0;" title="screen5" border="0" alt="screen5" src="http://oneminutedistraction.files.wordpress.com/2012/11/screen5_thumb.png?w=644&#038;h=425" width="644" height="425" /></a>
<p><font size="3">The plugin will generate a <font face="Courier New">xep-0114.xml</font> file for you and add in all the relevant libraries into your project.</font></p>
<p><font size="3">You can find the latest Vorpal2 bundle with the NetBeans plugin <a href="http://java.net/projects/xmppcontainer/downloads">here</a>. </font></p>
<p><font size="3">I’ve also create a new project call <a href="http://java.net/projects/playground">playground</a> to hold the 2 reference application; customer query and eliza. They have been ported to Vorpal2.</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/247/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=247&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/11/28/netbeans-plugin-for-vorpal2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/tools-belts_thumb.jpg" medium="image">
			<media:title type="html">tools-belts</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/screen0_thumb.png" medium="image">
			<media:title type="html">screen0</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/screen1_thumb.png" medium="image">
			<media:title type="html">screen1</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/screen2_thumb.png" medium="image">
			<media:title type="html">screen2</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/screen3_thumb.png" medium="image">
			<media:title type="html">screen3</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/screen4_thumb.png" medium="image">
			<media:title type="html">screen4</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/11/screen5_thumb.png" medium="image">
			<media:title type="html">screen5</media:title>
		</media:content>
	</item>
		<item>
		<title>New Beginnings Again!</title>
		<link>http://oneminutedistraction.wordpress.com/2012/10/08/new-beginnings-again/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/10/08/new-beginnings-again/#comments</comments>
		<pubDate>Mon, 08 Oct 2012 08:09:31 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[javaee6]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[cdi]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[webpofile]]></category>
		<category><![CDATA[xmpp]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=231</guid>
		<description><![CDATA[&#160; They say third time is a or should be charm! In this blog, I’ll talk about Vorpal2, a reworked version of Vorpal. &#160; Excuses, excuses, excuses The original Vorpal grew out of an experiment and the framework was implemented on top of AbstractComponent class from Tinder. It was a really easy way for someone [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=231&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://oneminutedistraction.files.wordpress.com/2012/10/exploring-calvin-and-hobbes.jpg"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="exploring-calvin-and-hobbes" border="0" alt="exploring-calvin-and-hobbes" src="http://oneminutedistraction.files.wordpress.com/2012/10/exploring-calvin-and-hobbes_thumb.jpg?w=604&#038;h=434" width="604" height="434" /></a></p>
<p>&#160;</p>
<p><font color="#000000" size="2">They say third time is a or should be charm! In this blog, I’ll talk about Vorpal2, a reworked version of Vorpal.</font></p>
<p>&#160;</p>
<h2>Excuses, excuses, excuses</h2>
<p><font color="#000000"><font size="2">The original </font><a href="http://oneminutedistraction.wordpress.com/2011/05/31/vorpal-framework-a-second-try/"><font size="2">Vorpal</font></a><font size="2"> grew out of an experiment and the framework was implemented on top of </font><a href="http://www.igniterealtime.org/builds/tinder/docs/latest/javadoc/org/xmpp/component/AbstractComponent.html"><font size="2" face="Courier New">AbstractComponent</font></a><font size="2"> class from </font><a href="http://www.igniterealtime.org/projects/tinder/index.jsp"><font size="2">Tinder</font></a><font size="2">. It was a really easy way for someone like me with very little knowledge of XMPP to learn about XMPP and get the framework up and running. However due to the way <font face="Courier New">AbstractComponent</font> class works, I had to repeat a same piece of code in different places. There is no single choke point for the XMPP packets coming in and leaving the framework (for that I had to modify <font face="Courier New">AbstractComponent</font> which was not very appealing); this make implementing certain features extremely difficult and in some cases impossible.</font></font></p>
<p><font color="#000000"><font size="2">The second issue was that I made the framework dependent on a custom </font><a href="http://glassfish.java.net/"><font size="2">Glassfish</font></a><font size="2"> container call </font><a href="http://java.net/projects/jabberwocky"><font size="2">Jabberwocky</font></a><font size="2">. Whilst Jabberwocky is great in that it provided a very robust base to run the framework, integrated extremely well with Glassfish <font face="Courier New">asadmin</font> command and provided all sort of statistics on a Vorpal application, the down side was that it effectively ties Vorpal to Glassfish. Also Jabberwocky is build with Maven. So whenever there is a new release of Glassfish I have to figure out which dependencies have change, etc.</font></font></p>
<p><font color="#000000" size="2">Finally testing the framework engine’s becomes difficult because of the 2 dependencies above.</font></p>
<p><font color="#000000"><font size="2">So I’ve decided to reimplement Vorpal; the good news for me is that most of the framework is fine. I just have to rewrite how the Vorpal is loaded. I’ve removed Vorpal’s dependence on <font face="Courier New">AbstractComponent</font> and Jabberwocky; the startup is now done in <font face="Courier New"><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContainerInitializer.html">ServletContainerInitializer</a></font>. Its lightweight and in theory should run on any </font><a href="http://jaxenter.com/introducing-the-java-ee-web-profile-36201.html"><font size="2">JavaEE 6 Web Profile</font></a><font size="2"> compliant container. Glassfish will still be the best supported platform simply because this is where I develop and test Vorpal.</font></font></p>
<p><font color="#000000" size="2">For the rest of this blog, I’ll talk about migrating from Vorpal to Vorpal2, packet filters which is a new feature available only in Vorpal2.</font></p>
<p>&#160;</p>
<h2>The Great Migration</h2>
<h3>No code change (except for ComponentManager)</h3>
<p><font color="#000000" size="2">I’m happy to say that there are no code changes going from Vorpal to Vorpal2 but if your application uses <font face="Courier New"><a href="http://www.igniterealtime.org/builds/tinder/docs/latest/javadoc/org/xmpp/component/ComponentManager.html">ComponentManager</a></font> then you’ll have to rewrite that portion of it.&#160; <font face="Courier New">ComponentManager</font> is an artefact of <font face="Courier New">AbstractComponent</font>. In the past I’ve allowed you to inject <font face="Courier New">ComponentManager</font> into your message handlers. This is no longer true; all the functions in <font face="Courier New">ComponentManager</font> can now be found in <font face="Courier New">ComponentContext</font>.</font></p>
<h3>New JARs </h3>
<p><font color="#000000"><font size="2">The new Vorpal2 bundle can be found </font><a href="http://java.net/projects/xmppcontainer/downloads"><font size="2">here</font></a><font size="2"> (vorpal2). As before, the framework is split into 2 JARs</font></font></p>
<ul>
<li><font color="#000000"><font size="2"><font face="Courier New">vorpal2.jar</font> – the framework </font></font></li>
<li><font size="2"><font color="#000000"><font face="Courier New">jabberwocky_support2.jar</font> – framework supporting files</font> </font></li>
</ul>
<p><font color="#000000" size="2">If you have been using Vorpal, you’ll need to uninstall them. Delete <font face="Courier New">jabberwocky.jar</font> and <font face="Courier New">jabberwocky_support.jar</font> from <font face="Courier New">$GLASSFISH_HOME/glassfish/modules directory</font>.</font></p>
<p><font color="#000000" size="2">As before, <font face="Courier New">vorpal2.jar</font> must be bundled with your application in <font face="Courier New">WEB-INF/lib</font>. </font></p>
<p><font color="#000000" size="2">There are 2 ways you can install <font face="Courier New">jabberwocky_support2.jar</font>. You can either bundle that with your application in <font face="Courier New">WEB-INF/lib</font> or install it at your application’s <font face="Courier New">CLASSPATH</font>. For Glassfish this will be the <font face="Courier New">$GLASSFISH_HOME/glassfish/modules</font> directory.</font></p>
<p><font color="#000000" size="2">At the writing of this blog, I’ve not fully migrated the NetBeans plugin to support Vorpal2 (hence the missing <font face="Courier New">update</font> directory from the download bundle). Hopefully I’ll have the plugins done by the next release. In the mean time, you’ll have to manage the build and package yourself. In NetBeans, add the the JARs from the bundle to your project’s library and remove the previous version of the libraries. Also remember that these JARs, or at the minimum <font face="Courier New">vorpal2.jar,</font> must be in <font face="Courier New">WEB-INF/lib</font> of the final WAR file. </font></p>
<h3>Deployment descriptor</h3>
<p><font color="#000000" size="2">Vorpal uses a deployment file to connect to an XMPP server. The file is called <font face="Courier New">xep-0114.xml</font>. This file is found in <font face="Courier New">WEB-INF</font> directory. If you’re developing Vorpal applications with the NetBeans plugin, the template of this deployment file is in your project directory and its called <font face="Courier New">xep-0114.txmlt</font>. </font></p>
<p><font color="#000000" size="2">The deployment file contains the following pieces of information</font></p>
<ul>
<li><font size="2"><font color="#000000">The XMPP domain, port number to connect to</font> </font></li>
<li><font size="2"><font color="#000000">The subdomain name we wish to use</font> </font></li>
<li><font size="2"><font color="#000000">The class name of the component to start – this is very specific to the frame</font> </font></li>
<li><font size="2"><font color="#000000">An option to specify the shared secret when connecting to the XMPP domain</font> </font></li>
<li><font size="2"><font color="#000000">An optional list of properties to be passed to the application</font> </font></li>
</ul>
<p><font color="#000000" size="2">The file looks like the following</font></p>
<blockquote><p><font color="#000000" size="2" face="Courier New">&lt;subdomain domain=”batcomputer” port=”5275” name=”customer”&gt;        <br />&#160;&#160; &lt;component-class name=”com.kenai.jabberwock.framework.core.JabberwockyComponent”/&gt;         <br />&#160;&#160; &lt;create-subdomain shared-secret=”AlwaysRight” create=”true” cleanup=”true”/&gt;         <br />&#160;&#160; &lt;properties&gt;         <br />&#160;&#160;&#160;&#160;&#160; &lt;property name=”vorpal.component.name” value=”Customer service”/&gt;         <br />&#160;&#160;&#160;&#160;&#160; &lt;property name=”vorpal.component.description” value=”Customer service”/&gt;         <br />&#160;&#160; &lt;/properties&gt;         <br />&lt;/subdomain&gt;</font></p>
</blockquote>
<p><font color="#000000" size="2">What you need to do is the following</font></p>
<ul>
<li><font size="2"><font color="#000000">Change the <font face="Courier New">component-class</font> name from <font face="Courier New">com.kenai.jabberwock.framework.core.Jabberwocky</font> to <font face="Courier New">com.kenai.jabberwocky.framework.core.XMPPComponentServer</font></font> </font></li>
<li><font color="#000000" size="2">Always specify the shared secret with <font face="Courier New">create-domain</font>. The create and cleanup attributes must be present but their values are ignored. </font></li>
</ul>
<p><font color="#000000" size="2">So the new deployment file <font face="Courier New">xep-0114.xml</font> looks like below</font></p>
<blockquote><p><font color="#000000"><font size="2" face="Courier New">&lt;subdomain domain=”batcomputer” port=”5275” name=”customer”&gt;          <br />&#160;&#160; &lt;component-class&#160; name=”<strong><font color="#ff0000">com.kenai.jabberwock.framework.core.XMPPComponentServer</font></strong>”/&gt;           <br />&#160;&#160; <strong><font color="#0000ff">&lt;create-subdomain shared-secret=”AlwaysRight” create=”true” cleanup=”true”/&gt; </font></strong>          <br />&#160;&#160; &lt;properties&gt;           <br />&#160;&#160;&#160;&#160;&#160; &lt;property name=”vorpal.component.name” value=”Customer service”/&gt;           <br />&#160;&#160;&#160;&#160;&#160; &lt;property name=”vorpal.component.description” value=”Customer service”/&gt;           <br />&#160;&#160; &lt;/properties&gt;           <br />&lt;/subdomain&gt;</font></font></p>
</blockquote>
<p><font color="#000000" size="2"></font></p>
<p><font color="#000000" size="2">Recompile your application after you’ve made these changes.</font></p>
<p><font color="#000000"></font></p>
<h2>New Features</h2>
<h3>Packet filtering</h3>
<p><font color="#000000" size="2">Packet filtering is one of the feature that I wanted to implement into Vorpal. But I could not do this easily under the old code base until now.</font></p>
<p><font color="#000000" size="2">Packet filter are filters that are applied to incoming and outgoing XMPP packets. To implement a packet filter, first annotate your class with <font face="Courier New">@PacketFilter</font> and either implement <font face="Courier New">RequestFilter</font> or <font face="Courier New">ResponseFilter</font> (or both!). The following is an example of a request filter that filters out packets that are not send from the local domain</font></p>
<blockquote><p><font size="2"><font color="#000000" face="Courier New"><strong><font color="#ff0000">@PacketFilter </font></strong>          <br />public class LocalEntityOnlyFilter implements <strong><font color="#00a600">RequestFilter</font></strong> {           <br /><strong><font color="#0000ff">&#160;&#160;&#160; @Inject @Named(PredefinedBindings.DOMAIN) String domain; </font></strong>          <br />&#160;&#160;&#160; //Inspired by AbstractComponent.sentByLocalEntity(Packet packet)           <br />&#160;&#160;&#160; @Override           <br /><font color="#00a600">&#160;&#160;&#160; </font><strong><font color="#00cc00"><font color="#00a600">public Packet incoming(final Packet in                <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , ComponentContext context)</font> </font></strong>{           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; final JID jid = in.getFrom();           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (null != jid)           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (in);           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; String pktDomain = jid.getDomain();           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (pktDomain.equals(domain)           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; || pktDomain.endsWith(&quot;.&quot; + domain))           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (in);           <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; return (null);           <br />&#160;&#160;&#160; }           <br />}</font></font> </font></p>
</blockquote>
<p><font color="#000000" size="2">Firstly we see that our filter is annotated with <font face="Courier New">@PacketFilter</font> and implements <font face="Courier New">RequestFilter</font>. So what this mean is that this filter will only monitor incoming packets viz. packets sent to the Vorpal application. </font></p>
<p><font color="#000000" size="2">Filters are managed by CDI. so you can injected values into it. In this case we are injecting the domain name that the subcomponent is connected to. Filters are statesless so you should not use any CDI scopes on it.</font></p>
<p><font size="2"><font color="#000000">In the filter, you can do anything to the packet; you can add stanzas, remove stanzas and change certain values in the packet. </font><font color="#000000">When you return a packet, this can be the original packet from the parameter or a totally new packet, Vorpal will take the packet and proceed with finding an appropriate handler. If you return a null, then Vorpal will discard the packet.</font></font></p>
<p><font color="#000000" size="2">A <font face="Courier New">ResponseFilter</font> behaves the same way as a <font face="Courier New">RequestFilter</font>. When you implement <font face="Courier New">ResponseFilter</font>, you have to override the <font face="Courier New">outgoing(Packet, ComponentContext)</font> method which returns a <font face="Courier New">Packet</font> as well. If you return a null, then Vorpal will not send the packet.</font></p>
<p><font color="#000000" size="2">If you have more that one request or response filter, Vorpal does not impose any ordering on them. Also if one of the filter rejects a packet, then that packet will not be processed by other filters. For example if you have 3 request filters; the first of these 3 filters rejects the packet, then that packet will not be passed to the other 2 to be processed. </font></p>
<h3><font color="#000000">Ordering sent packets</font></h3>
<p><font color="#000000" size="2">One side effect of this new version is you can now impose some order on how the packets are send out by returning a List in your message handler. The following code snippet illustrates this</font></p>
<blockquote><p><font color="#000000" size="2" face="Courier New">@Body(“{body}”)        <br />private List&lt;ResponseContext&gt; handle(@Named(“body”) String body) {         <br />&#160;&#160; …         <br />}</font></p>
</blockquote>
<p><font color="#000000"><font size="2"><font face="Courier New">handle()</font> returns a <font face="Courier New">List</font>; so Vorpal will send out the packet according to the order of the packets in the list.</font></font></p>
<p><font color="#000000"><font size="2">Let me know what you think of Vorpal2. If you have any issues, please post them to the </font><a href="http://java.net/projects/vorpal/forums"><font size="2">forum</font></a><font size="2">. The new bundle can be found </font><a href="http://java.net/projects/xmppcontainer/downloads"><font size="2">here</font></a><font size="2">.</font></font></p>
<p><font size="2"></font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/231/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/231/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=231&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/10/08/new-beginnings-again/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/10/exploring-calvin-and-hobbes_thumb.jpg" medium="image">
			<media:title type="html">exploring-calvin-and-hobbes</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Server Sent Events with Vorpal</title>
		<link>http://oneminutedistraction.wordpress.com/2012/09/12/using-server-sent-events-with-vorpal/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/09/12/using-server-sent-events-with-vorpal/#comments</comments>
		<pubDate>Wed, 12 Sep 2012 09:32:57 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SSE]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[cdi]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[sse]]></category>
		<category><![CDATA[vorpal]]></category>
		<category><![CDATA[xmpp]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=227</guid>
		<description><![CDATA[I came across the following 2 blogs (here and here) on a CDI framework for server sent event (SSE) several months ago. I downloaded the source then tried it with Vorpal running on Glassfish 3.1.x. The result was a really long exception stack. 4 months later. After publishing part 4 of on conversations, I decided [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=227&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><font size="2"><img style="background-image:none;padding-left:0;padding-right:0;display:inline;padding-top:0;border-width:0;" title="bridge" border="0" alt="bridge" src="http://oneminutedistraction.files.wordpress.com/2012/09/bridge.jpg?w=644&#038;h=208" width="644" height="208" /></font></p>
<p><font size="2">I came across the following 2 blogs (</font><a href="http://weblogs.java.net/blog/bhaktimehta/archive/2012/04/21/server-sent-events-sample-glassfish?force=176"><font size="2">here</font></a><font size="2"> and </font><a href="http://weblogs.java.net/blog/jitu/archive/2012/04/20/server-sent-events-sse-glassfish"><font size="2">here</font></a><font size="2">) on a CDI framework for </font><a href="http://www.w3schools.com/html5/html5_serversentevents.asp"><font size="2">server sent event</font></a><font size="2"> (SSE) several months ago. I downloaded the source then tried it with Vorpal running on <a href="https://glassfish.java.net">Glassfish</a> 3.1.x. The result was a really long exception stack. </font></p>
<p><font size="2">4 months later. </font></p>
<p><font size="2">After publishing </font><a href="http://oneminutedistraction.wordpress.com/2012/09/11/art-of-conversation-miscellaneous-topics-part-4/"><font size="2">part 4</font></a><font size="2"> of on conversations, I decided to see if I have any luck with the SSE framework. I wasn’t really expecting it to work. Well it did. What is even more surprising is that it worked with Vorpal.&#160; </font></p>
<p><font size="2">What I’m going to do in this blog is explain how the SSE framework works and more importantly how you can use it with Vorpal. Its really easy.</font></p>
<p><font size="2"></font></p>
<h3>Setup the push event stream</h3>
<p><font size="2">Firstly create a notification handler. The notification handler will maintain the connection between the browser and the web application. The following is a simple notification handler</font></p>
<blockquote><p><font color="#00cc00" size="2" face="Courier New"><strong>@ServerSentEvents(“/notifications”)</strong></font></p>
<p><font size="2" face="Courier New">public class NotificationHandler <strong><font color="#ff0000">extends ServerSentEventHandler</font></strong> {</font></p>
<p><font size="2" face="Courier New">&#160;&#160; public void sendMessage(String msg) {</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; try {</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong><font color="#ff0000">connection</font></strong>.sendMessage(msg);</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; } catch (IOException ex) {</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; }</font></p>
<p><font size="2" face="Courier New">&#160;&#160; }        <br /></font></p>
</blockquote>
<p><font size="2">A few points to note: the notification handler must extend <font face="Courier New">ServerSentEventHandler</font>; this class is from the framework. You also have to annotate the class with <font face="Courier New">@ServerSentEvents</font> and specify the URL of the event stream. In this case it is <font face="Courier New">notifications</font>. More on this later.</font></p>
<p><font size="2">Now you need to write a HTML/JSP/JSF/whatever page to invoke this event stream. Here is a snippet of the HTML page along with a snippet of Javascript that loads loads the event stream</font></p>
<blockquote><p><font size="2" face="Courier New">&lt;html xmlns=&quot;</font><a href="http://www.w3.org/1999/xhtml&quot;"><font size="2" face="Courier New">http://www.w3.org/1999/xhtml&quot;</font></a>       <br /><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; xmlns:h=&quot;</font><a href="http://java.sun.com/jsf/html&quot;"><font size="2" face="Courier New">http://java.sun.com/jsf/html&quot;</font></a><font size="2" face="Courier New">&gt;        <br />&#160;&#160;&#160; &lt;h:head&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;title&gt;XMPP with SSE&lt;/title&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;script src=&quot;resources/app.js&quot;&gt;&lt;/script&gt;         <br />&#160;&#160;&#160; &lt;/h:head&gt;         <br />&#160;&#160;&#160; &lt;h:body&gt;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;h1&gt;Using Vorpal with SSE&lt;/h1&gt;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160; &lt;/h:body&gt;         <br />&lt;/html&gt;</font></p>
</blockquote>
<p><font size="2">The Javascript that creates the event stream is in <font face="Courier New">app.js</font>. Here is the code</font></p>
<blockquote><p><font size="2" face="Courier New">var url = ‘http://’ + document.location.host </font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; + <strong><font color="#0000ff">“/xmppsse</font><font color="#00cc00">/notifications</font></strong>”;</font></p>
<p><font size="2" face="Courier New">eventSource = new EventSource(url);</font></p>
<p><font size="2" face="Courier New">eventSource.onmessage = {</font></p>
<p><font size="2" face="Courier New">&#160;&#160; document.body.innerHTML += msg + ’&lt;br&gt;’;</font></p>
<p><font size="2" face="Courier New">};</font></p>
</blockquote>
<p><font size="2"></font></p>
<p><font size="2">The important thing here is to get the event stream URL right. If our example, this is made up of the application context of our web application (in <font color="#0000ff">blue</font>) and the value that you’ve specified in <font color="#000000" face="Courier New">@ServerSentEvent</font> annotation (in <font color="#00cc00">green</font>). </font></p>
<p>&#160;</p>
<h3>Pushing data to the browser</h3>
<p><font size="2">We now hook the event handler to a Vorpal application. I did a screencast on developing a customer query service which allows you to use a Jabber client to query a database, which you can find </font><a href="http://www.youtube.com/watch?v=7MT0eaX9BQQ&amp;feature=plcp"><font size="2">here</font></a><font size="2"> and the source </font><a href="http://java.net/projects/xmppsupport/sources/source-code-repository/show/playground/customer_query/CustomerQuery"><font size="2">here</font></a><font size="2">. </font></p>
<p><font size="2">Lets extend this so that whenever someone performs a query, the result is also pushed to the browser using SSE. </font></p>
<blockquote><p><font size="2" face="Courier New">@Message</font></p>
<p><font size="2" face="Courier New">public class MessageHandler {</font></p>
<p><font size="2" face="Courier New">&#160;&#160; @EJB CustomerManager mgr;</font></p>
<p><font size="2" face="Courier New">&#160;&#160; <strong><font color="#ff0000">@Inject @ServerSentEventContext(“/notifications”)</font></strong></font></p>
<p><font color="#ff0000" size="2" face="Courier New"><strong>&#160;&#160;&#160;&#160;&#160;&#160;&#160; ServerSentEventHandlerContext&lt;NotificationHandler&gt; ctx<font color="#000000">;</font></strong></font></p>
<p><font size="2" face="Courier New">&#160;&#160; @Body(“{id}”)</font></p>
<p><font size="2" face="Courier New">&#160;&#160; private String handle(@Named(“id”) int id) {</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; //Assume we find every record and no NPE</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; String record = mgr.find(id).toString();</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; //Loop through all handlers </font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; for (NotificationHandler nh: <strong><font color="#0000ff">ctx.getHandlers()</font></strong>)</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; try {</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong><font color="#0000ff">nh.sendMessage(record)</font></strong>;</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; } catch (Exception ex) {</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nh.close();</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }</font></p>
<p><font size="2" face="Courier New">&#160;&#160;&#160;&#160;&#160; return (record);</font></p>
<p><font size="2" face="Courier New">&#160;&#160; }</font></p>
</blockquote>
<p><font size="2">After we got the record from <font face="Courier New">CustomerManager</font>, which uses JPA, we loop through all the registered handlers for an event stream channel and pushes the record to them. You have to get the correct handler to push to. </font></p>
<p><font size="2">In our example, we are pushing to all browser registered on <font face="Courier New">notifications</font>. So we inject an instance of <font face="Courier New">ServerSentEventHandlerContext</font> with qualifying it with <font face="Courier New">@ServerSentEventContext(“notifications”)</font>.</font></p>
<h3>Example code</h3>
<p><font size="2">You can download the example source from <a href="http://java.net/projects/xmppcontainer/downloads/directory/miscellaneous">here</a>. I’ve bundled the SSE library into the download which is a <a href="http://www.netbeans.org/">NetBeans</a> project.</font></p>
<p><font size="2">Use the following steps to setup, deploy and run the example</font></p>
<ol>
<li><font size="2">Configure an external component on your Jabber server. The example uses <font face="Courier New">customer</font> for subdomain and <font face="Courier New">AlwaysRight</font> for shared secret (Caps!). You can always change these settings. I use <a href="http://www.google.com.sg/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;ved=0CCAQFjAA&amp;url=http%3A%2F%2Fwww.igniterealtime.org%2Fprojects%2Fopenfire%2F&amp;ei=Q09QUOS7KoHzrQeRy4GADw&amp;usg=AFQjCNHAPRZMbe407F_gz_GjkX9L2m6ddw&amp;sig2=aNqSxV3gMSNK3EcbuNYsRw">Openfire</a> for the Jabber server</font></li>
<li><font size="2">Make sure that you’ve installed your Glassfish with Jabberwocky container. </font></li>
<li><font size="2">Deploy the project to Glassfish either from NetBeans or using <font face="Courier New">asadmin</font> command. It’s probably easier if you deploy if from NetBeans</font></li>
<li><font size="2">Open your browser and point to <font face="Courier New"><a href="http://yourserver:port/xmppsse" rel="nofollow">http://yourserver:port/xmppsse</a></font><font face="Arial">. I’ve tested with Firefox and Chrome, latest versions.</font></font></li>
<li><font size="2">Open your Jabber client, open a chat with <font face="Courier New">query@customer.your_jabber_server</font>. Send 1</font></li>
<li><font size="2">You should now see your reply display in your Jabber client and in your browser.</font></li>
</ol>
<p><font size="2">The source code for the SSE can be found <a href="http://java.net/projects/glassfish/sources/svn/show/trunk/main/appserver/web/web-sse">here</a> if you wish to look at it.</font></p>
<p><font size="2">Part of the code (the <font face="Courier New">app.js</font>) is taken from Bhakti Mehta’s <a href="http://weblogs.java.net/blog/bhaktimehta/archive/2012/04/21/server-sent-events-sample-glassfish?force=176">blog</a>. Big thanks.</font></p>
<p><font size="2">Till next time.</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/227/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/227/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=227&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/09/12/using-server-sent-events-with-vorpal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/09/bridge.jpg" medium="image">
			<media:title type="html">bridge</media:title>
		</media:content>
	</item>
		<item>
		<title>Art of Conversation: Miscellaneous Topics &#8211; Part 4</title>
		<link>http://oneminutedistraction.wordpress.com/2012/09/11/art-of-conversation-miscellaneous-topics-part-4/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/09/11/art-of-conversation-miscellaneous-topics-part-4/#comments</comments>
		<pubDate>Tue, 11 Sep 2012 07:41:46 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[vorpal xmpp java conversation]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=224</guid>
		<description><![CDATA[Programmatically looking up Beans As you may know, you can use Vorpal client to create client side application. I talked about using client side Vorpal here. For the most part, Vorpal client has the same programming model as its server counterpart. Vorpal relies heavily on CDI for resources; this is not a problem if you&#8217;re [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=224&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h3>Programmatically looking up Beans</h3>
<p><font size="3">As you may know, you can use Vorpal client to create client side application. I talked about using client side Vorpal <a href="http://oneminutedistraction.wordpress.com/2012/05/01/yet-another-xmpp-client-library/">here</a>. For the most part, Vorpal client has the same programming model as its server counterpart.</font></p>
<p><font size="3">Vorpal relies heavily on CDI for resources; this is not a problem if you&#8217;re on the server side, but on the client side you have to lookup the CDI instances yourself. The trick is this is to get a reference to either <font face="Courier New"><a href="http://docs.oracle.com/javaee/6/api/javax/enterprise/inject/spi/BeanManager.html">BeanManager</a></font> or <font face="Courier New"><a href="http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#weld-se">WeldContainer</a></font>.</font></p>
<p><font size="3">Which of those 2 objects you get depends largely on how you bootstrap CDI in the Java SE environment. See <a href="http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#weld-se">this</a> for the various ways. Since <font face="Courier New">BeanManager</font> is standard Java EE API, we’ll use <font face="Courier New">BeanManager</font>.</font></p>
<p><font size="3">One way that I&#8217;ve found that works is to get CDI to inject an instance of <font face="Courier New">BeanManager</font> by listening to the container starting up event. Then you can cache a reference of <font face="Courier New">BeanManager</font> in a singleton. The following shows how we go about doing this</font></p>
<blockquote><p><font size="3" face="Courier New">@ApplicationScoped</font></p>
<p><font size="3" face="Courier New">public class MySingletons {</font></p>
<p><font color="#ff0000" size="3" face="Courier New"><strong>&#160;&#160; @Inject private BeanManager bm;</strong></font></p>
<p><font color="#ff0000" size="3" face="Courier New"><strong>&#160;&#160; @Inject ComponentContext ctx;</strong></font></p>
<p><font size="3" face="Courier New">&#160;&#160; </font></p>
<p><font size="3" face="Courier New">&#160;&#160; private static MySingletons instance = null;</font></p>
<p><font size="3" face="Courier New">&#160;&#160; </font></p>
<p><font size="3" face="Courier New">&#160;&#160; @PostConstruct </font><font size="3" face="Courier New">private void init() {</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; instance = this;</font></p>
<p><font size="3" face="Courier New">&#160;&#160; }</font></p>
<p><font size="3" face="Courier New">&#160;&#160; private void containerInitialized(</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @ObservesContainerInitialized ciEvt) { }</font></p>
<p><font size="3" face="Courier New"></font></p>
<p><font size="3" face="Courier New">&#160;&#160; public BeanManager getBeanManager() {</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; return (bm);</font></p>
<p><font size="3" face="Courier New">&#160;&#160; }</font></p>
<p><font size="3" face="Courier New">&#160;&#160; public ComponentContext getComponentContext() {</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; return (ctx);</font></p>
<p><font size="3" face="Courier New">&#160;&#160; }</font></p>
<p><font size="3" face="Courier New">&#160;&#160; public static MySingletons getInstance() {</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; return (instance);</font></p>
<p><font size="3" face="Courier New">&#160;&#160; }</font></p>
<p><font size="3" face="Courier New">}</font></p>
</blockquote>
<p><font size="3">During container startup we use <font face="Courier New">MySingletons</font> to listen to the container initializing and use this opportunity to hold a reference to <font face="Courier New">BeanManager</font>. Same with <font face="Courier New">ComponentContext</font>. Now wherever you need to access either <font face="Courier New">BeanManager</font> or <font face="Courier New">ComponentContext</font>, you can use the static method <font face="Courier New">MySingletons.getInstance()</font> .</font></p>
<p><font size="3">Note: Vorpal client is suppose to be CDI implementation agnostic, but I&#8217;ve only ever tested it with Weld.</font></p>
<p><font size="3"></font></p>
<h3>Client side conversation setup</h3>
<p><font size="3">When you&#8217;re working with conversation on the client side, you have make sure that you are not working with conversation artifacts from a previous processing cycle.</font></p>
<p><font size="3">On the server, before Vorpal calls your message handler, it initializes the component context and populate it with the appropriate bindings and meta data that your message handler will need to process a message correctly. Server side is mostly reactive processing to incoming messages and Vorpal uses this behaviour to perform a lot of mundane task under the hood.</font></p>
<p><font size="3">However on the client side, message processing is mostly proactive viz. you click on a button and your application uses Vorpal client to send out a message. Since there is no initializing the component context; you may actually be picking up artifacts from the previous processing cycle. What you have to do is to clear the component context before you perform your processing by calling <font face="Courier New">ComponentContext.startProcessingCycle()</font>.</font></p>
<p><font size="3">The following code shows you how you clear the component context on every button click</font></p>
<blockquote><p><font size="3" face="Courier New">public void actionPerformed(ActionEvent aEvt) {</font></p>
<p><font size="3" face="Courier New">&#160;&#160; String cmd = aEvt.getActionCommand();</font></p>
<p><font size="3" face="Courier New">&#160;&#160; switch (cmd) {</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; case “New Conversation”:</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ComponentContext compCtx = </font><font size="3" face="Courier New">MySingletons</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .getInstance()</font><font size="3" face="Courier New">.getComponentContext();</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; compCtx.startProcessingCycle();</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Conversation conv = lookup(</font><font size="3" face="Courier New">Conversation.class</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; , new NamedQualifier(“__conversation__”));</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConversationContext convCtx = lookup(</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ConversationContext.class);</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; conv.begin();</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ResponseContext respCtx = </font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; new ResponseContext(&#8230;);</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font size="3" face="Courier New">&#8230;</font></font><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; compCtx.send(respCtx);</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; break;</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; &#8230;</font></p>
<p><font size="3" face="Courier New">&#160;&#160; }</font></p>
<p><font size="3" face="Courier New">}</font></p>
<p><font size="3" face="Courier New">private &lt;T&gt; T lookup(Class&lt;T&gt; type</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; , Annotation&#8230; qualifiers) {</font></p>
<p><font size="3" face="Courier New">&#160;&#160; T instance = null;</font></p>
<p><font size="3" face="Courier New">&#160;&#160; BeanManager bm = MySingletons.getInstance();</font></p>
<p><font size="3" face="Courier New">&#160;&#160; Bean&lt;T&gt; bean = (Bean&lt;T&gt;)bm.resolve(</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; bm.getBeans(type, qualifiers));</font></p>
<p><font size="3" face="Courier New">&#160;&#160; if (null != bean) {</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; CreationalContext&lt;T&gt; ctx =</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; bm.createCreationalContext(bean);</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160; instance = bean.create(ctx);</font></p>
<p><font size="3" face="Courier New">&#160;&#160; }</font></p>
<p><font size="3" face="Courier New">&#160;&#160; return (instance);</font></p>
<p><font size="3" face="Courier New">}</font></p>
</blockquote>
<p><font size="3">The above shows how you can lookup an instance of <font face="Courier New">Conversation</font> and <font face="Courier New">ConversationContext</font> programmatically. I&#8217;ll leave the <font face="Courier New">NamedQualifier</font> class as an exercise for the reader. Hint: use <font face="Courier New"><a href="http://docs.oracle.com/javaee/6/api/javax/enterprise/util/AnnotationLiteral.html">AnnotationLiteral</a></font></font></p>
<p><font size="3">If you&#8217;re just interested in just creating a new conversation but not about associating any states with it then you can manually add a <font face="Courier New">ThreadID</font> to <font face="Courier New">ResponseContext</font> like so:</font></p>
<blockquote><p><font size="3" face="Courier New">case “ New Conversation”:</font></p>
<p><font size="3" face="Courier New">&#160;&#160; ComponentContext compCtx = </font><font size="3" face="Courier New">MySingletons</font></p>
<p><font size="3" face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; .getInstance()</font><font size="3" face="Courier New">.getComponentContext();</font></p>
<p><font size="3" face="Courier New">&#160;&#160; compCtx.startProcessingCycle();</font></p>
<p><font size="3" face="Courier New">&#160;&#160; ResponseContext respCtx = </font><font size="3" face="Courier New">new ResponseContext(&#8230;);</font></p>
<p><font size="3" face="Courier New">&#160;&#160; respCtx.add(ThreadID.generate());</font></p>
<p><font face="Courier New">&#160;&#160;&#160;&#160;&#160;&#160; &#8230;</font></p>
<p><font size="3" face="Courier New">&#160;&#160; compCtx.send(respCtx);</font></p>
<p><font size="3" face="Courier New">&#160;&#160; break;</font></p>
</blockquote>
<p><font size="3">which is a lot simpler. I&#8217;ve <a href="http://oneminutedistraction.wordpress.com/2012/09/07/art-of-conversation-explicit-conversation-part-3/">blog</a> about this in my previously. </font></p>
<p><font size="3">Warning: you should not use <font face="Courier New">ComponentContext.startProcessingCycle()</font> when you are developing server side components or bad things will happen.</font></p>
<p><font size="3"></font></p>
<h3>Turning off conversation inheritance</h3>
<p><font size="3">Vorpal allows conversation states to be inherited viz. if you create a new conversation in the context of an existing conversation, then that new conversation will inherit all the states from the existing <font face="Courier New">ConversationContext</font>.</font></p>
<p><font size="3">If you want to suppress the new conversation from inheriting the states, add a property call <font face="Courier New">com.kenai.jabberwocky.framework.property.inheritConversationContext</font> </font><font size="3">to the existing conversation&#8217;s <font face="Courier New">ComponentContext</font> and set the value to <font face="Courier New">false</font>. This will prevent new conversation from inheriting the existing conversation’s state.</font></p>
<blockquote><p><font size="3" face="Courier New">//ConversationContext from an existing conversation</font></p>
<p><font color="#0000ff" size="3" face="Courier New"><strong>convCtx.setAttribute(</strong></font></p>
<p><font color="#0000ff" size="3" face="Courier New"><strong>&#160;&#160;&#160;&#160;&#160; FrameworkProperty.INHERIT_CONVERSATION_CONTEXT</strong></font></p>
<p><font color="#0000ff" size="3" face="Courier New"><strong>&#160;&#160;&#160;&#160;&#160; , false);</strong></font></p>
<p><font size="3" face="Courier New">//Outgoing packet will not inherit conversation states</font></p>
<p><font size="3" face="Courier New">ResponseContext respCtx = new ResponseContext(&#8230;);</font></p>
<p><font size="3" face="Courier New">respCtx.add(ThreadID.generate());</font></p>
<p><font size="3" face="Courier New">&#160;&#160; &#8230;</font></p>
<p><font size="3" face="Courier New">compCtx.send(respCtx);</font></p>
</blockquote>
<p><font size="3">The <font face="Courier New">com.kenai.jabberwocky.framework.property.inheritConversationContext</font></font> <font size="3"> is not a permanent attribute as it is cleared after every processing cycle. So you will have to set it again on the next processing cycle if you wish to further suppress conversation inheritance.</font></p>
<p><font size="3">The September 2012 bundles supports the latest conversation features. You can download them <a href="http://java.net/projects/xmppcontainer/downloads">here</a>. I’ve just uploaded a new bundle with bug fixes.</font></p>
<p><font size="3">Till next time.</font></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/224/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/224/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=224&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/09/11/art-of-conversation-miscellaneous-topics-part-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>
	</item>
		<item>
		<title>Art of Conversation: Explicit Conversation &#8211; Part 3</title>
		<link>http://oneminutedistraction.wordpress.com/2012/09/07/art-of-conversation-explicit-conversation-part-3/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/09/07/art-of-conversation-explicit-conversation-part-3/#comments</comments>
		<pubDate>Fri, 07 Sep 2012 03:40:59 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[xmpp conversation vorpal jabberwocky stateful]]></category>

		<guid isPermaLink="false">https://oneminutedistraction.wordpress.com/?p=221</guid>
		<description><![CDATA[In my previous blog entry, I talked about implicit conversations. A quick recap: implicit conversations are conversations that are created for you by the Vorpal framework whenever you send or receive an IQ get or set. These conversation are terminated when you send or receive an IQ result or error. Conversation gives you the ability [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=221&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In my <a target="_blank" href="http://oneminutedistraction.wordpress.com/2012/08/14/art-of-conversation-implicit-conversations-part-2/">previous blog entry</a>, I talked about implicit conversations. A quick recap: implicit conversations are conversations that are created for you by the Vorpal framework whenever you send or receive an IQ get or set. These conversation are terminated when you send or receive an IQ result or error. </p>
<p>Conversation gives you the ability to store states either in a <font face="monospace">ConversationContext</font> object or in the message handler class annotated with <font face="monospace">@ConversationScoped</font>. For example, you can associate some state with an outgoing IQ get packet; so when the reply returns, you can look at the state know what to do with it. When Vorpal creates another implicit conversation in the context of an ongoing conversation, the newly created conversation will inherit the states from the ongoing conversation. This is known as conversation inheritance.</p>
<p>In this blog, we will introduce explicit conversation, viz conversation that are directly under the control of the application; explicit conversation share lots of similar properties as implicit conversations but build on these concepts.<br />
<h3>What are explicit conversations?</h3>
<p>In XMPP, conversation between 2 Jabber entities are marked with <font face="monospace">&lt;thread&gt;</font>; see this <a target="_blank" href="http://xmpp.org/extensions/xep-0201.html">document</a>. You will typically find <font face="monospace">&lt;thread&gt;</font> element in message. This allows both parties to track the packets between the 2 conversing entities. Below is an example of a pair of message exchange between a patient and his psychiatrist.<br />
<blockquote><font face="monospace">&lt;message from=&#8221;normanb@arkham&#8221; to=&#8221;eliza@arkham&#8221; type=&#8221;chat&#8221;&gt;<br />&lt;body&gt;I want my mother&lt;/body&gt;<br />&lt;thread&gt;1234567890&lt;/thread&gt;<br />&lt;/message&gt;</p>
<p>&lt;message from=&#8221;eliza@arkham&#8221; to=&#8221;normanb@arkham&#8221; type=&#8221;chat&#8221;&gt;<br />&lt;body&gt;Why do you need your mother?&lt;/body&gt;<br />&lt;thread&gt;1234567890&lt;/thread&gt;<br />&lt;/message&gt;</font></p></blockquote>
<p>To see how to use explicit conversation, lets write s service, call Eliza (which is a <a target="_blank" href="http://www.chayden.net/eliza/Eliza.html">bot</a>), that provides psychiatric help.<br />
<blockquote><font face="monospace"><font color="#CC0000"><b>@ConversationScoped</b></font><br />@Message<br />@To(&#8220;eliza@{__subdomain__}&#8221;)<br />public class ElizaService {<br /><font color="#3333FF"><b>&nbsp;&nbsp; @Inject @Named(&#8220;__conversation__&#8221;) Conversation conv;</b></font><br />&nbsp;&nbsp; private Eliza eliza = null;</p>
<p>&nbsp;&nbsp; @PostConstruct private void init() {<br /><font color="#3333FF"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conv.begin();</b></font><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Initialize Eliza<br />&nbsp;&nbsp; </font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>eliza = new Eliza();<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font></font><font face="monospace">&#8230;<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>}</p>
<p></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>@Body(&#8220;{body}&#8221;)<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>private String handleMessage(@Named(&#8220;body&#8221;) String body) {<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">if (&#8220;bye&#8221;.equals(body))<br /></font><font color="#3333FF"><b><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font></font></b></font><font face="monospace"><font color="#3333FF"><b>conv.end();</b></font><br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">return (eliza.process(body));<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>}<br />}</font></p></blockquote>
<p>Assume we receive a new message with a thread id of <font face="monospace">1234567890</font>; also assume that this is the first time that Eliza service is seeing this new thread id. </p>
<p>So what we want to do is associate an instance of <font face="monospace">Eliza</font> object with this thread. We will continue to use the same Eliza instance for messages with the same thread id until the sender decides to terminate the conversation.</p>
<p>To do that we annotate the message hander with <font face="monospace">@com.kenai.jabberwocky.framework.ConversationScoped</font>. Do note that the <font face="monospace">@ConversationScoped</font> is from <font face="monospace">com.kenai.jabberwocky.framework</font> package and not from <font face="monospace">javax.enterprise.context</font>. Any class that is annotated with this will be associated with the same conversation for the life time of that conversation.</p>
<p>We also inject an instance of <a target="_blank" href="http://docs.oracle.com/javaee/6/api/javax/enterprise/context/Conversation.html"><font face="monospace">Conversation</font></a> object into the handler. The <font face="monospace">Conversation</font> object is use to start and terminate a conversation. For explicit conversation, if you do not explicitly start them, the conversation will not begin. It is also important that we qualify the conversation with <font face="monospace">@Named(&#8220;__conversation__&#8221;)</font> or else the appserver will try to inject a <font face="monospace">Conversation</font> instance from JSF instead of from Vorpal.</p>
<p>This is how the code works: after the handler has been instantiated; the <a target="_blank" href="http://docs.oracle.com/javaee/6/api/javax/annotation/PostConstruct.html"><font face="monospace">@PostConstruct</font></a> method will start the conversation by calling <font face="monospace">conv.begin()</font>. Since <font face="monospace">ElizaService</font> class is annotated with <font face="monospace">@ConversationScoped</font>, this particular instance will be associated with this thread id for the life time of the conversation. </p>
<p>After invoking <font face="monospace">@PostConstruct</font>, normal method processing occurs viz. <font face="monospace">handleMessage()</font> will be called. When we receive an &#8216;<font face="monospace">bye</font>&#8216; in the message body, we terminate the conversation by calling <font face="monospace">conv.end()</font>. After that the <font face="monospace">ElizaService</font> instance will be discarded. A new instance will be created even when the next message contains the same thread id.</p>
<p>Once you have started a conversation, any handlers annotated with <font face="monospace">@ConversationScoped</font> that process messages from this open conversation will also be automatically associated with the conversation. This is known as a <u>conversation group</u>. <br />
<blockquote><font face="monospace">@ConversationScoped<br />@Message<br />@To(&#8220;eliza@{__subdomain__}&#8221;)<br />@DataForm<br />&nbsp;&nbsp; public class Configure {</font></p></blockquote>
<p>For example if the following is the one of the handler that is invoked after the patient has started <font face="monospace">normanb@arkham</font> has started the conversation, then <font face="monospace">Configure</font> object will be associated with the conversation. When the conversation terminates, all objects in the conversation group will be released.<br />
<h3>Using ConversationContext</h3>
<p>The above can be rewritten using <font face="monospace">ConversationContext</font>. A <font face="monospace">CovnersationContext</font> is a map and behaves like <a target="_blank" href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSession.html"><font face="monospace">HttpSession</font></a>. You can also use this to store the conversation state. The following shows <font face="monospace">ElizaService</font> rewritten using <font face="monospace">ConversationContext</font><br />
<blockquote><font face="monospace">@Message<br />@To(&#8220;eliza@{__subdomain__}&#8221;)<br />public class ElizaService {<br />&nbsp;&nbsp; @Inject @Named(&#8220;__conversation__&#8221;) Conversation conv;<br /></font><font face="monospace"><font face="monospace" color="#CC0000">&nbsp;&nbsp; </font><font color="#CC0000"><b>@Inject ConversationContext convCtx;</b></font></p>
<p></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>@PostConstruct private void init() {<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">conv.begin();<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">Eliza eliza = new Eliza();<br /></font><font color="#CC0000"><b><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font></b></font><font face="monospace"><font color="#CC0000"><b>convCtx.setAttribute(“elizaInstance”, eliza);</b></font><br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>}</p>
<p>@Body(&#8220;{body}&#8221;)<br />private String handleMessage(@Named(&#8220;body&#8221;) String body) {<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font><b><font color="#CC0000">Eliza eliza = (Eliza)convCtx.getAttribute(“elizaInstance”);</font></b><br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>if (&#8220;bye&#8221;.equals(body))<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">conv.end();<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>return (eliza.process(body));<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>}<br />}</font></p></blockquote>
<p>You can use a combination of <font face="monospace">@ConversationScoped</font> objects and <font face="monospace">ConversationContext</font> to hold your conversation state. </p>
<p>How do you decide which to use? If you have a really complex object then <font face="monospace">@ConversationScoped</font> objects are the way to go; but if you decide to share conversation states with all handlers (including those that are not annotated with <font face="monospace">@ConversationScoped</font>) then you should consider <font face="monospace">ConversationContext</font>.<br />
<h3>Creating new conversation threads</h3>
<p>Vorpal does not support nested conversations; furthermore a message handler can only deal with 1 conversation at a time. Let say we have the following use case; in the course of <font face="monospace">normanb@arkham</font>&#8216;s conversation with Eliza, Eliza would like to start a new conversation thread on a new topic with <font face="monospace">normanb</font>. In other words, <font face="monospace">normanb</font> is have 2 open conversation with Eliza (2 unique thread id). To do this, you need to manually insert a new thread id into an outgoing message packet. The following code shows how this is done<br />
<blockquote><font face="monospace">@Message<br />@To(&#8220;eliza@{__subdomain__}&#8221;)<br />public class ElizaService {<br />&nbsp;&nbsp; @Inject @Named(&#8220;__conversation__&#8221;) Conversation conv;<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>@Inject ConversationContext convCtx;</p>
<p></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>@PostConstruct private void init() {<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">conv.begin();<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">Eliza eliza = new Eliza();<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">convCtx.setAttribute(“elizaInstance”, eliza);<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>}</p>
<p></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>@Body(&#8220;{body}&#8221;)<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>private ResponseContext handleMessage(@Named(&#8220;body&#8221;) String body) {<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">Eliza eliza = (Eliza)convCtx.getAttribute(“elizaInstance”);<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">ResponseContext reply = new ResponseContext(<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">ResponseContext.Type.Message);</p>
<p></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">if (&#8220;bye&#8221;.equals(body))<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font></font><font face="monospace">conv.end();<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">else if (body.toLowerCase().startsWith(“new topic”)) {<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font></font><font face="monospace">body = body.substring(9).trim();</font><br /><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font><font color="#009900"><b><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>//Insert a new ThreadID into the response<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font></font></b></font><font face="monospace"><font color="#009900"><b>responseContext.add(ThreadID.generate());</b></font><br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; }</font></font></font><font face="monospace"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">responseContext.add(eliza.process(body));<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; <font face="monospace">&nbsp;&nbsp; </font></font></font><font face="monospace">return (responseContext);<br /></font><font face="monospace"><font face="monospace">&nbsp;&nbsp; </font>}<br />}</font></p></blockquote>
<p>Assume that when the response from <font face="monospace">normanb</font> starts with the phrase &#8216;<font face="monospace">new topic</font>&#8216; then we want to start a new conversation thread. Under normal circumstances, if there are no open conversation, then what we do is inject an instance of <font face="monospace">Conversation</font> object in, call <font face="monospace">begin()</font>. The out going message will then be marked with a randomly generated thread id. </p>
<p>However in the situation above, we already are in the context of an open conversation; invoking <font face="monospace">begin()</font> again will result in an <font face="monospace">IllegalStateException</font>. In this situation, to let Vorpal know that the out going message is under a new conversation, you have to manually add the <font face="monospace">&lt;thread&gt;</font> element into the message packet. You can either manipulate the <a target="_blank" href="http://www.igniterealtime.org/builds/tinder/docs/latest/javadoc/org/xmpp/packet/Message.html"><font face="monospace">Message</font></a> packet or use <font face="monospace">ResponseContext</font> as show above to add a thread id. </p>
<p>When Vorpal sends out the message, it&#8217;ll create a new a <font face="monospace">Conversation</font> and a <font face="monospace">ConversationContext</font> objects for this new conversation; it then<br /><u>automatically</u> starts the conversation for you viz. calls <font face="monospace">Conversation.begin()</font>. This is the only time an explicit conversation behaves like an implicit conversation. You&#8217;ll still need to manually terminate it. The <font face="monospace">ConversationContext</font> will inherit all the values from the existing open conversation. </p>
<p>This new way of creating conversation works in all handlers including IQ and Presence; for example, you can create a new conversation with another Jabber entity when you are handling a <font face="monospace">disco#info</font>. The new conversation&#8217;s <font face="monospace">ConversationContext</font> will inherit all the values from the <font face="monospace">ConversationContext</font> of the currently opened implicit conversation.</p>
<p>The only restriction is that this works with messages (<font face="monospace">&lt;message&gt;</font>) only.<br />
<h3>Flash Conversations</h3>
<p>Inheriting conversation only works when a new conversation is created and started in the context of an existing conversation; for example in the case of implicit conversation (eg. sending out <font face="monospace">disco#info</font> get in a <font face="monospace">disco#item</font> result handler), the out going IQ packet&#8217;s conversation is automatically started by Vorpal so it&#8217;ll inherit the conversation states of the existing conversation. Similar situation with inserting a new thread id into messages described above.</p>
<p>There are cases where you would like to inherit the conversation states from an existing conversation but you only wish to start the conversation in the next packet that you receive. Here is an example
<ol>
<li><font face="monospace">fred@myjabber</font> sends a message to <font face="monospace">chess@playground.myjabber</font> to create a chess game</li>
<li><font face="monospace">chess@playground.myjabber</font> randomly generates a game room name <font face="monospace">abcde</font>. Saves <font face="monospace">abcde@conference.myjabber</font> and <font face="monospace">fred@myjabber</font> in <font face="monospace">ConversationContext</font>.</li>
<li>sends a presence to <font face="monospace">abcde@conference.myjabber</font> to create a chat room to host the game</li>
<li><font face="monospace">conference.myjabber</font> returns after creating the room</li>
<li><font face="monospace">chess@playground.myjabber</font> now sends a <a target="_blank" href="http://xmpp.org/extensions/xep-0249.html">direct invitation</a> to <font face="monospace">fred@myjabber</font>. Also updates <font face="monospace">ConversationContext</font> with any other data need for the game</li>
</ol>
<p>Since the invitation to <font face="monospace">fred@myjabber</font> is a message (<font face="monospace">&lt;message&gt;</font>), no conversation will be started. What Vorpal does at this point is this; if there is an existing conversation, it&#8217;ll take all the states in the <font face="monospace">ConversationContext</font> and save that in the flash conversation map. Vorpal constructs a pseudo thread id made up of the &#8216;to&#8217; and &#8216;from&#8217; attributes from the message. </p>
<p>Sometime in the future, when <font face="monospace">fred@myjabber</font> confirms the chess game, Vorpal will now pickup the existing <font face="monospace">ConversationContext</font> (and <font face="monospace">Conversation</font>) object from flash and starts the conversation.</p>
<p>So flash conversations are conversations that are not started but have the potential to start sometime in the <u>near</u> future; its model after <a target="_blank" href="http://api.rubyonrails.org/classes/ActionDispatch/Flash.html">Ruby on Rails&#8217; flash</a>. If a flash conversation is not started within a certain time period (default is 3 mins), its discarded. </p>
<p>See <a target="_blank" href="http://java.net/projects/xmppsupport/sources/source-code-repository/show/playground/eliza/Arkham?rev=47">Arkham</a> in playground especially <a href="http://java.net/projects/xmppsupport/sources/source-code-repository/content/playground/eliza/Arkham/src/com/acme/xmpp/client/arkham/protocol/CreateChatroom.java" target="_blank"><font face="monospace">CreateChatroom.java</font></a> and <a target="_blank" href="http://java.net/projects/xmppsupport/sources/source-code-repository/content/playground/eliza/Arkham/src/com/acme/xmpp/client/arkham/protocol/ConfirmChatRoom.java"><font face="monospace">ConfirmChatroom.java</font></a>.</p>
<p>You can find the latest bundle <a target="_blank" href="http://java.net/projects/xmppcontainer/downloads">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=221&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/09/07/art-of-conversation-explicit-conversation-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>
	</item>
		<item>
		<title>Art of Conversation: Implicit Conversations &#8211; Part 2</title>
		<link>http://oneminutedistraction.wordpress.com/2012/08/14/art-of-conversation-implicit-conversations-part-2/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/08/14/art-of-conversation-implicit-conversations-part-2/#comments</comments>
		<pubDate>Tue, 14 Aug 2012 06:49:21 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[XMPP/Jabber]]></category>

		<guid isPermaLink="false">http://oneminutedistraction.wordpress.com/?p=219</guid>
		<description><![CDATA[&#8220;You say it best, when you say nothing at all&#8221; &#8211; When You Say Nothing At All, Ronan Keating In my last blog, I reintroduce conversation support in Vorpal. Conversation allows Vorpal application to keep states about ongoing covnersation. One of the side effect of conversation support is a notion call implicit conversation.&#160; What is [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=219&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>&#8220;<i>You say it best, when you say nothing at all</i>&#8221; &#8211; <a href="http://www.youtube.com/watch?v=AuJrEBtmM1Q">When You Say Nothing At All</a>, Ronan Keating
<div></div>
<div>
<div>In my <a href="http://oneminutedistraction.wordpress.com/2012/07/29/art-of-conversation-part-1/">last blog</a>, I reintroduce conversation support in Vorpal. Conversation allows Vorpal application to keep states about ongoing covnersation. One of the side effect of conversation support is a notion call implicit conversation.&nbsp;</div>
<div></div>
<div>What is an implicit conversation? It is when a Vorpal application</div>
<div>
<ul>
<li>Either sends/receives an IQ get/set packet, and</li>
<li>Receives/sends an IQ result/error packet in response</li>
</ul>
<div>So an implicit conversation is an exchange between a pair of corresponding IQ packets, having the same packet id; the conversation is started automatically when you sends or receives an IQ get or set and is terminated automatically when you receives or sends an IQ result or error packet.</div>
</div>
</div>
<div></div>
<h2>Starting and Terminating Implicit Conversations</h2>
<div></div>
<div>Note: the following examples makes heavy use of service discovery. If you&#8217;re not familiar with service discovry have a look at <a href="http://xmpp.org/extensions/xep-0030.html">XEP-0030</a> and also this <a href="http://oneminutedistraction.wordpress.com/2010/07/26/discovering-services/">blog</a>.</div>
<div></div>
<div>Lets look at how implicit conversation works with the following use case: assume that we are writing a service &nbsp;that upon receiving a &#8216;chat&#8217; message will send and&nbsp;<code>disco#info</code>&nbsp;to the server. When the reply comes back, the services will send a message back to the message sender with the number of features that the server supports.</div>
<div></div>
<div>Lets first look at the code to send the&nbsp;<code>disco#info</code>&nbsp;and receive the result</div>
<div></div>
<p><code>&nbsp; &nbsp;@Body("{ignore}")</code>
<div><code>&nbsp; &nbsp;private void <b>handleMessage</b>(@From JID from) {&nbsp;</code>
<div><code>&nbsp; &nbsp; &nbsp; ResponseContext iqCtx = new ResponseContext(ResponseContext.Type.IQ); &nbsp;&nbsp;</code></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp; &nbsp; iqCtx.to(postStartEvt.getDomain()).add(IQ.Type.get)</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(new IQChildElement(&#8220;query&#8221;, &#8220;<a href="http://jabber.org/protoco/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protoco/disco#info&#038;#8221</a>;);</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;"><font class="Apple-style-span" color="#008000">&nbsp; &nbsp; &nbsp;<b> //Implicit conversation starts the moment we send the IQ packet &nbsp;</b></font></span></div>
<div><span class="Apple-style-span" style="font-family:monospace;"><b><font class="Apple-style-span" color="#008000">&nbsp; &nbsp; &nbsp; componentContext.send(iqCtx);&nbsp;</font></b></span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;}&nbsp;</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;"><br /></span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;@Query(namespace=&#8221;<a href="http://jabber.org/protoco/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protoco/disco#info&#038;#8221</a>;)&nbsp;</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;@IQType(type=IQ.Type.result)&nbsp;</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;private void <b>discoInfoReply</b>(@Feature List&lt;FeatureSpecification&gt; features)&nbsp;</span><span class="Apple-style-span" style="font-family:monospace;">{</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp; &nbsp; //Do something&nbsp;</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;"><font class="Apple-style-span" color="#c00000">&nbsp; <b>&nbsp;} //Implicit conversation ends the moment we exit the handle method&nbsp;</b></font></span></div>
<div>
<div></div>
<p>In the example above, we have 2 different methods &nbsp;<code>handleMessage()</code>&nbsp;and&nbsp;<code>discoInfoReply()</code>in 2 difference classes (one class annotated with&nbsp;<code>@Message</code>&nbsp;and the other with<code> @IQ</code>).&nbsp;On receiving a chat message,&nbsp;<code>handleMessage()</code>&nbsp;constructs a&nbsp;<code>disco#info</code>&nbsp;and sends it to the server. If every thing goes well, a reply will return to be handled by&nbsp;<code>discoInfoReply()</code>&nbsp;handler.&nbsp;</div>
<div></div>
<div>An implicit conversation begins the moment we send an IQ get/set packet (the <font class="Apple-style-span" color="#008000">green</font> lines); the implicit conversation resumes when we receive an IQ result/error reply and do not end until we exit the handler (the <font class="Apple-style-span" color="#c00000">red</font> lines). In the case where this is no handler to the IQ response, the implict conversation will resume and then terminates immediately. The only time that we have an incomplete implicit conversation is if we did not receive any reply. &nbsp;More on this later.&nbsp;</div>
<div></div>
<div>The reverse is also true viz. when our service receives a IQ get, the implicit conversation starts the moment the handler is invoked and ends when we exit the handler method.</div>
</div>
<div></div>
<div>Vorpal tracks IQ request/response packets by their packet id. In XMPP, IQ packets are required to have packet ids; furthermore an IQ request/response packet pair must have the same packet id. Vorpal uses this to track the implicit conversation.</div>
<div></div>
<h2>Saving States in ConversationContext</h2>
<div></div>
<div>Lets continue with our example. We have fulfilled the first part of our use case and that is by sending a&nbsp;<code>disco#info</code>&nbsp;to our server. However to send the result back to message sender we need to know the sender&#8217;s JID. But the sender&#8217;s JID is only available in&nbsp;<code>handleMessage()</code>. For this we will need to use&nbsp;<code>ConversationContext</code>.&nbsp;</div>
<div></div>
<p><code>&nbsp; &nbsp;@Message<br /></code>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;public class MessageHandler {</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;&nbsp; &nbsp;@Inject ComponentContext componentContext;</font></div>
<div><font class="Apple-style-span" color="#0000c0"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;</span><font class="Apple-style-span" face="monospace"><b>&nbsp; &nbsp;@Inject ConversationContext convCtx;</b></font></font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;&nbsp; &nbsp;@Body(&#8220;{ignore}&#8221;)</font></div>
<div><font class="Apple-style-span" face="monospace"><code>&nbsp; &nbsp;&nbsp; &nbsp;private void&nbsp;<b>handleMessage</b>(@From JID from) {&nbsp;</code></font></div>
<div><font class="Apple-style-span" face="monospace"><code>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; //Save the send in the conversation context</code></font></div>
<div><font class="Apple-style-span" face="monospace"><font class="Apple-style-span" color="#0000c0">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; <b>convCtx.setAttribute(&#8220;sender&#8221; from);</b></font>
<div style="font-family:Times;"><code>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; ResponseContext iqCtx = new ResponseContext(ResponseContext.Type.IQ); &nbsp;&nbsp;</code></div>
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; iqCtx.to(postStartEvt.getDomain()).add(IQ.Type.get)</span></div>
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(new IQChildElement(&#8220;query&#8221;, &#8220;<a href="http://jabber.org/protoco/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protoco/disco#info&#038;#8221</a>;);</span></div>
<p></font><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;</span><font class="Apple-style-span" color="#008000"><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp;</font><b><font class="Apple-style-span" face="monospace">&nbsp;//Implicit conversation starts the moment we send the IQ packet</font></b></font></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<b>componentContext.send(iqCtx);&nbsp;<br /></b></span><font class="Apple-style-span" face="monospace">
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp;}</span></div>
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;"><br /></span></div>
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;@IQ</span></div>
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;public class IQHandler {</span></div>
<div style="font-family:Times;"><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp;@Inject ComponentContext componentContext;</span></div>
<p></font><span class="Apple-style-span" style="font-family:monospace;"><font class="Apple-style-span" color="#0000c0">&nbsp; &nbsp;&nbsp; &nbsp;</font></span><b><font class="Apple-style-span" face="monospace" color="#0000c0">@Inject ConversationContext convCtx;</font><br /></b>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp;@Query(namespace=&#8221;<a href="http://jabber.org/protoco/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protoco/disco#info&#038;#8221</a>;)&nbsp;</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp;@IQType(type=IQ.Type.result)&nbsp;</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp;private void&nbsp;<b>discoInfoReply</b>(@Feature List&lt;FeatureSpecification&gt;&nbsp;features)&nbsp;</span><span class="Apple-style-span" style="font-family:monospace;">{</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;"><font class="Apple-style-span" color="#0000c0">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; <b>JID from = (JID)convCtx.getAttribute(&#8220;sender&#8221;);</b></font></span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; ResponseContext msgCtx = new ResponseContext(ResponseContext.Type.Message);</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; msgCtx.to(from)</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(&#8220;There are &#8221; + features.size() + &#8221; features&#8221;);</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; componentContext.send(msgCtx);</span></div>
<div><span class="Apple-style-span" style="font-family:monospace;">&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<b><font class="Apple-style-span" color="#c00000">}&nbsp;</font></b></span><span class="Apple-style-span" style="color:rgb(192,0,0);font-family:monospace;"><b>//Implicit conversation ends the moment we exit the handle method</b></span></div>
<div></div>
<div>When we receive the chat message, we save the send&#8217;s&nbsp;<code>JID</code>&nbsp;in a&nbsp;<code>ConversationContext</code>. A&nbsp;<code>ConversationContext</code>&nbsp;is like a&nbsp;<code><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpSession.html">HttpSession</a></code>&nbsp;object. So when we send the&nbsp;<code>disco#info</code>, the&nbsp;<code>ConversationContext</code>&nbsp;will be associated with the implicit message. Upon receiving a reply, Vorpal will reinject the same&nbsp;<code>ConversationContext</code>&nbsp;object based on the id of the IQ packet.</div>
</div>
<div></div>
<div>Note: Observant reader may recall that in <code>handleMessage()</code>&nbsp;the implicit conversation doesn&#8217;t start until we send the IQ packet (the <font class="Apple-style-span" color="#008000">green</font> line). So the&nbsp;<code>ConversationContext</code>&nbsp;object in this case is said to be an unbounded context as oppose to a bounded one in&nbsp;<code>discoInfoReply()</code>.</div>
<div></div>
<h2>Inheriting Conversations</h2>
<div></div>
<div><span class="Apple-style-span" style="font-family:monospace;">ComponentContext.send()</span>&nbsp;allows you to send more than 1 packet. What if in&nbsp;<code>handleMessage()</code>&nbsp;method, we send more than one&nbsp;<code>disco#info</code>&nbsp;packet to different JID entities eg.&nbsp;<code>conference.mydomain</code>&nbsp;and&nbsp;<code>pubsub.mydomain</code>&nbsp;like the following code snippet</div>
<div></div>
<p><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;componentContext.send(toConference, toPubsub);<br /></font>
<div></div>
<div>where&nbsp;<code>toConference</code>&nbsp;and&nbsp;<code>toPubsub</code>&nbsp;are&nbsp;<code>disco#info</code>&nbsp;packets. Firstly, Vorpal will start two separate implicit conversation since the two IQ packets will have different ids. Secondly both the implicit conversations will have their own separate&nbsp;<code>ConversationContext</code>&nbsp;but these two&nbsp;<code>ConversationContexts</code>&nbsp;will <b><u>inherit</u></b> all the attributes from the existing&nbsp;<code>convCtx</code>. <u>Conversation inheritance applies to all implicit conversations irregardless of whether a&nbsp;<code>ConversationContext</code>&nbsp;is bound or unbound.</u></div>
<div></div>
<div>Lets consider another scenario. Assume that instead of sending back the number of features that a XMPP server supports, we want to return a list of all Jabber entities discoverable through&nbsp;<code>disco#items</code>. The algorithm to do that is roughly like this:</div>
<div>
<ol>
<li>Send a&nbsp;<code>disco#info</code>&nbsp;to the Jabber entity. If this is the start, then it&#8217;ll be the JID of the XMPP server</li>
<li>Check if the replies contains&nbsp;<code>disco#items</code></li>
<li>Send a get&nbsp;<code>disco#item</code>&nbsp;to the Jabber entity. Again the first JID will be the XMPP server</li>
<li>Save a list of all the item when the disco#item reply returns.&nbsp;</li>
<li>For each JID in <code>&lt;item&gt;</code>, repeat from step 1 until there are no more&nbsp;<code>disco#items</code>&nbsp;to be found</li>
</ol>
<div>The following code kicks off this process. when we receives a message</div>
</div>
<div></div>
<p><font class="Apple-style-span" face="monospace"><span class="Apple-style-span" style="font-family:Times;"><code>&nbsp; &nbsp;@Message<br /></code>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;public class MessageHandler {</font></div>
<div><font class="Apple-style-span" face="monospace"><br /></font></div>
<p></span></font>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; @Inject ConversationContext convCtx;</font></div>
<div><font class="Apple-style-span" face="monospace"><br /></font>
<div><font class="Apple-style-span" face="monospace"><span class="Apple-style-span" style="font-family:Times;">
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; @Body(&#8220;{ignore}&#8221;)</font></div>
<div><font class="Apple-style-span" face="monospace"><code>&nbsp; &nbsp;&nbsp; &nbsp;private void&nbsp;<b>handleMessage</b>(@From JID from) {&nbsp;</code></font></div>
<p></span></font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ResponseContext iqCtx = new ResponseContext(ResponseContext.Type.IQ);</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iqCtx.add(IQ.Type.get)</font><span class="Apple-style-span" style="font-family:monospace;">.to(postStartEvt.getDomain())</span></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add(new IQChildElement(&#8220;query&#8221;, &#8220;<a href="http://jabber.org/protocol/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protocol/disco#info&#038;#8221</a>;);</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Setup the conversation context</font></div>
<div><font class="Apple-style-span" face="monospace"><span class="Apple-style-span" style="color:rgb(192,0,0);"><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;convCtx.setAttribute(&#8220;requestor&#8221;, from);</b></span></font></div>
<div><font class="Apple-style-span" face="monospace" color="#c00000"><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;convCtx.setAttribute(&#8220;jids&#8221;, new HashSet&lt;JID&gt;());</b></font></div>
<div><font class="Apple-style-span" face="monospace" color="#c00000"><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;convCtx.setAttribute(&#8220;disco#info&#8221;, new AtomicInteger(1));</b></font></div>
<div><span class="Apple-style-span" style="color:rgb(192,0,0);font-family:monospace;"><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;convCtx.setAttribute(&#8220;disco#items&#8221;, new AtomicInteger(0));</b></span></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;postStartEvt.getComponentContext().send(iqCtx);</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;}<br /></font>
<div></div>
<div>The only important thing here is that we setup a collection to hold our discovered JIDs. Also we create 2&nbsp;<code>AtomicInteger</code>&nbsp;to count the number of&nbsp;<code>disco#info</code>&nbsp;and&nbsp;<code>disco#items</code>&nbsp;that we have sent. &nbsp;This is how the counters work: every time we send a get&nbsp;<code>disco#info</code>&nbsp;we increment the count. When we receive a reply for&nbsp;<code>disco#info</code>, we decrement the counter. When the counter reaches zero, we know that we have completed our search. &nbsp;The same goes for the&nbsp;<code>disco#items</code>&nbsp;counter. We also keep the client&#8217;s JID for the reply.</div>
</div>
</div>
<div></div>
<div>The following class shows the real work of collating the Jabber entities.</div>
<div></div>
<p><font class="Apple-style-span" face="monospace">
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp;@IQ</font></div>
<p>&nbsp; &nbsp;public class CollateJID {</font>
<div><font class="Apple-style-span" face="monospace"><br /></font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; @Inject ConversationContext convCtx;</font></div>
<div>
<div><font class="Apple-style-span" face="monospace"><br /></font>
<div><font class="Apple-style-span" face="monospace" color="#c00000"><b>&nbsp; &nbsp; &nbsp; @Query(namespace=&#8221;<a href="http://jabber.org/protocol/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protocol/disco#info&#038;#8221</a>;)</b></font></div>
<div><font class="Apple-style-span" face="monospace" color="#c00000"><b>&nbsp; &nbsp; &nbsp; @Feature(&#8220;<a href="http://jabber.org/protocol/disco#items&#038;#8221" rel="nofollow">http://jabber.org/protocol/disco#items&#038;#8221</a>;)</b></font></div>
<div><font class="Apple-style-span" face="monospace" color="#c00000"><b>&nbsp; &nbsp; &nbsp; public ResponseContext discoInfoResult(@From JID from) {</b></font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Decrement the disco#info counter cause we have just got a reply</font></div>
<div><font class="Apple-style-span" face="monospace"><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;discoInfo(-1);</b></font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ResponseContext iqCtx = new ResponseContext(ResponseContext.Type.IQ);</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iqCtx.add(IQ.Type.get).to(from)</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.add(new IQChildElement(&#8220;query&#8221;, &#8220;<a href="http://jabber.org/protocol/disco#items&#038;#8221" rel="nofollow">http://jabber.org/protocol/disco#items&#038;#8221</a>;);</font></div>
<div><font class="Apple-style-span" face="monospace"><span class="Apple-style-span" style="font-family:Times;">
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Decrement the disco#items counter cause we have just got a reply</font></div>
<div><font class="Apple-style-span" face="monospace"><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;discoItems(1);</b></font></div>
<p></span></font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (iqCtx);</font></div>
<div><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; }</font></div>
<div><font class="Apple-style-span" face="monospace"><br /></font></div>
<div><font class="Apple-style-span" face="monospace">
<div style="font-family:Times;"><font class="Apple-style-span" face="monospace" color="#0000ff"><b>&nbsp; &nbsp; &nbsp; @Query(namespace=&#8221;<a href="http://jabber.org/protocol/disco#info&#038;#8221" rel="nofollow">http://jabber.org/protocol/disco#info&#038;#8221</a>;)</b></font></div>
<div style="font-family:Times;"><font class="Apple-style-span" face="monospace" color="#0000ff"><b>&nbsp; &nbsp; &nbsp; public ResponseContext discoInfoResult() {</b></font></div>
<div style="font-family:Times;"><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Decrement the disco#info counter cause we have just got a reply</font></div>
<div><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;discoInfo(-1);</b></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Check to see if&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (shouldEnd())</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (createReply());</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (null);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div style="font-family:Times;"><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; }</font></div>
<div></div>
<div><b><font class="Apple-style-span" color="#008000">&nbsp; &nbsp; &nbsp; @Query(namespace=&#8221;<a href="http://jabber.org/protocol/disco#items&#038;#8221" rel="nofollow">http://jabber.org/protocol/disco#items&#038;#8221</a>;)</font></b></div>
<div><b><font class="Apple-style-span" color="#008000">&nbsp; &nbsp; &nbsp; public ResponseContext discoItemsResult(@Item List&lt;ItemSpecification&gt; jids) {</font></b></div>
<div>
<div style="font-family:Times;"><font class="Apple-style-span" face="monospace">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Decrement the disco#items counter cause we have just got a reply</font></div>
<div><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;discoItems(-1);</b></div>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (jids.isEmpty() &amp;&amp; shouldEnd())</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (createReply());</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Set&lt;JID&gt; jidSet = (Set&lt;JID&gt;)convCtx.getAttribute(&#8220;jids&#8221;);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ResponseContext iqCtx = ResponseContext(ResponseContext.Type.IQ);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;iqCtx.add(IQ.Type.get)</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.add(new IQChildElement(&#8220;query&#8221;, &#8220;<a href="http://jabber.protocol.disco#info&#038;#8221" rel="nofollow">http://jabber.protocol.disco#info&#038;#8221</a>;));</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (ItemSpecification j: jids) {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iqCtx.to(j.getJid());</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jidSet.add(j.getJid);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//Increment the disco#info counter. We are about to send a bunch of these</div>
<div><b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;discoInfo(jids.size());</b></div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (iqCtx);</div>
<div>&nbsp; &nbsp; &nbsp; }</div>
<div></div>
<div>&nbsp; &nbsp; &nbsp; //Helper methods</div>
<div>&nbsp; &nbsp; &nbsp; private int discoInfo(int i) {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (((AtomicInteger)convCtx.getAttribute(&#8220;disco#info&#8221;)).addAndGet(i));</div>
<div>&nbsp; &nbsp; &nbsp; }</div>
<div>
<div>&nbsp; &nbsp; &nbsp; private int discoItems(int i) {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (((AtomicInteger)convCtx.getAttribute(&#8220;disco#items&#8221;)).addAndGet(i));</div>
<div>&nbsp; &nbsp; &nbsp; }</div>
</div>
<div>&nbsp; &nbsp; &nbsp; private boolean shouldEnd() {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return ((discoInfo(0) &lt;= 0) &amp;&amp; (discoItems(0) &lt;= 0));</div>
<div>&nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp; &nbsp; private ResponseContext createReply() {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Set&lt;JID&gt; jidSet = (Set&lt;JID&gt;)convCtx.getAttribute(&#8220;jids&#8221;);</div>
<div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ResponseContext reply = new ResponseContext(ResponseContext.Type.Message);</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;reply.to((JID)convCtx.getAttribute(&#8220;requestor&#8221;))</div>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.add(jidSet.toString());</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return (reply);</div>
<div>&nbsp; &nbsp; &nbsp; }</div>
<div>&nbsp; &nbsp;}</div>
<p></font>
<div></div>
<div>The code is long but its acutally quite simple. After sending the initial&nbsp;<code>disco#info</code>&nbsp;to the server (in&nbsp;<code>handleMessage()</code>), the returns the result. This is handle by our IQ handler class&nbsp;<code>CollateJID</code>&nbsp;above. The<code> disco#info</code>&nbsp;reply can produce 2 different response; if the reply does not contain&nbsp;<code>disco#items</code>&nbsp;feature (meaning the server does not have any Jabber entities), then this will be handled by&nbsp;<code>discoInfoResult()</code>&nbsp;method (in <font class="Apple-style-span" color="#0000ff">blue</font>). We decrement the&nbsp;<code>disco#info</code>&nbsp;counter in the conversation context and checks if we should end.&nbsp;</div>
<div></div>
<div>If the&nbsp;<code>disco#info</code>&nbsp;reply do contain&nbsp;<code>disco#items</code>&nbsp;feature, then we send a&nbsp;<code>disco#items</code>&nbsp;to the sender of the reply packet. At the same time we decrement&nbsp;<code>disco#info</code>&nbsp;and increment&nbsp;<code>disco#items</code>&nbsp;counter. This is handled by&nbsp;<code>discoInfoResult()</code>&nbsp;(in <font class="Apple-style-span" color="#c00000">red</font>).</div>
</div>
</div>
</div>
<div></div>
<div>When we receive the&nbsp;<code>disco#items</code>&nbsp;reply, for every JIDs in the reply we send need to further send a<code> disco#info</code>&nbsp;packet. This is handle by&nbsp;<code>discoItemsResult()</code>&nbsp;(in <font class="Apple-style-span" color="#008000">green</font>). After that, the entire service discovery process repeats itself.</div>
<div></div>
<div>What is interesting to note is the conversation inheretance happening here. Once we have initialized the initial conversation context object with the sender&#8217;s JID and the counters, these values are inherited in all subsequent implicit conversations. For example, when we are in&nbsp;<code>discoInfoResult()</code>&nbsp;(the <font class="Apple-style-span" color="#c00000">red</font> one) in response to a&nbsp;<code>disco#info</code>&nbsp;reply, &nbsp;Vorpal will inject the previously created conversation context into the handler. Now when we now send out&nbsp;<code>disco#items</code>, Vorpal will create a new conversation context for this implicit conversation. But since there is an existing conversation context, the new conversation context for the outgoing&nbsp;<code>disco#items</code>&nbsp;packet will inherit all the values.</div>
<div></div>
<div>What happens if there is no response to a previously sent&nbsp;<code>disco#items</code>&nbsp;or&nbsp;<code>disco#info</code>&nbsp;packet? Wouldn&#8217;t Vorpal be holding all these objects in conversation context? Memory leak? All conversations have timeouts unless you turn them off. When timeout occurs for a particular conversation, the conversation context object for that conversation will be destroyed. We will look at timeout and handling them in a future blog.</div>
<div></div>
<div>If you&#8217;re testing implicit conversation and find a bug or have issues or questions, please post them to the <a href="http://java.net/projects/jabberwocky/forums">forum</a>.</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/219/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/219/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=219&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/08/14/art-of-conversation-implicit-conversations-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>
	</item>
		<item>
		<title>Art of Conversation &#8211; Part 1</title>
		<link>http://oneminutedistraction.wordpress.com/2012/07/29/art-of-conversation-part-1/</link>
		<comments>http://oneminutedistraction.wordpress.com/2012/07/29/art-of-conversation-part-1/#comments</comments>
		<pubDate>Sat, 28 Jul 2012 22:48:59 +0000</pubDate>
		<dc:creator>oneminutedistraction</dc:creator>
				<category><![CDATA[CDI]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Netbeans]]></category>
		<category><![CDATA[XMPP/Jabber]]></category>
		<category><![CDATA[xmpp vorpal conversation muc]]></category>

		<guid isPermaLink="false">http://oneminutedistraction.wordpress.com/?p=215</guid>
		<description><![CDATA[Reintroduce conversation support in Vorpal<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=215&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div><img width="400" height="267" src="http://oneminutedistraction.files.wordpress.com/2012/07/lotus.jpg?w=400&#038;h=267"></div>
<div>It has been a while since my last blog. A combination of work and <a href="http://en.wikipedia.org/wiki/Battlestar_Galactica_%282004_TV_series%29">reimagining</a> conversation support led to this hiatus. After the last blog, I wanted to do a series on the applicational aspect of <a href="http://oneminutedistraction.wordpress.com/2011/08/07/integrating-cdi-with-vorpal-and-the-cuckoo-bird-effect/">Vorpal</a> viz. how do you go about using the various features in Vorpal to implement a custom XMPP service. However I found IQ support wanting; in particular correlating IQ request/response to be really tedious. I&#8217;ve also felt, for quite sometime now, that my initial design for conversation support which I blog about <a href="http://oneminutedistraction.wordpress.com/2011/08/20/convesations-with-vorpal/">here</a> to be extremely simplistic. So I took the opportunity to redesign these.</div>
<div></div>
<div>The good news is that if you are using previous version of Vorpal to develop your application, nothing apparent have change; everything should works as is. If you have any issues, please <a href="http://java.net/jira/browse/VORPAL">open an issue</a>&nbsp;or post your problem in the <a href="http://java.net/projects/vorpal/forums">forum</a>. Bad news? Conversation support is quite tricky because it affects the framework greatly so if you find any NPE which wasn&#8217;t in earlier releases of Vorpal, please let me know.</div>
<div></div>
<div>Rather than writing a long blog on conversation, I&#8217;m going to break this subject into 3 or 4 blogs convering concepts, implicit and explicit conversations and interactions between these.</div>
<h2>What are Conversations?</h2>
<div>What is the purpose of conversation? Simply put conversation allows a Vorpal application to keep states about an interaction between 2 jabber entity. This is very similar to <a href="http://en.wikipedia.org/wiki/Session_management">HTTP session support</a> using&nbsp;<code><a href="http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html">HttpSession</a></code>. In Vorpal states can be kept in&nbsp;<code>ConversationContext</code>, very similiar to&nbsp;<code>HttpSession</code>, and/or in stateful objects marked with&nbsp;<code><a href="http://docs.oracle.com/javaee/6/api/javax/enterprise/context/Conversation.html">@Conversation</a></code>&nbsp;(think <a href="http://docs.oracle.com/javaee/6/tutorial/doc/gipjg.html">Stateful Session Bean</a>). Every conversation is marked with a unique conversation id.</div>
<div></div>
<div>The default conversation provider gets the conversation id from the following&nbsp;</div>
<div>
<ul>
<li>when there is a&nbsp;<code>&lt;thread&gt;</code>&nbsp;element in&nbsp;<code>&lt;message&gt;</code>. This is part of the XMPP standard. See <a href="http://xmpp.org/extensions/xep-0201.html">XEP-0201</a>.</li>
<li>or by using implied conversation id support between any 2 Jabber entities when&nbsp;<span class="Apple-style-span" style="font-family:monospace;">&lt;thread&gt;</span>&nbsp;element is missing.&nbsp;</li>
</ul>
</div>
<div>All packet falling under the same conversation (viz. having the same conversation id) will be able to access to the same state information stored in&nbsp;<code>ConversationContext</code>&nbsp;or&nbsp;<code>@Conversation</code>&nbsp;message handlers.</div>
<div></div>
<div>The way implied conversation id works is by examining the to or from of a packet and figure out a thread id from there. Vorpal will first look for&nbsp;</div>
<div>
<ul>
<li>conversation between an entity and a chat room (ok an entity as well). It does this by looking for the following pattern &#8216;<code>@conference.</code>&#8216; in either the from or to. If it finds it then Vorpal can use the chat rooms JID as a conversation id</li>
<li>if the interaction is not with a chat room, then Vorpal will <b>sort</b> the full JID of to and from of a packet and concatenate them giving us a unique conversation id</li>
</ul>
</div>
<div>The advantage of implied conversation id over those explicity marked with&nbsp;<span class="Apple-style-span" style="font-family:monospace;">&lt;thread&gt;</span><span class="Apple-style-span">&nbsp;element is that implied conversation supports all 3 types of packets (message, iq and presence) whereas&nbsp;</span><span class="Apple-style-span" style="font-family:monospace;">&lt;thread&gt;</span><span class="Apple-style-span">&nbsp;is only supported in message. The down side of it is that because its implied viz we compute the conversation id using information from the packet, you are really at the mercy of this algorithmn and have no control over the conversation states.</span></div>
<h2>Conversation Types</h2>
<div>Vorpal supports 2 types of conversation:</div>
<div>
<ol>
<li>implicit conversation &#8211; these are for a request response cycle; they are started automatically started automatically when you send IQ (get/set) packets and are terminated automatically when the corresponding IQ response (result/error) is received or vice versa</li>
<li>explicit conversations &#8211; these are conversations that are started by the service by calling&nbsp;<code><a href="http://docs.oracle.com/javaee/6/api/javax/enterprise/context/Conversation.html#begin()">Conversation.begin()</a></code>. Under certain situation explicit conversations are also started automatically; however you must terminate the conversation. We&#8217;ll look at this in a future blog (of this series)</li>
</ol>
<h2>Conversation Examples from the Playground</h2>
</div>
<div>I&#8217;ll go into the nitty gritty details of conversation in my next blog. For the impatient, have a look at Eliza in <a href="http://java.net/projects/xmppsupport/sources/source-code-repository/show/playground/eliza">playground</a>. It contains 2 <a href="http://www.netbeans.org">Netbeans</a> project;</div>
<div>
<ul>
<li><code>ElizaService</code>&nbsp;is the external component that implements <a href="http://en.wikipedia.org/wiki/ELIZA">Eliza</a>. I got the source <a href="http://coweb.cc.gatech.edu/cs1316/62">here</a>. To run it you need to define &#8216;arkham&#8217; as a subdomain in your XMPP server and the shared secret is &#8216;ICannotSleep&#8217;. Or you can change the deployment settings</li>
<li><code>Arkham</code>&nbsp;is a Java Eliza client</li>
</ul>
<div>Deploy&nbsp;<span class="Apple-style-span" style="font-family:monospace;">ElizaService</span>&nbsp;to <a href="http://glassfish.java.net/">Glassfish</a> with Jabberocky. To verify that Eliza has been deployed successfully run a discovery service on it. The following screenshot from <a href="http://gajim.org/">Gajim</a> shows Eliza service.</div>
</div>
<div><img width="300" height="231" src="http://oneminutedistraction.files.wordpress.com/2012/07/eliza_service.png?w=300&#038;h=231"></div>
<div>There are 2 ways of accessing Eliza.&nbsp;</div>
<div>
<ol>
<li>The first is to use a standard XMPP client like Gajim. Create a chat room (<a href="http://xmpp.org/extensions/xep-0045.html/">muc</a>). Then send a <a href="http://xmpp.org/extensions/xep-0249.html">direct invitation</a> to&nbsp;<code>eliza@arkham.your_domain</code>. Once Eliza has joined the chat room it will send a greeting to the root. Now you can interact with it by telling Eliza your problem. To end the therapy session, simply type quit or bye</li>
<li>The second is to use Arkham which essentially does the same thing as 1 above. The only difference is that it will initiate a search of for the Eliza service first on the server before connecting. If it cannot find the service, it&#8217;ll notify you. Arkham uses client side Vorpal</li>
</ol>
<div>Eliza shows how to use the client and the server to create a virtual conversation; I&#8217;ll be talking about this in greater detail in my next few blogs.</div>
</div>
<div></div>
<div>For those who want to check out these new features please download the latest from <a href="http://java.net/projects/xmppcontainer/downloads">here</a>. If you have downloaded the package prior to July 29 2012, please download it again.&nbsp;</div>
<div></div>
<div>Note: I&#8217;m trying to get a hang <a href="http://blogilo.gnufolks.org/">Blogilo</a> after I&#8217;ve decided to change from <a href="https://addons.mozilla.org/en-US/firefox/addon/scribefire-blog-editor/">ScribeFire</a>. So you&#8217;ll have to excuse me if there are any issues with formatting, etc.</div>
<div></div>
<div>Till next time</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/oneminutedistraction.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/oneminutedistraction.wordpress.com/215/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=oneminutedistraction.wordpress.com&#038;blog=12103693&#038;post=215&#038;subd=oneminutedistraction&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://oneminutedistraction.wordpress.com/2012/07/29/art-of-conversation-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/17e69b45adb2cf73eda5cc74052ca0c8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">oneminutedistraction</media:title>
		</media:content>

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/07/lotus.jpg" medium="image" />

		<media:content url="http://oneminutedistraction.files.wordpress.com/2012/07/eliza_service.png" medium="image" />
	</item>
	</channel>
</rss>
