<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Vidalia: Stream.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a> » <a class="el" href="dir_67a2ce25601334632bdc5ef4a72c0f17.html">torcontrol</a> </div> </div> <div class="contents"> <h1>Stream.cpp</h1><a href="_stream_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment">** This file is part of Vidalia, and is subject to the license terms in the</span> <a name="l00003"></a>00003 <span class="comment">** LICENSE file, found in the top level directory of this distribution. If </span> <a name="l00004"></a>00004 <span class="comment">** you did not receive the LICENSE file with this file, you may obtain it</span> <a name="l00005"></a>00005 <span class="comment">** from the Vidalia source package distributed by the Vidalia Project at</span> <a name="l00006"></a>00006 <span class="comment">** http://www.vidalia-project.net/. No part of Vidalia, including this file,</span> <a name="l00007"></a>00007 <span class="comment">** may be copied, modified, propagated, or distributed except according to</span> <a name="l00008"></a>00008 <span class="comment">** the terms described in the LICENSE file.</span> <a name="l00009"></a>00009 <span class="comment">*/</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">/* </span> <a name="l00012"></a>00012 <span class="comment">** \file Stream.cpp</span> <a name="l00013"></a>00013 <span class="comment">** \version $Id: Stream.cpp 3735 2009-04-28 20:28:01Z edmanm $</span> <a name="l00014"></a>00014 <span class="comment">** \brief Object representing a Tor stream</span> <a name="l00015"></a>00015 <span class="comment">*/</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#include "<a class="code" href="_stream_8h.html">Stream.h</a>"</span> <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="_circuit_8h.html">Circuit.h</a>"</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#include <QStringList></span> <a name="l00021"></a>00021 <a name="l00022"></a>00022 <span class="comment"></span> <a name="l00023"></a>00023 <span class="comment">/** Default constructor. */</span> <a name="l00024"></a><a class="code" href="class_stream.html#a8c3f05bd00361ec92627fa41f330a39b">00024</a> <a class="code" href="class_stream.html#a8c3f05bd00361ec92627fa41f330a39b">Stream::Stream</a>() <a name="l00025"></a>00025 { <a name="l00026"></a>00026 <a class="code" href="class_stream.html#aa3b0bd2844ee62354b4416156245f3d3">_status</a> = <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2aed01de5ee6660abc13d3050573b284bd">Unknown</a>; <a name="l00027"></a>00027 <a class="code" href="class_stream.html#af29712e2c1e1cd5d0b9a40fff0aa2cff">_port</a> = 0; <a name="l00028"></a>00028 } <a name="l00029"></a>00029 <span class="comment"></span> <a name="l00030"></a>00030 <span class="comment">/** Constructor */</span> <a name="l00031"></a><a class="code" href="class_stream.html#a37656b3fb11c16d9cb693ca33689a036">00031</a> <a class="code" href="class_stream.html#a8c3f05bd00361ec92627fa41f330a39b">Stream::Stream</a>(<span class="keyword">const</span> <a class="code" href="_stream_8h.html#a90d94d8a7b979b4c7e07f5869a431064">StreamId</a> &streamId, <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2">Status</a> status, <a name="l00032"></a>00032 <span class="keyword">const</span> <a class="code" href="_circuit_8h.html#a8b7b0182a6d1ff0aab7ab31df9c3f83d">CircuitId</a> &circuitId, <span class="keyword">const</span> QString &address, <a name="l00033"></a>00033 quint16 port) <a name="l00034"></a>00034 { <a name="l00035"></a>00035 <a class="code" href="class_stream.html#ab0991ad6af0b5b8938cf0e8b7862689a">_streamId</a> = streamId; <a name="l00036"></a>00036 <a class="code" href="class_stream.html#aa3b0bd2844ee62354b4416156245f3d3">_status</a> = status; <a name="l00037"></a>00037 <a class="code" href="class_stream.html#a3d91a042a0c586c8098f2bcd484ecfa6">_circuitId</a> = circuitId; <a name="l00038"></a>00038 <a class="code" href="class_stream.html#afa6dd3449b2be82e364574a2232e6be1">_address</a> = address; <a name="l00039"></a>00039 <a class="code" href="class_stream.html#af29712e2c1e1cd5d0b9a40fff0aa2cff">_port</a> = port; <a name="l00040"></a>00040 } <a name="l00041"></a>00041 <span class="comment"></span> <a name="l00042"></a>00042 <span class="comment">/** Constructor */</span> <a name="l00043"></a><a class="code" href="class_stream.html#ace77ad18cf50708c6dc27d5c703fe615">00043</a> <a class="code" href="class_stream.html#a8c3f05bd00361ec92627fa41f330a39b">Stream::Stream</a>(<span class="keyword">const</span> <a class="code" href="_stream_8h.html#a90d94d8a7b979b4c7e07f5869a431064">StreamId</a> &streamId, <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2">Status</a> status, <a name="l00044"></a>00044 <span class="keyword">const</span> <a class="code" href="_circuit_8h.html#a8b7b0182a6d1ff0aab7ab31df9c3f83d">CircuitId</a> &circuitId, <span class="keyword">const</span> QString &target) <a name="l00045"></a>00045 { <a name="l00046"></a>00046 <a class="code" href="class_stream.html#ab0991ad6af0b5b8938cf0e8b7862689a">_streamId</a> = streamId; <a name="l00047"></a>00047 <a class="code" href="class_stream.html#aa3b0bd2844ee62354b4416156245f3d3">_status</a> = status; <a name="l00048"></a>00048 <a class="code" href="class_stream.html#a3d91a042a0c586c8098f2bcd484ecfa6">_circuitId</a> = circuitId; <a name="l00049"></a>00049 <a class="code" href="class_stream.html#af29712e2c1e1cd5d0b9a40fff0aa2cff">_port</a> = 0; <a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="keywordtype">int</span> <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> = target.indexOf(<span class="stringliteral">":"</span>); <a name="l00052"></a>00052 <span class="keywordflow">if</span> (i >= 0) <a name="l00053"></a>00053 <a class="code" href="class_stream.html#afa6dd3449b2be82e364574a2232e6be1">_address</a> = target.mid(0, i); <a name="l00054"></a>00054 <span class="keywordflow">if</span> (i + 1 < target.length()) <a name="l00055"></a>00055 <a class="code" href="class_stream.html#af29712e2c1e1cd5d0b9a40fff0aa2cff">_port</a> = target.mid(i+1).toUInt(); <a name="l00056"></a>00056 } <a name="l00057"></a>00057 <span class="comment"></span> <a name="l00058"></a>00058 <span class="comment">/** Parses the given string for stream information, given in Tor control</span> <a name="l00059"></a>00059 <span class="comment"> * protocol format. The format is:</span> <a name="l00060"></a>00060 <span class="comment"> *</span> <a name="l00061"></a>00061 <span class="comment"> * StreamID SP StreamStatus SP CircID SP Target</span> <a name="l00062"></a>00062 <span class="comment"> */</span> <a name="l00063"></a>00063 <a class="code" href="class_stream.html">Stream</a> <a name="l00064"></a><a class="code" href="class_stream.html#aeaff90f7abf81399e207c480d1bee8a0">00064</a> <a class="code" href="class_stream.html#aeaff90f7abf81399e207c480d1bee8a0">Stream::fromString</a>(<span class="keyword">const</span> QString &stream) <a name="l00065"></a>00065 { <a name="l00066"></a>00066 QStringList parts = stream.split(<span class="stringliteral">" "</span>, QString::SkipEmptyParts); <a name="l00067"></a>00067 <span class="keywordflow">if</span> (parts.size() >= 4) { <a name="l00068"></a>00068 <span class="comment">/* Get the stream ID */</span> <a name="l00069"></a>00069 <a class="code" href="_stream_8h.html#a90d94d8a7b979b4c7e07f5869a431064">StreamId</a> streamId = parts.at(0); <a name="l00070"></a>00070 <span class="comment">/* Get the stream status value */</span> <a name="l00071"></a>00071 <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2">Stream::Status</a> <a class="code" href="class_stream.html#ae704c6dddc0196b8813de2769426fed6">status</a> = <a class="code" href="class_stream.html#a73c71589dc054b2d9fd6180c67c15ba1">Stream::toStatus</a>(parts.at(1)); <a name="l00072"></a>00072 <span class="comment">/* Get the ID of the circuit on which this stream travels */</span> <a name="l00073"></a>00073 <a class="code" href="_circuit_8h.html#a8b7b0182a6d1ff0aab7ab31df9c3f83d">CircuitId</a> circId = parts.at(2); <a name="l00074"></a>00074 <span class="comment">/* Get the target address for this stream */</span> <a name="l00075"></a>00075 QString <a class="code" href="class_stream.html#a81f58b982d1074cf8a5a2d70cbd32bdc">target</a> = parts.at(3); <a name="l00076"></a>00076 <a name="l00077"></a>00077 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#a8c3f05bd00361ec92627fa41f330a39b">Stream</a>(streamId, status, circId, target); <a name="l00078"></a>00078 } <a name="l00079"></a>00079 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#a8c3f05bd00361ec92627fa41f330a39b">Stream</a>(); <a name="l00080"></a>00080 } <a name="l00081"></a>00081 <span class="comment"></span> <a name="l00082"></a>00082 <span class="comment">/** Returns true iff <b>streamId</b> consists of only between 1 and 16</span> <a name="l00083"></a>00083 <span class="comment"> * (inclusive) ASCII-encoded letters and numbers. */</span> <a name="l00084"></a>00084 <span class="keywordtype">bool</span> <a name="l00085"></a><a class="code" href="class_stream.html#a9fa94da8bf9132d9d43a80aebe569393">00085</a> <a class="code" href="class_stream.html#a9fa94da8bf9132d9d43a80aebe569393">Stream::isValidStreamId</a>(<span class="keyword">const</span> <a class="code" href="_stream_8h.html#a90d94d8a7b979b4c7e07f5869a431064">StreamId</a> &streamId) <a name="l00086"></a>00086 { <a name="l00087"></a>00087 <span class="keywordtype">int</span> length = streamId.length(); <a name="l00088"></a>00088 <span class="keywordflow">if</span> (length < 1 || length > 16) <a name="l00089"></a>00089 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> = 0; <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a> < length; <a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a>++) { <a name="l00092"></a>00092 <span class="keywordtype">char</span> c = streamId[<a class="code" href="html_8cpp.html#a4a5dba6492ea149585950c59c210ff47">i</a>].toAscii(); <a name="l00093"></a>00093 <span class="keywordflow">if</span> (c < '0' && c > <span class="charliteral">'9'</span> && c < 'A' && c > <span class="charliteral">'Z'</span> && c < 'a' && c > <span class="charliteral">'z'</span>) <a name="l00094"></a>00094 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00095"></a>00095 } <a name="l00096"></a>00096 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00097"></a>00097 } <a name="l00098"></a>00098 <span class="comment"></span> <a name="l00099"></a>00099 <span class="comment">/** Converts a string description of a stream's status to its enum value */</span> <a name="l00100"></a>00100 <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2">Stream::Status</a> <a name="l00101"></a><a class="code" href="class_stream.html#a73c71589dc054b2d9fd6180c67c15ba1">00101</a> <a class="code" href="class_stream.html#a73c71589dc054b2d9fd6180c67c15ba1">Stream::toStatus</a>(<span class="keyword">const</span> QString &strStatus) <a name="l00102"></a>00102 { <a name="l00103"></a>00103 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"NEW"</span>, Qt::CaseInsensitive)) <a name="l00104"></a>00104 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a71ddfed4a525f68b10d609f5d50fc7ab">New</a>; <a name="l00105"></a>00105 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"NEWRESOLVE"</span>, Qt::CaseInsensitive)) <a name="l00106"></a>00106 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a0fd9f8a104f93dfb9448b25138dd56d9">NewResolve</a>; <a name="l00107"></a>00107 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"SENTCONNECT"</span>, Qt::CaseInsensitive)) <a name="l00108"></a>00108 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2ad63c4027dbb4be0bcb07f4fe4fda2c1f">SentConnect</a>; <a name="l00109"></a>00109 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"SENTRESOLVE"</span>, Qt::CaseInsensitive)) <a name="l00110"></a>00110 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a171a5190c3d0829d953beebcc2a62144">SentResolve</a>; <a name="l00111"></a>00111 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"SUCCEEDED"</span>, Qt::CaseInsensitive)) <a name="l00112"></a>00112 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2af02bc26726950e8fcd7a416da3f1159c">Succeeded</a>; <a name="l00113"></a>00113 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"FAILED"</span>, Qt::CaseInsensitive)) <a name="l00114"></a>00114 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a1435e25b3f7e41821a2a32821b4e1f17">Failed</a>; <a name="l00115"></a>00115 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"CLOSED"</span>, Qt::CaseInsensitive)) <a name="l00116"></a>00116 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2aac96f9b4c5acde5c2c1070f57b8447d4">Closed</a>; <a name="l00117"></a>00117 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"DETACHED"</span>, Qt::CaseInsensitive)) <a name="l00118"></a>00118 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a937bb3762d2e52394d3c985432641349">Detached</a>; <a name="l00119"></a>00119 <span class="keywordflow">if</span> (!strStatus.compare(<span class="stringliteral">"REMAP"</span>, Qt::CaseInsensitive)) <a name="l00120"></a>00120 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2ac4a12a9e22e9abf6e2320e62c700b97d">Remap</a>; <a name="l00121"></a>00121 <span class="keywordflow">return</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2aed01de5ee6660abc13d3050573b284bd">Unknown</a>; <a name="l00122"></a>00122 } <a name="l00123"></a>00123 <span class="comment"></span> <a name="l00124"></a>00124 <span class="comment">/** Returns a human-understandable string representation of this </span> <a name="l00125"></a>00125 <span class="comment"> * stream's status. */</span> <a name="l00126"></a>00126 QString <a name="l00127"></a><a class="code" href="class_stream.html#a7330c007d8ff77ce0a58be66e5c14591">00127</a> <a class="code" href="class_stream.html#a7330c007d8ff77ce0a58be66e5c14591">Stream::statusString</a>()<span class="keyword"> const</span> <a name="l00128"></a>00128 <span class="keyword"></span>{ <a name="l00129"></a>00129 QString <a class="code" href="class_stream.html#ae704c6dddc0196b8813de2769426fed6">status</a>; <a name="l00130"></a>00130 <span class="keywordflow">switch</span> (<a class="code" href="class_stream.html#aa3b0bd2844ee62354b4416156245f3d3">_status</a>) { <a name="l00131"></a>00131 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a71ddfed4a525f68b10d609f5d50fc7ab">New</a>: status = tr(<span class="stringliteral">"New"</span>); <span class="keywordflow">break</span>; <a name="l00132"></a>00132 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a0fd9f8a104f93dfb9448b25138dd56d9">NewResolve</a>: <a name="l00133"></a>00133 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a171a5190c3d0829d953beebcc2a62144">SentResolve</a>: status = tr(<span class="stringliteral">"Resolving"</span>); <span class="keywordflow">break</span>; <a name="l00134"></a>00134 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2ad63c4027dbb4be0bcb07f4fe4fda2c1f">SentConnect</a>: status = tr(<span class="stringliteral">"Connecting"</span>); <span class="keywordflow">break</span>; <a name="l00135"></a>00135 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2af02bc26726950e8fcd7a416da3f1159c">Succeeded</a>: status = tr(<span class="stringliteral">"Open"</span>); <span class="keywordflow">break</span>; <a name="l00136"></a>00136 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a1435e25b3f7e41821a2a32821b4e1f17">Failed</a>: status = tr(<span class="stringliteral">"Failed"</span>); <span class="keywordflow">break</span>; <a name="l00137"></a>00137 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2aac96f9b4c5acde5c2c1070f57b8447d4">Closed</a>: status = tr(<span class="stringliteral">"Closed"</span>); <span class="keywordflow">break</span>; <a name="l00138"></a>00138 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2a937bb3762d2e52394d3c985432641349">Detached</a>: status = tr(<span class="stringliteral">"Retrying"</span>); <span class="keywordflow">break</span>; <a name="l00139"></a>00139 <span class="keywordflow">case</span> <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2ac4a12a9e22e9abf6e2320e62c700b97d">Remap</a>: status = tr(<span class="stringliteral">"Remapped"</span>); <span class="keywordflow">break</span>; <a name="l00140"></a>00140 <span class="keywordflow">default</span>: status = tr(<span class="stringliteral">"Unknown"</span>); <span class="keywordflow">break</span>; <a name="l00141"></a>00141 } <a name="l00142"></a>00142 <span class="keywordflow">return</span> status; <a name="l00143"></a>00143 } <a name="l00144"></a>00144 <span class="comment"></span> <a name="l00145"></a>00145 <span class="comment">/** Returns true if all fields in this Stream object are valid. */</span> <a name="l00146"></a>00146 <span class="keywordtype">bool</span> <a name="l00147"></a><a class="code" href="class_stream.html#a347b15c4c22b5c84145433725c9637e3">00147</a> <a class="code" href="class_stream.html#a347b15c4c22b5c84145433725c9637e3">Stream::isValid</a>()<span class="keyword"> const</span> <a name="l00148"></a>00148 <span class="keyword"></span>{ <a name="l00149"></a>00149 <span class="keywordflow">return</span> (<a class="code" href="class_stream.html#a9fa94da8bf9132d9d43a80aebe569393">isValidStreamId</a>(<a class="code" href="class_stream.html#ab0991ad6af0b5b8938cf0e8b7862689a">_streamId</a>) <a name="l00150"></a>00150 && <a class="code" href="class_circuit.html#a440c821a5d795deb029848a064d9f3fd">Circuit::isValidCircuitId</a>(<a class="code" href="class_stream.html#a3d91a042a0c586c8098f2bcd484ecfa6">_circuitId</a>) <a name="l00151"></a>00151 && (<a class="code" href="class_stream.html#aa3b0bd2844ee62354b4416156245f3d3">_status</a> != <a class="code" href="class_stream.html#abe342e3ab850c3b79befd8073dd613c2aed01de5ee6660abc13d3050573b284bd">Unknown</a>) <a name="l00152"></a>00152 && !<a class="code" href="class_stream.html#afa6dd3449b2be82e364574a2232e6be1">_address</a>.isEmpty()); <a name="l00153"></a>00153 } <a name="l00154"></a>00154 </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:55 2010 for Vidalia by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>