Here is the code of the server, i'll try to make a wireshark trace later.
public class ChatroomSipServlet extends SipServlet {
public final static String USER_LIST = "userList";
public final static String CHATROOM_SERVER_NAME = "chatroomservername";
public String serverAddress;
public SipFactory factory;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
System.out.println("Chatroom sip servlet is gestart!");
try {
factory = (SipFactory) getServletContext().getAttribute("javax.servlet.sip.SipFactory");
System.out.println("Sip Factory: " + factory);
} catch (Exception e) {
throw new ServletException("Factory probleem!", e);
}
getServletContext().setAttribute(USER_LIST, new ArrayList<String>());
serverAddress = getServletConfig().getInitParameter(CHATROOM_SERVER_NAME);
System.out.println("serverAddress is: " + serverAddress);
}
@Override
public void destroy() {
try {
sendToAll(serverAddress, "Server sluit af!");
} catch (Throwable e) {
e.printStackTrace();
}
super.destroy();
}
protected void doMessage(SipServletRequest request) throws ServletException, IOException {
System.out.println(getDateTime() + " Bericht ontvangen");
request.createResponse(SipServletResponse.SC_OK).send();
String message = request.getContent().toString();
String from = ((SipURI) request.getFrom().getURI()).toString();
if (message.equalsIgnoreCase("/quit")) {
sendToUser(from, "Bye");
removeUser(from);
return;
}
if (!containsUser(from)) {
sendToUser(from, "Welkom in de chatroom. Typ '/quit' om af te sluiten.");
addUser(from);
}
if (message.equalsIgnoreCase("/who")) {
String users = "Lijst van de gebruikers:\n";
List<String> list = (List<String>) getServletContext().getAttribute(USER_LIST);
for (String user : list) {
users += user + "\n";
}
sendToUser(from, users);
return;
}
if (message.equalsIgnoreCase("/join")) {
return;
}
sendToAll(from, message);
}
protected void doErrorResponse(SipServletResponse response) throws ServletException, IOException {
// String receiver = response.getTo().toString();
String receiver = ((SipURI) response.getTo().getURI()).toString();
System.out.println(getDateTime() + " Errorresponse voor " + receiver);
removeUser(receiver);
}
protected void doSuccessResponse(SipServletResponse response) throws ServletException, IOException {
response.getApplicationSession().invalidate();
}
private void sendToAll(String from, String message) throws ServletParseException, IOException {
List<String> list = (List<String>) getServletContext().getAttribute(USER_LIST);
for (String user : list) {
SipApplicationSession session = factory.createApplicationSession();
System.out.println(getDateTime() + " Session created voor " + user);
SipServletRequest request = factory.createRequest(session, "MESSAGE", serverAddress, user);
String msg = from + " stuurt: \n" + message;
request.setContent(msg.getBytes(), "text/plain");
request.send();
}
}
private void sendToUser(String to, String message) throws ServletParseException, IOException {
SipApplicationSession session = factory.createApplicationSession();
SipServletRequest request = factory.createRequest(session, "MESSAGE", serverAddress, to);
request.setContent(message.getBytes(), "text/plain");
request.send();
}
private boolean containsUser(String from) {
List<String> list = (List<String>) getServletContext().getAttribute(USER_LIST);
return list.contains(from);
}
private void addUser(String from) {
List<String> list = (List<String>) getServletContext().getAttribute(USER_LIST);
list.add(from);
}
private void removeUser(String from) {
System.out.println(getDateTime() + " " + from + " wordt verwijderd uit de lijst.");
List<String> list = (List<String>) getServletContext().getAttribute(USER_LIST);
list.remove(from);
}
@Override
protected void doRegister(SipServletRequest req) throws ServletException, IOException {
System.out.println("Register request ontvangen: " + req.getTo());
int response = SipServletResponse.SC_OK;
SipServletResponse resp = req.createResponse(response);
resp.send();
}
private String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("[" + "HH:mm:ss" + "]");
Date date = new Date();
return dateFormat.format(date);
}
}
And the sip.xml
<sip-app>
<app-name>sip.chatvoorbeeld.ChatServer</app-name>
<display-name>Chatroom Sip Servlet</display-name>
<description>Chatroom Sip Servlet</description>
<servlet-selection>
<main-servlet>
ChatroomSipServlet
</main-servlet>
</servlet-selection>
<session-config>
<session-timeout>5</session-timeout>
</session-config>
<servlet>
<servlet-name>ChatroomSipServlet</servlet-name>
<display-name>ChatroomSipServlet</display-name>
<description>Chatroom SIP servlet</description>
<servlet-class>
sip.chatvoorbeeld.ChatroomSipServlet
</servlet-class>
<init-param>
<param-name>chatroomservername</param-name>
<param-value>sip:[email protected]:5060</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
</sip-app>
I also tried an empty servlet, just with a doMessage()
method that just prints out "message received". Same 32 seconds delay...
Wireshark gives me:
I send the message "test" to the server:
MESSAGE sip:[email protected]:5060;transport=udp SIP/2.0
Call-ID: [email protected]
CSeq: 1 MESSAGE
From: "bobby" <sip:[email protected]:5095>;tag=textclientv1.0
To: "chatserver" <sip:[email protected]:5060>
Via: SIP/2.0/UDP 192.168.56.101:5095;branch=branch1
Max-Forwards: 70
Contact: "bobby" <sip:[email protected]:5095>
Content-Type: text/plain
Content-Length: 4
test
Server sends back:
SIP/2.0 200 OK
Content-Length: 0
To: "chatserver"<sip:[email protected]:5060>;tag=g9vdns7u-e
Cseq: 1 MESSAGE
Via: SIP/2.0/UDP 192.168.56.101:5095;branch=branch1
From: "bobby"<sip:[email protected]:5095>;tag=textclientv1.0
Call-Id: [email protected]
Server: Glassfish_SIP_2.0.0
MESSAGE sip:[email protected]:5095 SIP/2.0
Max-Forwards: 69
Content-Length: 43
To: <sip:[email protected]:5095>
Cseq: 1 MESSAGE
Via: SIP/2.0/UDP 192.168.56.1:5060;branch=z9hG4bKdaacb7673c871796474ca951221a6643db6c
Content-Type: text/plain
Call-Id: 192.168.56.1_11_6595680936174578736
From: <sip:[email protected]:5060>;tag=g9vdns7u-g
sip:[email protected]:5095 stuurt:
test
Then my client again answers with an OK:
SIP/2.0 200 OK
To: <sip:[email protected]:5095>;tag=888
CSeq: 1 MESSAGE
Via: SIP/2.0/UDP 192.168.56.1:5060;branch=z9hG4bKdaacb7673c871796474ca951221a6643db6c;received=192.168.56.1
Call-ID: 192.168.56.1_11_6595680936174578736
From: <sip:[email protected]:5060>;tag=g9vdns7u-g
Content-Length: 0
So far so good, everything works well.
But now I send a second message to the server "test2" and I get this:
Client to server:
MESSAGE sip:[email protected]:5060;transport=udp SIP/2.0
Call-ID: [email protected]
CSeq: 1 MESSAGE
From: "bobby" <sip:[email protected]:5095>;tag=textclientv1.0
To: "chatserver" <sip:[email protected]:5060>
Via: SIP/2.0/UDP 192.168.56.101:5095;branch=branch1
Max-Forwards: 70
Contact: "bobby" <sip:[email protected]:5095>
Content-Type: text/plain
Content-Length: 5
test2
Then the server does respond with:
SIP/2.0 200 OK
Content-Length: 0
To: "chatserver"<sip:[email protected]:5060>;tag=g9vdns7u-e
Cseq: 1 MESSAGE
Via: SIP/2.0/UDP 192.168.56.101:5095;branch=branch1
From: "bobby"<sip:[email protected]:5095>;tag=textclientv1.0
Call-Id: [email protected]
Server: Glassfish_SIP_2.0.0
But then the communication stops... I get a 200 OK, but the println()
in my doMessage()
method is not passed.