![]() Using the P42 networkRequirements:
Basic setup and group functionalityCreate your own service groupBefore you can use the network functionality of the peeranha network you have to create a unique service group for your plugin. The following code example illustrates how you create your own service group: PeerGroup svcPg = Network.createServiceGroup(PluginID); Subsequently the service group has to be registered to our network. The following subsection shows you how to register a peer group to the network. Registering of a peer group(also service group) to the networkwhen a peer group is registered you are able to use the methods send and receive of the network like this: Network.register(PluginID, PeerGroup, [ Optional: UserName ]); The network of peeranha42 supports the possibility to own different names in several registered peer groups. Therefore you can specify any user name like you want to. In the case of null as optional user name the network uses the given peer name in this peer group. This name is createdd when you start the configurator and specify a peer name. Usage of the listeners in the registered peer groupsBy implementing the listeners you can get events in the different registered peer groups. The listeners support different tasks like checking the state of the connection to the rendezvous and updating the arrays with the found peers, pipes or group advertisements. The advantage for the developer is that she/he does not worry about the discovery tasks. So you will be informed of changes from the different listener types. The following listeners are provided when the peer group is registered:
In order that you can use a listener you have to register it for the plugin in the right peer group. This works like it is shown in the following code example: Register a P42RendezvousListeners: Network.addP42RendezvousListener(PluginID, PeerGroup, P42ConnectionListener); Register a P42PeerAdvListeners: Network.addP42PeerAdvListener(PluginID, PeerGroup, P42PeerAdvListener); Register a P42PipeAdvListeners: Network.addP42PipeAdvListener(PluginID, PeerGroup, P42PipeAdvListener); Register a P42PeerGroupListeners: Network.addP42PeerGroupListener(PluginID, PeerGroup, P42PeerGroupListener); As soon as a new listener is registered to the network you can handle the events which are given by these listeners. If there are changes concerning the connection state you can get them like this: public void rendezvousEvent(PeerGroup pg, boolean isConnectedToRendezvous) { ... } When the network founds new advertisements regarding peers, pipes or groups the corresponding listeners notice the plugin. Therefore different methods have to be implemented. // This method is invoked if the // P42PeerAdvListener is registered public void changedPeersEvent(PeerGroup pg, PeerAdvertisement peerAdv_arr[]) { ... } // This method is invoked if the // P42PipeAdvListener is registered public void changedPipesEvent(PeerGroup pg, PipeAdvertisement pipeAdv_arr[]) { ... } // This method is invoked if the // P42PeerGroupListener is registered public void changedGroupsEvent(PeerGroup pg, PeerGroupAdvertisement pgAdv_arr[]) { ... } If a listener should be deregistered you have to do the following for the different registered listeners: // deregister a P42RendezvousListeners Network.removeP42RendezvousListener(PluginID, PeerGroup, P42ConnectionListener); // deregister a P42PeerAdvListeners Network.removeP42PeerAdvListener(PluginID, PeerGroup, P42PeerAdvListener); // deregister a P42PipeAdvListeners Network.removeP42PipeAdvListener(PluginID, PeerGroup, P42PipeAdvListener); // deregister a P42PeerGroupListeners Network.removeP42PeerGroupListener(PluginID, PeerGroup, P42PeerGroupListener); Deregistering of peer groupsBefore you can leave a registered peer group you have to invoke the deregister function in the network of peeranha42. Therefore you can use two different ways which are described in the following subsections. Leave all registered peer groups of a plugin With the help of this method you can leave all joined peer groups of a plugin. This method should be invoked at the latest in the stop method of the plugin. When the method is invoked you only have to give the pluginID as parameter. Network.deregister(PluginID); Leave a specified peer group of a plugin To leave a specified peer group of a plugin you have to do the following: Network.deregister(PluginID, PeerGroup); Creating peer group(s)Creation of a new peer group You can create a new peer group with the following statement: PeerGroup newpg = Network.createGroup(PeerGroup parentPeerGroup, String peerGroupName, String peerGroupDescription); If null is inserted as ParentPeerGroup the new created peer group is a subgroup of the Peeranha42PeerGroup. You usually can use each arbitrary peer group. After the new peer group has been created it has to to be registered before you can use the network in this group. Creating a peer group with a found peer group advertisement If you have an existing PeerGroupAdvertisement you can use it to create a new peer group instance. This is especially used if you have found PeerGroupAdvertisements with the registered P42PeerGroupListener or manually with getGroupDiscoveryResults(described in the next section). PeerGroup newpg = Network.createGroup(PeerGroupAdvertisement, ParentPeerGroup); How do I get the Advertisements without using a listener of a specified peer group?
Sending messages over the networkWhen a peer group has been registered with the help of the register method to the network the plugin is able to send messages in the registered peer group. Therefore you have three possiblities: Sending unicast messagesIf you only want to send one peer a message you can do this by executing the following statement: Network.sendMsg(PluginID, PeerGroup, PipeAdvertisement, Message); The parameter peer group indicates the peer group in which the message should be sent. With the specification of the pipe advertisement you define the peer who should be receive the message. Sending multicast messagesIf you want to send a message to all peers in peer group except yourself you can do this by leaving out the pipe advertisement of the unicast sending method. The other parameters do not change. Network.sendMsg(PluginID, PeerGroup, Message); Sending messages to a Set of peers(peerIDs)If you have the desire to send messages to a specified set of peers you get a Set or Collection(for more information look at the java API at java.sun.com --> package jav.util) with the corresponding peerIDs of the specified peers. Network.sendMsgToSet(PeerGroup, Set, Message); Receiving messagesIf you want to use the following receive function there has to be an implemented Receiver-Interface. By implementing this Receiver you also have to implement the method public Plugin getPlugin(). This should return the reference to your plugin. Unless you have not implemented this method you are not able to receive messages in the following receive method: public void receive(PeerGroup pg, PipeAdvertisement pa, Message msg) The parameter peer group indicates in which peer group the arrived message was sent. Furthermore the method gives gives back the pipe advertismement of the peer who send this message. So you are able to reply to this message.
URL: |