fi.hut.tml.xsmiles.mlfc.comm.jxta
Class JXTACOMMImpl

java.lang.Object
  extended byfi.hut.tml.xsmiles.mlfc.comm.jxta.JXTACOMMImpl
All Implemented Interfaces:
COMMInterface, net.jxta.discovery.DiscoveryListener, java.util.EventListener, net.jxta.pipe.PipeMsgListener

public class JXTACOMMImpl
extends java.lang.Object
implements COMMInterface, net.jxta.pipe.PipeMsgListener, net.jxta.discovery.DiscoveryListener

This class implements a simple chat application.

It's very inspired by the hotwire application developped by Eric Pouyoul. Thanks to him for the advices and also for providing the source code of hotwire. A particular big thanks to William R. Bauer who spends time to reading the code of this application and who gives some useful advices, he spends also a lot of time trying it, thanks to him !!!

Here is how it works: First we try to find an advertisement for a chat group. If we find it, we create the pipes in order to talk with the member of the group. If we don't find it, we create a new propagate Pipe Advertisement and we publish it. It's possible that two chat groups may be on the same network. To avoid that, you must raise the sleeping time.

Author:
Sebastien Baehni (in collaboration with William R. Bauer and Eric Pouyoul)

Field Summary
static java.lang.String MIME_TEXT
           
 
Fields inherited from interface fi.hut.tml.xsmiles.mlfc.comm.COMMInterface
JOIN_ERROR, JOIN_GROUPCREATED, JOIN_GROUPJOINED
 
Constructor Summary
JXTACOMMImpl()
          Main constructor.
 
Method Summary
 void addDataListener(DataListener dl)
          Receive data
 net.jxta.protocol.PipeAdvertisement createChatAdvertisement()
          Method used to create a chat advertisement.
 void createInputAndOutputPipes(net.jxta.protocol.PipeAdvertisement chatAdv)
          Method used to create input and output pipes from the chat advertisement.
 void destroy()
          Do cleanup - is this enough?
 void discoveryEvent(net.jxta.discovery.DiscoveryEvent event)
          Method used to receive new discovery event.
 boolean getFile(java.net.URL location, java.lang.String name)
           
 java.lang.String getLocalPeerName()
          Get local Peer name.
 java.lang.String getPeerGroupName()
          Get PeerGroup name.
 int initJXTAChat()
          Method used to init the application.
 int join(java.lang.String name, java.lang.String params)
          This actually starts up the JXTA network and join a "group" A group is service named: "XSmiles: "+groupname
 void leave(java.lang.String name)
           
 void pipeMsgEvent(net.jxta.pipe.PipeMsgEvent pipeMsgEvent)
          This method is used to receive new message from the inputPipe we have created before.
 boolean send(java.lang.String target, java.lang.String data)
          Send data to target
 boolean sendFile(java.lang.String target, java.net.URL data)
           
 boolean sendMime(java.lang.String target, byte[] data, java.lang.String mimetype)
          Send data to target
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIME_TEXT

public static final java.lang.String MIME_TEXT
See Also:
Constant Field Values
Constructor Detail

JXTACOMMImpl

public JXTACOMMImpl()
Main constructor. Initialize the jxta platform. Inspired by the exemple 2 of the tutorial section of http://platform.jxta.org

Method Detail

send

public boolean send(java.lang.String target,
                    java.lang.String data)
Send data to target

Specified by:
send in interface COMMInterface
Returns:
True if successful false if not

sendMime

public boolean sendMime(java.lang.String target,
                        byte[] data,
                        java.lang.String mimetype)
Send data to target

Specified by:
sendMime in interface COMMInterface
Returns:
True if successful false if not

addDataListener

public void addDataListener(DataListener dl)
Description copied from interface: COMMInterface
Receive data

Specified by:
addDataListener in interface COMMInterface

sendFile

public boolean sendFile(java.lang.String target,
                        java.net.URL data)
Specified by:
sendFile in interface COMMInterface
Parameters:
target - Send a file to a target
data - The URL where file is located  @return true if successful

getFile

public boolean getFile(java.net.URL location,
                       java.lang.String name)
Specified by:
getFile in interface COMMInterface
Parameters:
location - Where to save the file
name - The filename to get  @return true if successful

getLocalPeerName

public java.lang.String getLocalPeerName()
Get local Peer name.

Specified by:
getLocalPeerName in interface COMMInterface
Returns:
Local peer name

getPeerGroupName

public java.lang.String getPeerGroupName()
Get PeerGroup name.

Specified by:
getPeerGroupName in interface COMMInterface
Returns:
Local peer name

join

public int join(java.lang.String name,
                java.lang.String params)
This actually starts up the JXTA network and join a "group" A group is service named: "XSmiles: "+groupname

Specified by:
join in interface COMMInterface
Parameters:
params - The parameters that might be needed
name - The group to join  @return JOIN_ERROR, JOIN_GROUPCREATED or JOIN_GROUPJOINED

leave

public void leave(java.lang.String name)
Specified by:
leave in interface COMMInterface
Parameters:
name - Leave a group

destroy

public void destroy()
Do cleanup - is this enough?

Specified by:
destroy in interface COMMInterface

initJXTAChat

public int initJXTAChat()
                 throws java.lang.Exception
Method used to init the application. This method tries to discover a chat advertisement. If no chat advertisement is discovered then it creates and publishes a new one.

Returns:
JOIN_ERROR if a fatal error occurred, JOIN_GROUPJOINED if the group was successfully joined, JOIN_HROUPCREATED if the group wasn't found and a new group was created.
Throws:
net.jxta.exception.PeerGroupException - If a peer group exception occurs.
java.lang.Exception

discoveryEvent

public void discoveryEvent(net.jxta.discovery.DiscoveryEvent event)
Method used to receive new discovery event. An event is received when a new advertisement is received. This method is synchronized because we do not want receiving two new advertisements when the user is already trying to choose one.

Specified by:
discoveryEvent in interface net.jxta.discovery.DiscoveryListener
Parameters:
event - The received event.
Throws:
An - exception if something bad happened

createChatAdvertisement

public net.jxta.protocol.PipeAdvertisement createChatAdvertisement()
Method used to create a chat advertisement. This advertisement is a pipe advertisement with a special name (a prefix plus the inetaddress of the local machine).

Returns:
A new chat advertisement.

createInputAndOutputPipes

public void createInputAndOutputPipes(net.jxta.protocol.PipeAdvertisement chatAdv)
                               throws java.lang.Exception
Method used to create input and output pipes from the chat advertisement.

Parameters:
chatAdv - The chat advertisement used to create the pipes.
Throws:
java.lang.Exception

pipeMsgEvent

public void pipeMsgEvent(net.jxta.pipe.PipeMsgEvent pipeMsgEvent)
This method is used to receive new message from the inputPipe we have created before. MLFC: This receives data and sends it to the listeners.

Specified by:
pipeMsgEvent in interface net.jxta.pipe.PipeMsgListener
Parameters:
pipeMsgEvent - The pipe message event we will retrieve the message from it.


X-Smiles 1.2