]> sjero.net Git - linphone/blob - p2pproxy/src/org/linphone/p2pproxy/core/media/jxtaudpproxy/UdpSession.java
312b899613cb696423552fafd2979d55bb09f67e
[linphone] / p2pproxy / src / org / linphone / p2pproxy / core / media / jxtaudpproxy / UdpSession.java
1 package org.linphone.p2pproxy.core.media.jxtaudpproxy;
2
3 import java.io.IOException;
4 import java.net.DatagramPacket;
5 import java.net.DatagramSocket;
6 import java.net.InetAddress;
7 import java.net.InetSocketAddress;
8 import java.net.SocketException;
9 import java.net.UnknownHostException;
10 import org.apache.log4j.Logger;
11 import org.linphone.p2pproxy.core.GenericUdpSession;
12
13 import net.jxta.endpoint.ByteArrayMessageElement;
14 import net.jxta.endpoint.MessageElement;
15 import net.jxta.pipe.OutputPipe;
16 import net.jxta.pipe.PipeMsgEvent;
17 import net.jxta.pipe.PipeMsgListener;
18
19 public class UdpSession implements GenericUdpSession.MessageHandler ,PipeMsgListener{
20    private final static Logger mLog = Logger.getLogger(UdpSession.class);   
21    private  InetSocketAddress mRemoteAddress;
22    private OutputPipe mRemotePipe;
23    private boolean mExit = false;
24    private final String mMessageName;
25    private final GenericUdpSession mGenericUdpSession;
26    UdpSession(int aPort,String aMessageName) throws SocketException, UnknownHostException {
27       mMessageName = aMessageName;
28       mGenericUdpSession = new GenericUdpSession(new InetSocketAddress(aPort),this);
29    }
30
31
32
33    public void pipeMsgEvent(PipeMsgEvent event) {
34       MessageElement lMessageElement = event.getMessage().getMessageElement(mMessageName);
35       if (lMessageElement == null) {
36          //nop, this is not for me
37          return;
38       }
39       //test if we have an address to forward to
40       if (mRemoteAddress == null) {
41          mLog.warn("no remote adress, message discarded");
42          return;
43       }
44       byte[] lBuff = lMessageElement.getBytes(false);
45       DatagramPacket lDatagramPacket = new DatagramPacket(lBuff,(int) lMessageElement.getByteLength());
46       lDatagramPacket.setSocketAddress(mRemoteAddress);
47       try {
48          mGenericUdpSession.getSocket().send(lDatagramPacket);
49          mLog.debug("message from ["+mMessageName+"] sent to ["+mRemoteAddress+"]");
50       } catch (IOException e) {
51          mLog.error("cannot send message for session ["+this+"]" , e);
52       }
53       //
54    }
55    public void setRemoteAddress( InetSocketAddress aRemoteAddress) {
56       mRemoteAddress = aRemoteAddress;
57    }
58    public void setRemotePipe(OutputPipe aRemotePipe) {
59       mRemotePipe = aRemotePipe;
60    }
61    public void close() {
62       mExit = true;
63       mGenericUdpSession.close();
64    }
65
66    public String toString() {
67       return "name ["+mMessageName +"] udp dest ["+mRemoteAddress+"]";
68    }
69    public int getPort() {
70       return mGenericUdpSession.getSocket().getPort();
71    }
72    public void onMessage(DatagramPacket message) {
73       try {
74          // if destination is known just send
75          if (mRemotePipe != null) {
76             net.jxta.endpoint.Message lMessage = new net.jxta.endpoint.Message();
77             ByteArrayMessageElement lByteArrayMessageElement = new ByteArrayMessageElement(mMessageName, null,message.getData(),0,message.getLength(), null);
78             lMessage.addMessageElement(mMessageName, lByteArrayMessageElement);
79             //send the message
80             mRemotePipe.send(lMessage);
81             mLog.debug("message from ["+message.getAddress()+":"+message.getPort()+"]sent to ["+mRemotePipe.getPipeID()+"]");
82          } else {
83             mLog.warn("output pipe not set for ["+this+"], discarding message");
84          }
85       }catch(Exception e) {
86          //nop
87       }
88
89    }
90 }