Browse Source

SDP ergänzt, sinnvoll wär es jetzt geeignete Funktionen zu erstellen + RTP zu ergänzen

RTP
Normal 5 years ago
parent
commit
8fbb5a96c8
1 changed files with 124 additions and 76 deletions
  1. 124
    76
      src/model/VoIP.java

+ 124
- 76
src/model/VoIP.java View File

@@ -12,6 +12,9 @@ import java.text.ParseException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sdp.SdpException;
import javax.sdp.SdpFactory;
import javax.sdp.SessionDescription;
import javax.sip.DialogTerminatedEvent;
import javax.sip.IOExceptionEvent;
import javax.sip.InvalidArgumentException;
@@ -42,10 +45,10 @@ public class VoIP implements SipListenerExt
AddressFactory addressFactory; // Used to create SIP URIs.
ListeningPoint listeningPoint; // SIP listening IP address/port.
Properties properties; // Other properties.
private int counter;
private int myPort = 5060;
private String myName = "129";
private String protocolTyp = "UDP";
private String myIPAddress;
@@ -53,11 +56,10 @@ public class VoIP implements SipListenerExt
private int branch = hashCode();
Address contactAddress;
private ContactHeader contactHeader;

public VoIP() //Konstruktor für eigene Daten
{
try
{
//wird an sich erstmal nicht gebraucht aber später sinnvoll um eigene Daten zu initialisieren
@@ -74,7 +76,7 @@ public class VoIP implements SipListenerExt
this.listeningPoint = this.sipStack.createListeningPoint(this.myIPAddress, this.myPort, this.protocolTyp);
this.sipProvider = this.sipStack.createSipProvider(this.listeningPoint);
this.sipProvider.addSipListener(this);
this.contactAddress = this.addressFactory.createAddress("sip:129@" + this.myIPAddress+ ":" + this.myPort); //+ ":" + this.myPort
this.contactAddress = this.addressFactory.createAddress("sip:129@" + this.myIPAddress + ":" + this.myPort); //+ ":" + this.myPort
this.contactHeader = this.headerFactory.createContactHeader(contactAddress);
}
catch (Exception e)
@@ -82,30 +84,26 @@ public class VoIP implements SipListenerExt
System.out.println("Shit");
System.exit(-1);
}
}

public int hashCode()
{
int temp = tag*(new Random(100)).nextInt();
{
int temp = tag * (new Random(100)).nextInt();
return temp;
}

public void sendRegister(String serverIP, int serverPort) throws ParseException, InvalidArgumentException, SipException
public void sendRegister(String serverIP, int serverPort) throws ParseException, InvalidArgumentException, SipException
{
System.out.println("-----------SendRegister-----------");
Address addressto = this.addressFactory.createAddress("sip:192.168.100.11"); //evtl. mit Port: sip:192.168.100.11:5060
Address addresstob = this.addressFactory.createAddress("sip:192.168.100.11"); //to get example maybe needed
ToHeader to = this.headerFactory.createToHeader(addresstob, null); // Integer.toString(tag) address + tag
URI requestURI = addressto.getURI();
CallIdHeader callId = this.sipProvider.getNewCallId();
FromHeader from = this.headerFactory.createFromHeader(addresstob, String.valueOf(this.tag)); //this.contactAddress
ArrayList via = new ArrayList(); //Via needs a List as input
ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch));
ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch));
via.add(viaheader);
CSeqHeader cSeq = this.headerFactory.createCSeqHeader(1L, "REGISTER");
MaxForwardsHeader maxForwards = this.headerFactory.createMaxForwardsHeader(70);
@@ -114,13 +112,11 @@ public class VoIP implements SipListenerExt
//Create Request Header
System.out.println("----REQUEST HEADER----");
Request request = this.messageFactory.createRequest(requestURI, "REGISTER", callId, cSeq, from, to, via, maxForwards);
request.addHeader(contactHeader);
request.addHeader(allow);
request.addHeader(expire);
System.out.println(request);
this.sipProvider.sendRequest(request);
}

@Override
@@ -131,24 +127,25 @@ public class VoIP implements SipListenerExt
@Override
public void processRequest(RequestEvent requestEvent)
{
System.out.println("----Bekomme Anfrage----");
if ((Request.OPTIONS).equals(requestEvent.getRequest().getMethod()))
System.out.println("----Bekomme Anfrage----");
//Kommunikation mit Server das man zur Verfügung steht
if ((Request.OPTIONS).equals(requestEvent.getRequest().getMethod())) //Options Anfrage von Server nach erfolgreicher Registrierung
{
System.out.println("200 OK zu Server");
try
{
Response response = this.messageFactory.createResponse(200,requestEvent.getRequest());
response.addHeader((ViaHeader)requestEvent.getRequest().getHeader("Via"));
response.addHeader((FromHeader)requestEvent.getRequest().getHeader("From"));
response.addHeader((ToHeader)requestEvent.getRequest().getHeader("To"));
response.addHeader((CallIdHeader)requestEvent.getRequest().getHeader("Call-ID"));
response.addHeader((CSeqHeader)requestEvent.getRequest().getHeader("Cseq"));
{
Response response = this.messageFactory.createResponse(200, requestEvent.getRequest());
response.addHeader((ViaHeader) requestEvent.getRequest().getHeader("Via"));
response.addHeader((FromHeader) requestEvent.getRequest().getHeader("From"));
response.addHeader((ToHeader) requestEvent.getRequest().getHeader("To"));
response.addHeader((CallIdHeader) requestEvent.getRequest().getHeader("Call-ID"));
response.addHeader((CSeqHeader) requestEvent.getRequest().getHeader("Cseq"));
//Kontakt Header
response.addHeader(contactHeader);
//Accept Header für application/sdp
AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
AllowEventsHeader allowevents = this.headerFactory.createAllowEventsHeader("talk,hold,refer,call-info");
response.addHeader((CallIdHeader)requestEvent.getRequest().getHeader("Call-ID"));
response.addHeader((CallIdHeader) requestEvent.getRequest().getHeader("Call-ID"));
response.addHeader(allow);
response.addHeader(allowevents);
this.sipProvider.sendResponse(response);
@@ -158,110 +155,160 @@ public class VoIP implements SipListenerExt
Logger.getLogger(VoIP.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if ((Request.INVITE).equals(requestEvent.getRequest().getMethod()))
//Aktive Kommunikation
else if ((Request.INVITE).equals(requestEvent.getRequest().getMethod())) // Invite von anderem Benutzer
{
System.out.println("Hab invitation bekommen");
System.out.println("Gleich antworte ich");
try
{
//Trying
Response responsetry = this.messageFactory.createResponse(100,requestEvent.getRequest());
responsetry.addHeader((ViaHeader)requestEvent.getRequest().getHeader("Via"));
responsetry.addHeader((FromHeader)requestEvent.getRequest().getHeader("From"));
responsetry.addHeader((ToHeader)requestEvent.getRequest().getHeader("To"));
responsetry.addHeader((CallIdHeader)requestEvent.getRequest().getHeader("Call-ID"));
responsetry.addHeader((CSeqHeader)requestEvent.getRequest().getHeader("Cseq"));
//Trying Message to Server (Response Trying)
System.out.println("----REquest HEader----");
Response responsetry = this.messageFactory.createResponse(100, requestEvent.getRequest());

responsetry.addHeader((ViaHeader) requestEvent.getRequest().getHeader("Via"));
responsetry.addHeader((FromHeader) requestEvent.getRequest().getHeader("From"));
responsetry.addHeader((ToHeader) requestEvent.getRequest().getHeader("To"));
responsetry.addHeader((CallIdHeader) requestEvent.getRequest().getHeader("Call-ID"));
responsetry.addHeader((CSeqHeader) requestEvent.getRequest().getHeader("Cseq"));

SupportedHeader supp = this.headerFactory.createSupportedHeader("replace,path,timer"); //nachschauen
AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
responsetry.addHeader(supp);
responsetry.addHeader(allow);
this.sipProvider.sendResponse(responsetry);
System.out.println("Trying ist raus");
//Ringing
Response responsering = this.messageFactory.createResponse(180,requestEvent.getRequest());
responsering.addHeader((ViaHeader)requestEvent.getRequest().getHeader("Via"));
responsering.addHeader((FromHeader)requestEvent.getRequest().getHeader("From"));
ToHeader temptoring = (ToHeader)requestEvent.getRequest().getHeader("To");
temptoring.setTag(String.valueOf(this.tag+Math.random())); //evtl. besseren Tag

//Ringing Message to Server (Responste Ringing)
Response responsering = this.messageFactory.createResponse(180, requestEvent.getRequest());
responsering.addHeader((ViaHeader) requestEvent.getRequest().getHeader("Via"));
responsering.addHeader((FromHeader) requestEvent.getRequest().getHeader("From"));

ToHeader temptoring = (ToHeader) requestEvent.getRequest().getHeader("To");
String tempTag = String.valueOf(this.tag + Math.random());
temptoring.setTag(tempTag); //evtl. besseren Tag
responsering.addHeader(temptoring);
responsering.addHeader((CSeqHeader)requestEvent.getRequest().getHeader("Cseq"));
responsering.addHeader((CSeqHeader) requestEvent.getRequest().getHeader("Cseq"));
responsering.addHeader(contactHeader);
responsering.addHeader(supp);
responsering.addHeader(allow);
AllowEventsHeader allowevent = this.headerFactory.createAllowEventsHeader("talk,hold");// für was gebraucht
responsering.addHeader(allowevent);
this.sipProvider.sendResponse(responsering);
System.out.println("Response ist raus");

System.out.println("Erstelle 200 Ok + SDP Header(contentHeader)");
//Jetzt kommt 200 Ok SIP Header + SDP (SIP/SDP)
//Sdp Header erstellen nächster Schritt
System.out.println(requestEvent.getDialog());
Response okandSDP = this.messageFactory.createResponse(Response.OK, requestEvent.getRequest());
//okandSDP.addHeader((ViaHeader) requestEvent.getRequest().getHeader("Via"));
okandSDP.addHeader((FromHeader) requestEvent.getRequest().getHeader("From"));

okandSDP.addHeader(temptoring); //evtl noch Ergänzungen

okandSDP.addHeader((CSeqHeader) requestEvent.getRequest().getHeader("Cseq"));
okandSDP.addHeader(contactHeader);
okandSDP.addHeader(supp);
okandSDP.addHeader(allow);
okandSDP.addHeader(allowevent);
ContentTypeHeader content = headerFactory.createContentTypeHeader("application", "sdp");
}
catch (ParseException | SipException ex)
//Create Session Description Body
SdpFactory sdpFactory = SdpFactory.getInstance();
SessionDescription sessionDescription = sdpFactory.createSessionDescription();
sessionDescription.setOrigin(sdpFactory.createOrigin(myName, 8000, 8000, "IN", "IP4", myIPAddress));
sessionDescription.setSessionName(sdpFactory.createSessionName("SIP Call"));
sessionDescription.setConnection(sdpFactory.createConnection("IN", "IP4", myIPAddress));
//Media Body
String sdpformat[] =
{
"0", "4", "18",
};
Vector mediavec = new Vector();
mediavec.add(sdpFactory.createMediaDescription("audio", 6022, 1, "RTP/AVP", sdpformat));
//Media Attribute
mediavec.add(sdpFactory.createAttribute("sendrecv", null));
mediavec.add(sdpFactory.createAttribute("rtpmap", "0 PCMU/8000"));
mediavec.add(sdpFactory.createAttribute("rtpmap", "4 G723/8000"));
mediavec.add(sdpFactory.createAttribute("rtpmap", "18 G729A/8000"));
mediavec.add(sdpFactory.createAttribute("ptime", "20"));
sessionDescription.setAttributes(mediavec);
System.out.println("---Beispiel Header---");
/*
String contentparam = "v=0\r\n" + "o=129 1202 1202" + " IN IP4 192.168.100.247\r\n" + "s=SIP Call\r\n"
+ "c=192.168.100.247\r\n" + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n"
+ "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n" +
"a=ptime:20\r\n";
*/
okandSDP.addHeader(content);
okandSDP.setContent(sessionDescription, content);
System.out.println(okandSDP);
this.sipProvider.sendResponse(okandSDP);
}
catch (ParseException | SipException | SdpException ex)
{
Logger.getLogger(VoIP.class.getName()).log(Level.SEVERE, null, ex);
}

}
else if ((Request.ACK).equals(requestEvent.getRequest().getMethod()))
{
}

}

@Override
public void processResponse(ResponseEvent responseEvent)
public void processResponse(ResponseEvent responseEvent)
{
System.out.println("---Response---");
//System.out.println(responseEvent.getResponse());
if (responseEvent.getResponse().getStatusCode() == Response.UNAUTHORIZED)

if (responseEvent.getResponse().getStatusCode() == Response.UNAUTHORIZED)
{

try
{
try {
Address addresstob = this.addressFactory.createAddress("sip:129@192.168.100.11"); //"sip:Jan@192.168.100.11"
ToHeader temp = (ToHeader)responseEvent.getResponse().getHeader("To");
ToHeader temp = (ToHeader) responseEvent.getResponse().getHeader("To");

ToHeader to = this.headerFactory.createToHeader(addresstob, temp.getTag()); // Integer.toString(tag) address + tag
URI requestURI = addresstob.getURI();
//CallIdHeader tempcall = (CallIdHeader)responseEvent.getResponse().getHeader("Call-ID");
CallIdHeader callId = (CallIdHeader)responseEvent.getResponse().getHeader("Call-ID");
CallIdHeader callId = (CallIdHeader) responseEvent.getResponse().getHeader("Call-ID");
FromHeader from = this.headerFactory.createFromHeader(addresstob, String.valueOf(this.tag)); //this.contactAddress
//set Authorization header evtl. user Name benutzen den es gibt
WWWAuthenticateHeader abc = (WWWAuthenticateHeader)responseEvent.getResponse().getHeader("WWW-Authenticate");
WWWAuthenticateHeader abc = (WWWAuthenticateHeader) responseEvent.getResponse().getHeader("WWW-Authenticate");
AuthorizationHeader auth = this.headerFactory.createAuthorizationHeader("Digest");//was kommt in den string
auth.setRealm(abc.getRealm());
auth.setNonce(abc.getNonce());
Address addresstest = this.addressFactory.createAddress("sip:"+myName+"@192.168.100.11"); //vielleicht passwort setzen "sip:"+myName+":\"\"@192.168.100.11"
Address addresstest = this.addressFactory.createAddress("sip:" + myName + "@192.168.100.11"); //vielleicht passwort setzen "sip:"+myName+":\"\"@192.168.100.11"
auth.setURI(addresstest.getURI());
auth.setUsername(myName);

ArrayList via = new ArrayList(); //Via needs a List as input
ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch+2));// String.valueOf(hashCode())string branch
ViaHeader viaheader = this.headerFactory.createViaHeader(this.myIPAddress, this.myPort, "UDP", String.valueOf(branch + 2));// String.valueOf(hashCode())string branch
via.add(viaheader);
CSeqHeader cSeq = this.headerFactory.createCSeqHeader(2L, "REGISTER");
MaxForwardsHeader maxForwards = this.headerFactory.createMaxForwardsHeader(70);
AllowHeader allow = this.headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
ExpiresHeader expire = this.headerFactory.createExpiresHeader(3600); //int value
//Zusätzliche Register Header nach Unauthorzied
//Create Request Header
System.out.println("----REQUEST HEADER2----");
Request request = this.messageFactory.createRequest(requestURI, "REGISTER", callId, cSeq, from, to, via, maxForwards);
@@ -272,10 +319,11 @@ public class VoIP implements SipListenerExt
//System.out.println(request);
this.sipProvider.sendRequest(request);
}
catch (ParseException | SipException |InvalidArgumentException ex) {
catch (ParseException | SipException | InvalidArgumentException ex)
{
Logger.getLogger(VoIP.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (responseEvent.getResponse().getStatusCode() == Response.OK)
{

Loading…
Cancel
Save