[reSIProcate-users] How to code this simple SIP session?
Hi all,
I want to create a simple SIP client that will do the following
interactions with an existing SIP server:
client server
INVITE ============>
<=========== 200 OK
ACK ============>
<============ SUBSCRIBE
200 OK =============>
NOTIFY =============>
<============= 200 OK
BYE ==============>
<============== 200 OK
I tried the following code to start with the INVITE and the ACK. I get
"200 OK" for the INVITE but for the ACK, I am again getting the same
"200 OK" that I got for the INVITE. What did I do wrong?
static SipMessage* receive(SipStack& tu)
{
while (true)
{
Sleep(100);
SipMessage *msg = stack.receive();
if (msg)
{
std::cout << *msg << '\n';
return msg;
}
}
return 0;
}
int
main ()
{
NameAddr target;
target.uri().scheme() = "sip";
target.uri().host() = "192.168.114.211";
target.uri().port() = 5060;
target.uri().param(p_transport) = "tcp";
NameAddr from = target;
from.uri().user() = "me";
from.uri().port() = 5070;
std::auto_ptr<SipMessage> reg(Helper::makeInvite(target, from));
reg->header(h_Contacts).clear();
reg->header(h_Contacts).push_back(from);
reg->header(h_Vias).front().transport() = Data("tcp");
reg->header(h_Vias).front().sentHost() = "192.168.114.211";
reg->header(h_Vias).front().sentPort() = 5070;
Tokens tokens;
tokens.push_back(Token(Data("x-com-nice-imm-call-state")));
reg->header(h_AllowEvents) = tokens;
std::cout << *reg << '\n';
SipStack stack;
stack.addTransport(TCP, 5070);
StackThread stackThread(stack);
stackThread.run();
stack.send(*reg);
SipMessage* msg = receive(stack);
if (msg)
{
if (msg->isResponse())
{
std::cout << "REASON=" << msg->getReason() << "\n";
std::auto_ptr<SipMessage>
reg(Helper::makeRequest(target, from, ACK));
reg->header(h_Contacts).clear();
reg->header(h_Contacts).push_back(from);
reg->header(h_Vias).front().transport() = Data("tcp");
reg->header(h_Vias).front().sentHost() =
"192.168.114.211";
reg->header(h_Vias).front().sentPort() = 5070;
reg->header(h_CallID) = msg->header(h_CallId);
std::cout << *reg << '\n';
delete msg;
msg = 0;
stack.send(*reg);
}
else
{
std::cout << "MSG is not a response\n";
}
}
msg = receive(stack);
if (msg)
{
if (msg->isResponse())
{
std::cout << "REASON=" << msg->getReason() << "\n";
delete msg;
// std::auto_ptr<SipMessage>
reg(Helper::makeRequest(target, from, ACK));
}
else
{
std::cout << "MSG is not a response\n";
}
}
return 0;
}
Thanks in advance,
-- Vijay