Appendix 1 contains an example program for managing an INVITE dialog. This program shows how to establish and terminate an INVITE dialog. An own registration is used as the registration context in this example.
public class InviteClientSide { private Form iForm = null; InviteClientSide(Form aForm) { iForm = aForm; } public void executeInvite() { try{ //This simple example program doesn't listen arriving //requests. SipConnectionNotifier scn = (SipConnectionNotifier) Connector.open( "sip:*;type=\"application/test\"" ); SipClientConnection regObj = null; regObj =(SipClientConnection) Connector.open("sip:myServer:5070"); regObj.initRequest("REGISTER",scn); regObj.setHeader("Contact","sip:UserA@" + scn.getLocalAddress() + ":" + scn.getLocalPort() + ";expires=3000"); regObj.setHeader("To","sip:UserA@myServer"); regObj.setHeader("From","sip:UserA@myServer"); regObj.send(); boolean flag = regObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(regObj.getStatusCode()!= 200) throw new RuntimeException("status code " + regObj.getStatusCode()); //Now value of the 'To' header has been stored to the //SipConnectionNotifier object. regObj.close(); SipClientConnection inviteObj = null; SipDialog sd = null; inviteObj = (SipClientConnection) Connector.open("sip:UserB@myServer:5070"); //Value of the 'From' header is retrieved from the //SipConnectionNotifier object. inviteObj.initRequest("INVITE",scn); inviteObj.setHeader("Accept-Contact","*;type=\"application/test\""); inviteObj.send(); flag = inviteObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(inviteObj.getStatusCode()!= 100) throw new RuntimeException("status code " + inviteObj.getStatusCode()); flag = inviteObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(inviteObj.getStatusCode()!= 180) throw new RuntimeException("status code " + inviteObj.getStatusCode()); flag = inviteObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(inviteObj.getStatusCode()!= 200) throw new RuntimeException("status code " + inviteObj.getStatusCode()); sd = inviteObj.getDialog(); inviteObj.initAck(); inviteObj.setHeader("Accept-Contact","*;type=\"application/test\""); inviteObj.send(); inviteObj.close(); //Do something in the dialog… Thread.sleep(2000); SipClientConnection byeObj = sd.getNewClientConnection("BYE"); byeObj.setHeader("Accept-Contact","*;type=\"application/test\""); byeObj.send(); flag = byeObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(byeObj.getStatusCode()!= 200) throw new RuntimeException("status code "+byeObj.getStatusCode()); inviteObj.close(); byeObj.close(); SipClientConnection deRegObj = null; deRegObj = (SipClientConnection) Connector.open("sip:myServer:5070"); deRegObj.initRequest("REGISTER",null); deRegObj.setHeader("Contact","sip:UserA@" + scn.getLocalAddress() + ":" + scn.getLocalPort() + ";expires=0"); deRegObj.setHeader("To","sip:UserA@myServer"); deRegObj.setHeader("From","sip:UserA@myServer"); deRegObj.send(); flag = deRegObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(deRegObj.getStatusCode()!= 200) throw new RuntimeException("status code " + deRegObj.getStatusCode()); deRegObj.close(); scn.close(); iForm.append("Invite managed ok!!!"); } catch(Exception e) { iForm.append("Exception occurred: " + e); } } }
public class InviteServerSide { private Form iForm = null; InviteServerSide(Form aForm) { iForm = aForm; } public void manageInvite() { try{ SipConnectionNotifier scn = (SipConnectionNotifier) Connector.open( "sip:*;type=\"application/test\"" ); //Registration have to be done to get requests. SipClientConnection regObj = null; regObj = (SipClientConnection) Connector.open("sip:172.22.70.226:5070"); regObj.initRequest("REGISTER",null); regObj.setHeader("Contact","sip:UserB@" + scn.getLocalAddress() + ":" + scn.getLocalPort() + ";expires=3000"); regObj.setHeader("To","sip:[email protected]"); regObj.setHeader("From","sip:[email protected]"); regObj.send(); boolean flag = regObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(regObj.getStatusCode()!= 200) throw new RuntimeException("status code " + regObj.getStatusCode()); regObj.close(); //Waiting a first request. SipServerConnection inviteSrvObj = scn.acceptAndOpen(); String method = inviteSrvObj.getMethod(); if(!method.equals("INVITE")) throw new RuntimeException("Unexpected request arrived #1: " + method); inviteSrvObj.initResponse(180); inviteSrvObj.send(); inviteSrvObj.initResponse(200); inviteSrvObj.send(); SipServerConnection ackSrvObj = scn.acceptAndOpen(); method = ackSrvObj.getMethod(); if(!method.equals("ACK")) throw new RuntimeException("Unexpected request arrived #2: " + method); inviteSrvObj.close(); SipServerConnection byeSrvObj = scn.acceptAndOpen(); method = byeSrvObj.getMethod(); if(!method.equals("BYE")) throw new RuntimeException("Unexpected request arrived #3: " + method); ackSrvObj.close(); byeSrvObj.initResponse(200); byeSrvObj.send(); byeSrvObj.close(); SipClientConnection deRegObj = null; deRegObj = (SipClientConnection) Connector.open("sip:172.22.70.226:5070"); deRegObj.initRequest("REGISTER",null); deRegObj.setHeader("Contact","sip:UserB@" + scn.getLocalAddress() + ":" + scn.getLocalPort() + ";expires=0"); deRegObj.setHeader("To","sip:[email protected]"); deRegObj.setHeader("From","sip:[email protected]"); deRegObj.send(); flag = deRegObj.receive(5000); if(false == flag) throw new RuntimeException("Response not arrived"); if(deRegObj.getStatusCode()!= 200) throw new RuntimeException("status code " + deRegObj.getStatusCode()); deRegObj.close(); scn.close(); iForm.append("Invite managed ok!!!"); } catch(Exception e) { iForm.append("Exception occurred: " + e); } } }