Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / p2p / base / rawtransport.cc
1 /*
2  * libjingle
3  * Copyright 2004--2005, Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without 
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice, 
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products 
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include <string>
29 #include <vector>
30 #include "talk/p2p/base/constants.h"
31 #include "talk/p2p/base/parsing.h"
32 #include "talk/p2p/base/rawtransport.h"
33 #include "talk/p2p/base/rawtransportchannel.h"
34 #include "talk/p2p/base/sessionmanager.h"
35 #include "webrtc/libjingle/xmllite/qname.h"
36 #include "webrtc/libjingle/xmllite/xmlelement.h"
37 #include "talk/xmpp/constants.h"
38 #include "webrtc/base/common.h"
39
40 #if defined(FEATURE_ENABLE_PSTN)
41 namespace cricket {
42
43 RawTransport::RawTransport(rtc::Thread* signaling_thread,
44                            rtc::Thread* worker_thread,
45                            const std::string& content_name,
46                            PortAllocator* allocator)
47     : Transport(signaling_thread, worker_thread,
48                 content_name, NS_GINGLE_RAW, allocator) {
49 }
50
51 RawTransport::~RawTransport() {
52   DestroyAllChannels();
53 }
54
55 bool RawTransport::ParseCandidates(SignalingProtocol protocol,
56                                    const buzz::XmlElement* elem,
57                                    const CandidateTranslator* translator,
58                                    Candidates* candidates,
59                                    ParseError* error) {
60   for (const buzz::XmlElement* cand_elem = elem->FirstElement();
61        cand_elem != NULL;
62        cand_elem = cand_elem->NextElement()) {
63     if (cand_elem->Name() == QN_GINGLE_RAW_CHANNEL) {
64       if (!cand_elem->HasAttr(buzz::QN_NAME)) {
65         return BadParse("no channel name given", error);
66       }
67       if (type() != cand_elem->Attr(buzz::QN_NAME)) {
68         return BadParse("channel named does not exist", error);
69       }
70       rtc::SocketAddress addr;
71       if (!ParseRawAddress(cand_elem, &addr, error))
72         return false;
73
74       Candidate candidate;
75       candidate.set_component(1);
76       candidate.set_address(addr);
77       candidates->push_back(candidate);
78     }
79   }
80   return true;
81 }
82
83 bool RawTransport::WriteCandidates(SignalingProtocol protocol,
84                                    const Candidates& candidates,
85                                    const CandidateTranslator* translator,
86                                    XmlElements* candidate_elems,
87                                    WriteError* error) {
88   for (std::vector<Candidate>::const_iterator
89        cand = candidates.begin();
90        cand != candidates.end();
91        ++cand) {
92     ASSERT(cand->component() == 1);
93     ASSERT(cand->protocol() == "udp");
94     rtc::SocketAddress addr = cand->address();
95
96     buzz::XmlElement* elem = new buzz::XmlElement(QN_GINGLE_RAW_CHANNEL);
97     elem->SetAttr(buzz::QN_NAME, type());
98     elem->SetAttr(QN_ADDRESS, addr.ipaddr().ToString());
99     elem->SetAttr(QN_PORT, addr.PortAsString());
100     candidate_elems->push_back(elem);
101   }
102   return true;
103 }
104
105 bool RawTransport::ParseRawAddress(const buzz::XmlElement* elem,
106                                    rtc::SocketAddress* addr,
107                                    ParseError* error) {
108   // Make sure the required attributes exist
109   if (!elem->HasAttr(QN_ADDRESS) ||
110       !elem->HasAttr(QN_PORT)) {
111     return BadParse("channel missing required attribute", error);
112   }
113
114   // Parse the address.
115   if (!ParseAddress(elem, QN_ADDRESS, QN_PORT, addr, error))
116     return false;
117
118   return true;
119 }
120
121 TransportChannelImpl* RawTransport::CreateTransportChannel(int component) {
122   return new RawTransportChannel(content_name(), component, this,
123                                  worker_thread(),
124                                  port_allocator());
125 }
126
127 void RawTransport::DestroyTransportChannel(TransportChannelImpl* channel) {
128   delete channel;
129 }
130
131 }  // namespace cricket
132 #endif  // defined(FEATURE_ENABLE_PSTN)