~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to apps/clicktocall/test/clicktocallxml.cxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <rutil/Log.hxx>
 
2
#include <rutil/Logger.hxx>
 
3
#include <rutil/DnsUtil.hxx>
 
4
#include <rutil/BaseException.hxx>
 
5
#include <rutil/XMLCursor.hxx>
 
6
#include <rutil/WinLeakCheck.hxx>
 
7
 
 
8
using namespace resip;
 
9
using namespace std;
 
10
 
 
11
#define RESIPROCATE_SUBSYSTEM Subsystem::TEST
 
12
 
 
13
void sleepSeconds(unsigned int seconds)
 
14
{
 
15
#ifdef WIN32
 
16
   Sleep(seconds*1000);
 
17
#else
 
18
   sleep(seconds);
 
19
#endif
 
20
}
 
21
 
 
22
int 
 
23
main (int argc, char** argv)
 
24
{
 
25
#ifdef WIN32
 
26
   initNetwork();
 
27
#endif
 
28
 
 
29
   int sd, rc;
 
30
   struct sockaddr_in localAddr, servAddr;
 
31
   struct hostent *h;
 
32
 
 
33
   if(argc != 6) 
 
34
   {
 
35
      ErrLog(<< "usage: " << argv[0] <<" <server> <port> <initiator> <destination> <anchor 0|1>");
 
36
      exit(1);
 
37
   }
 
38
 
 
39
   h = gethostbyname(argv[1]);
 
40
   if(h==0) 
 
41
   {
 
42
      ErrLog(<< "unknown host " << argv[1]);
 
43
      exit(1);
 
44
   }
 
45
 
 
46
   servAddr.sin_family = h->h_addrtype;
 
47
   memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
 
48
   servAddr.sin_port = htons(atoi(argv[2]));
 
49
 
 
50
   // Create TCP Socket
 
51
   sd = socket(AF_INET, SOCK_STREAM, 0);
 
52
   if(sd < 0) 
 
53
   {
 
54
      ErrLog(<< "cannot open socket");
 
55
      exit(1);
 
56
   }
 
57
 
 
58
   // bind to any local interface/port
 
59
   localAddr.sin_family = AF_INET;
 
60
   localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
 
61
   localAddr.sin_port = 0;
 
62
 
 
63
   rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
 
64
   if(rc < 0) 
 
65
   {
 
66
      ErrLog(<<"error binding locally");
 
67
      exit(1);
 
68
   }
 
69
 
 
70
   // Connect to server
 
71
   rc = connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
 
72
   if(rc < 0) 
 
73
   {
 
74
      ErrLog(<< "error connecting");
 
75
      exit(1);
 
76
   }
 
77
 
 
78
   Data request(
 
79
      "<ClickToCall>\r\n"
 
80
      "  <Request>\r\n"
 
81
      "    <Initiator>" + Data(argv[3]) + "</Initiator>\r\n"
 
82
      "    <Destination>" + Data(argv[4]) + "</Destination>\r\n"
 
83
      "    <AnchorCall>" + Data(argv[5][0] == '0' ? "false" : "true") + "</AnchorCall>\r\n"
 
84
      "  </Request>\r\n"
 
85
      "</ClickToCall>\r\n");   
 
86
   rc = send(sd, request.c_str(), request.size(), 0);
 
87
   if(rc < 0) 
 
88
   {
 
89
      ErrLog(<< "error sending");
 
90
      close(sd);
 
91
      exit(1);
 
92
   }
 
93
 
 
94
   char readBuffer[8000];
 
95
   while(rc > 0)
 
96
   {
 
97
      rc = recv(sd, (char*)&readBuffer, sizeof(readBuffer), 0);
 
98
      if(rc < 0) 
 
99
      {
 
100
         ErrLog(<< "error receiving");
 
101
         close(sd);
 
102
         exit(1);
 
103
      }
 
104
 
 
105
      if(rc > 0)
 
106
      {
 
107
         Data response(Data::Borrow, (const char*)&readBuffer, rc);
 
108
         InfoLog(<< "Received response: \r\n" << response.c_str());
 
109
         ParseBuffer pb(response);
 
110
         XMLCursor xml(pb);
 
111
         if(xml.firstChild())
 
112
         {
 
113
            if(xml.nextSibling())
 
114
            {
 
115
               if(xml.firstChild())
 
116
               {
 
117
                  do
 
118
                  {
 
119
                     if(xml.getTag() == "Result")
 
120
                     {
 
121
                        XMLCursor::AttributeMap attribs = xml.getAttributes();
 
122
                        XMLCursor::AttributeMap::iterator it = attribs.find("Code");
 
123
                        if(it != attribs.end())
 
124
                        {
 
125
                           unsigned long statusCode = it->second.convertUnsignedLong();
 
126
                           if(statusCode >= 200 && statusCode < 300)
 
127
                           {
 
128
                              it = attribs.find("Leg");
 
129
                              if(it != attribs.end())
 
130
                              {
 
131
                                 if(it->second == "Destination")
 
132
                                 {
 
133
                                    // Successfully connected to destination - break out
 
134
                                    rc = 0; 
 
135
                                 }
 
136
                              }
 
137
                           }
 
138
                           else if(statusCode >=300)
 
139
                           {
 
140
                              // Failed - break out
 
141
                              rc = 0;
 
142
                           }
 
143
                        }
 
144
                        break;
 
145
                     }
 
146
                  } while(xml.nextSibling());
 
147
               }
 
148
            }
 
149
         }
 
150
      }
 
151
   }
 
152
 
 
153
   //sleepSeconds(5);
 
154
   InfoLog(<< "xmlrpcclient done.");
 
155
}
 
156
 
 
157
/* ====================================================================
 
158
 
 
159
 Copyright (c) 2009, SIP Spectrum, Inc.
 
160
 All rights reserved.
 
161
 
 
162
 Redistribution and use in source and binary forms, with or without
 
163
 modification, are permitted provided that the following conditions are 
 
164
 met:
 
165
 
 
166
 1. Redistributions of source code must retain the above copyright 
 
167
    notice, this list of conditions and the following disclaimer. 
 
168
 
 
169
 2. Redistributions in binary form must reproduce the above copyright
 
170
    notice, this list of conditions and the following disclaimer in the
 
171
    documentation and/or other materials provided with the distribution. 
 
172
 
 
173
 3. Neither the name of SIP Spectrum nor the names of its contributors 
 
174
    may be used to endorse or promote products derived from this 
 
175
    software without specific prior written permission. 
 
176
 
 
177
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 
178
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 
179
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 
180
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 
181
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 
182
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 
183
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 
184
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 
185
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 
186
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 
187
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
188
 
 
189
 ==================================================================== */
 
190