View Javadoc

1   
2   package org.neo.swarm.util.network.jxta;
3   
4   import java.io.IOException;
5   import java.net.DatagramPacket;
6   
7   import net.jxta.document.AdvertisementFactory;
8   import net.jxta.exception.PeerGroupException;
9   import net.jxta.peergroup.PeerGroup;
10  import net.jxta.peergroup.PeerGroupFactory;
11  import net.jxta.protocol.PipeAdvertisement;
12  import net.jxta.socket.JxtaMulticastSocket;
13  
14  import org.neo.swarm.util.network.multicast.Receiver;
15  
16  /***
17   * 
18   * @author navery
19   */
20  public class JXReciever implements Receiver {
21  
22  	private static PeerGroup netPeerGroup;
23  	private static PipeAdvertisement pipeAdv;
24  	protected static JxtaMulticastSocket mcastSocket;
25  	DatagramPacket packet;
26  	int bufferSize;
27  
28  	public JXReciever() {
29  		try {
30  			  
31  //		     PlatformConfigurator.c
32  //		     args[0], // peername
33  //		     "A dwPeer - " + args[0], // description
34  //		     tcpAddress, // ip
35  //		     myPort,  // ports
36  //		     rdvList    , // rdvs
37  //		     USER_NAME,
38  //		     USER_PASS
39  //		     );
40  
41  		     // disable multicast
42  		      // pass in to preserve settings
43  //		     TcpConfigurat tc = new TcpConfigurator(pa);
44  //		     tc.setMulticastState(false);
45  //		     // enable incoming connection
46  //		     tc.setServer(tcpAddress);
47  //		     tc.setServerEnabled(true);
48  //		     tc.save(pa);   // save to pa only, not file
49  			
50  			PeerGroupFactory.setNetPGDesc("swarm jxta multicast");
51  			PeerGroupFactory.setNetPGName("swamcast");
52  			netPeerGroup = PeerGroupFactory.newPeerGroup();	
53  			pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement("SWARM_MCAST");
54  			mcastSocket = new JxtaMulticastSocket(netPeerGroup, pipeAdv);
55  			
56  			// recommended max for jxta packets before chunking
57  			bufferSize = 16 * 1024;
58  			byte[] buffer = new byte[bufferSize];
59  			packet = new DatagramPacket(buffer, buffer.length);
60  	//	} catch (PeerGroupException ex){
61  		//	ex.printStackTrace();
62  		} catch (IOException ex) {
63  			ex.printStackTrace();
64  		}
65  	}
66  
67  	public byte[] read() {
68  		try {
69  			mcastSocket.receive(packet);
70          return packet.getData();
71  		} catch (IOException ex){
72  			ex.printStackTrace();
73  			return null;
74  		}
75  	}
76  
77  	public void close() {
78  	}
79  
80  }