View Javadoc

1   /***
2    * 
3    */
4   package base.jdbs.network;
5   
6   import java.util.Date;
7   
8   import net.jxta.endpoint.Message;
9   import net.jxta.endpoint.MessageElement;
10  import net.jxta.pipe.PipeMsgEvent;
11  import net.jxta.pipe.PipeMsgListener;
12  
13  import org.apache.log4j.Level;
14  import org.apache.log4j.Logger;
15  
16  import base.jdbs.JDBSPeer;
17  
18  /***
19   * @author skunk
20   *
21   */
22  public class JDBSPipeMsgListener implements PipeMsgListener {
23  
24  	private static final transient Logger logger = Logger.getLogger(JDBSPipeMsgListener.class.getName());
25  
26  	public static final String AVAILABLE_SPACE_REQUEST_MSG="getAvailableSpace";
27  	public static final String LOCATION_REQUEST_MSG="getLocation";
28  	
29  	
30  	private final JDBSPeer peer;
31  	
32  	
33  	public JDBSPipeMsgListener(JDBSPeer peer) {
34  		this.peer = peer;
35  	}
36  
37  
38  	/* (non-Javadoc)
39  	 * @see net.jxta.pipe.PipeMsgListener#pipeMsgEvent(net.jxta.pipe.PipeMsgEvent)
40  	 */
41  	public void pipeMsgEvent(PipeMsgEvent event) {
42  	     Message msg = null;
43  	        try {
44  	            // grab the message from the event
45  	            msg = event.getMessage();
46  	            if (msg == null) {
47  	                if (logger.isEnabledFor(Level.DEBUG)) {
48  	                    logger.debug("Received an empty message, returning");
49  	                }
50  	                return;
51  	            }
52  	            if (logger.isEnabledFor(Level.DEBUG)) {
53  	            	logger.debug("Received a response");
54  	            }
55  	            
56  	            
57  	            String senderMessage = "sender-message";
58  	            // get the message element named SenderMessage
59  	            MessageElement msgElement = msg.getMessageElement(senderMessage, senderMessage);
60  	            // Get message
61  	            if (msgElement.toString() == null) {
62  	                System.out.println("null msg received");
63  	            } else {
64  	                Date date = new Date(System.currentTimeMillis());
65  	                System.out.println(date.toString()+" Message  :"+ msgElement.toString());
66  	            }
67  	           
68  	        } catch (Exception ex) {
69  	            if (logger.isEnabledFor(Level.DEBUG)) {
70  	                logger.debug(ex);
71  	            }
72  	            return;
73  	        }
74  	}
75  	
76  
77  }