Methods Summary |
---|
void | cleanup()Close all connections.
try {
if (sc != null) {
sc.close();
}
if (ssc != null) {
ssc.close();
}
if (scn != null) {
scn.close();
}
} catch (IOException ioe) {
assertNull("Unexpected IOException during cleanup", ioe);
ioe.printStackTrace();
}
|
boolean | processClientRequest()Process the client request at server and receive the message
boolean requestProcessed = false;
StringBuffer receivedMsgStr = new StringBuffer();
// Wait for the request to receive at server
synchronized (this) {
while (!serverEstablished) {
try {
wait(2000);
} catch (InterruptedException e) {
// Ignore interrupt
}
}
}
if (serverEstablished) {
try {
// what was the SIP method
String method = ssc.getMethod();
if (method.equals("MESSAGE")) {
// read the content of the MESSAGE
String contentType = ssc.getHeader("Content-Type");
if ((contentType != null) &&
contentType.equals("text/plain")) {
InputStream is = ssc.openContentInputStream();
int ch;
// read content
ch = is.read();
while ((ch = is.read()) != -1) {
receivedMsgStr.append(ch);
}
}
// initialize SIP 200 OK and send it back
ssc.initResponse(200);
ssc.send();
requestProcessed = true;
}
} catch (Exception ex) {
// handle Exceptions
ex.printStackTrace();
} finally {
}
}
return requestProcessed;
|
boolean | receiveMsgFromServer()Receive the message and extract status code at server
boolean receivedOKResponse = false;
try {
// wait maximum 15 seconds for response
boolean ok = sc.receive(15000);
if (ok) { // response received
if (sc.getStatusCode() == 200) {
receivedOKResponse = true;
}
}
} catch (Exception ex) {
// handle Exceptions
ex.printStackTrace();
} finally {
}
return receivedOKResponse;
|
public void | run()Accept a new connection request and process the request from client.
try {
// block and wait for incoming request.
// SipServerConnection is establised and returned
// when new request is received.
ssc = scn.acceptAndOpen();
assertNotNull("ssc is null", ssc);
serverEstablished = true;
} catch (SipException sipex) {
assertNull("Unexpected SipException", sipex);
sipex.printStackTrace();
cleanup();
} catch (IOException ioe) {
assertNull("Unexpected IOException", ioe);
ioe.printStackTrace();
cleanup();
} finally {
synchronized (this) {
notifyAll();
}
}
|
public void | runTests()Tests execute.
declare("Test TestSipCommunication on " + transport);
setup();
testSipCommunication();
cleanup();
assertTrue("OK", true); // temporary
transport = "tcp";
declare("Test TestSipCommunication on " + transport);
setup();
testSipCommunication();
cleanup();
assertTrue("OK", true); // temporary
|
boolean | sendMsgFromClient(java.lang.String msg)Send a SIP message from user agent client to user agent server
SipConnectionNotifier queues the incoming requests if the
instance of SipServerConnection is not available yet
if (sc != null) {
sc.initRequest("MESSAGE", null);
sc.setHeader("From", "sip:sippy.tester@"+scn.getLocalAddress());
sc.setHeader("Subject", "testing...");
// write message body
sc.setHeader("Content-Type", "text/plain");
sc.setHeader("Content-Length", Integer.toString(msg.length()));
OutputStream os = sc.openContentOutputStream();
os.write(msg.getBytes());
os.close(); // close stream and send the message
return true;
}
return false;
|
void | setup()Open the connection and start the thread to call the run
method. It waits until the InputStream is open before returning.
serverEstablished = false;
try {
// Open SIP server connection and listen to port 5060
scn = (SipConnectionNotifier)
Connector.open("sip:;transport="+transport);
} catch (Exception ex) {
assertNull("Exception during scn open", scn);
ex.printStackTrace();
}
assertNotNull("scn is null", scn);
try {
// Open SIP client connection to the local SIP server
sc = (SipClientConnection) Connector.open("sip:" +
scn.getLocalAddress() +
":" +
scn.getLocalPort() +
";transport=" +
transport);
} catch (Exception ex) {
assertNull("Exception during sc open", sc);
ex.printStackTrace();
cleanup();
}
assertNotNull("sc is null", sc);
// Launch a thread which would be blocked for scn.acceptAndOpen()
t1 = new Thread(this);
t1.start();
|
public void | testSipCommunication()Send request and receive a responce.
boolean result = false;
try {
result = sendMsgFromClient("Hello SIP-World");
} catch (SipException sipex) {
assertNull("Unexpected SipException", sipex);
sipex.printStackTrace();
} catch (IOException ioe) {
assertNull("Unexpected IOException", ioe);
ioe.printStackTrace();
}
if (result) {
result = processClientRequest();
}
if (result) {
result = receiveMsgFromServer();
}
assertTrue("SIP client is not communicating to server", result);
|