/[resiprocate]/main/resip/stack/test/Transceiver.cxx
ViewVC logotype

Annotation of /main/resip/stack/test/Transceiver.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1295 - (hide annotations) (download)
Mon Mar 24 23:25:36 2003 UTC (16 years, 10 months ago) by jason
Original Path: main/sip/resiprocate/test/Transceiver.cxx
File size: 3734 byte(s)
directory reorg to sip/resiprocate, etc.

1 derekm 676 #include <sys/time.h>
2     #include <sys/types.h>
3     #include <unistd.h>
4     #include <iostream>
5    
6     #include <sys/socket.h>
7     #include <netinet/in.h>
8     #include <arpa/inet.h>
9    
10     #include "Transceiver.hxx"
11 jason 1295 #include "resiprocate/os/Logger.hxx"
12     #include "resiprocate/os/DataStream.hxx"
13     #include "resiprocate/os/Timer.hxx"
14 derekm 676
15    
16     #define VOCAL_SUBSYSTEM Subsystem::SIP
17    
18     using namespace std;
19     using namespace Vocal2;
20     using namespace Loadgen;
21    
22     Transceiver::Transceiver(int port)
23     : mUdp("localhost", port, "default", mReceived)
24     {
25 fluffy 1057 mContactUri.host() = mUdp.hostName();
26 derekm 676 mContactUri.port() = mUdp.port();
27     mContactUri.param(p_transport) = Transport::toData(mUdp.transport());
28     }
29    
30     void
31     Transceiver::send(SipMessage& message)
32     {
33     Uri target;
34     if (message.isRequest())
35     {
36     if (message.header(h_Routes).size() && !message.header(h_Routes).front().exists(p_lr))
37     {
38     target = message.header(h_Routes).front().uri();
39     }
40     else
41     {
42     target = message.header(h_RequestLine).uri();
43     }
44     }
45     else if (message.isResponse())
46     {
47     assert (!message.header(h_Vias).empty());
48     const Via& via = message.header(h_Vias).front();
49    
50     // should look at via.transport()
51     target.param(p_transport) = Symbols::UDP; // !jf!
52     target.host() = via.sentHost();
53     target.port() = via.sentPort();
54    
55     if (via.exists(p_received))
56     {
57     target.host() = via.param(p_received);
58     }
59     if (via.exists(p_rport))
60     {
61 davidb 1087 target.port() = via.param(p_rport).port();
62 derekm 676 }
63     }
64     else
65     {
66     assert(0);
67     }
68    
69     // do a dns lookup !jf!
70     // should only do this once and store in the SipMessage (or somewhere)
71     DebugLog(<<"Trying to resolve target: " << target);
72     Resolver resolver(target);
73    
74     send(target, message);
75     }
76    
77    
78     void
79     Transceiver::send(const Resolver& target,
80     SipMessage& message)
81     {
82     DebugLog(<< message.header(h_Vias).size() );
83    
84     if (message.isRequest())
85     {
86     assert(!message.header(h_Vias).empty());
87     message.header(h_Vias).front().transport() = Transport::toData(mUdp.transport());
88 fluffy 1057 message.header(h_Vias).front().sentHost() = mUdp.hostName();
89 derekm 676 message.header(h_Vias).front().sentPort() = mUdp.port();
90     }
91    
92     Data& encoded = message.getEncoded();
93     DataStream strm(encoded);
94     message.encode(strm);
95     strm.flush();
96     mUdp.send(target.mNextHops.front(), encoded, "bogus");
97     mUdp.process(mFdset);
98     }
99    
100     SipMessage*
101     Transceiver::receive(int waitMs)
102     {
103     UInt64 startTime = Timer::getTimeMs();
104    
105     UInt64 currentTime = Timer::getTimeMs();
106    
107 fluffy 712 while( UInt64(waitMs) > currentTime - startTime)
108 derekm 676 {
109     int timeLeft = waitMs - (currentTime - startTime);
110     if (!mReceived.messageAvailable())
111     {
112     mUdp.buildFdSet(mFdset);
113    
114 derekm 1009 int err = mFdset.selectMilliSeconds(timeLeft);
115 derekm 676 int e = errno;
116     if ( err == -1 )
117     {
118     InfoLog(<< "Error " << e << " " << strerror(e) << " in select");
119     }
120    
121     DebugLog(<<"Calling process in Transceiver::receive");
122     mUdp.process(mFdset);
123     }
124    
125     if (mReceived.messageAvailable())
126     {
127     Message* msg = mReceived.getNext();
128     DebugLog(<<"Received a message in the transceiver, " << msg);
129    
130     SipMessage* next = dynamic_cast<SipMessage*>(msg);
131     DebugLog(<<"Dynamic cast resulted in: " << next);
132     if (next)
133     {
134     DebugLog(<<"Received a sip message in the transceiver.");
135     return next;
136     }
137     else
138     {
139     DebugLog(<<"Which was apparently not a sip message.");
140     delete msg;
141     }
142     }
143     currentTime = Timer::getTimeMs();
144     }
145     return 0;
146     }

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27