xorp

xrl.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2001-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 #ifndef __LIBXIPC_XRL_HH__
00023 #define __LIBXIPC_XRL_HH__
00024 
00025 #include "libxorp/xorp.h"
00026 
00027 #include "libxorp/ref_ptr.hh"
00028 #include "xrl_pf.hh"  // Needed for ref_ptr instantiation of XrlPFSender
00029 
00030 #include "libxorp/exceptions.hh"
00031 #include "xrl_atom.hh"
00032 #include "xrl_args.hh"
00033 #include "xrl_tokens.hh"
00034 
00035 class XrlPFSender;
00036 
00040 class Xrl {
00041 public:
00045     Xrl(const string&   protocol,
00046     const string&   protocol_target,
00047     const string&   command,
00048     const XrlArgs&  args);
00049 
00053     Xrl(const string&   target,
00054     const string&   command,
00055     const XrlArgs&  args);
00056 
00060     Xrl(const string& protocol,
00061     const string& protocol_target,
00062     const string& command);
00063 
00067     Xrl(const string& target,
00068     const string& command);
00069 
00073     Xrl(const char* target,
00074         const char* command);
00075 
00079     Xrl(const char* xrl_c_str) throw (InvalidString);
00080 
00081     Xrl();
00082 
00083     ~Xrl();
00084 
00085     Xrl(const Xrl& xrl);
00086     Xrl& operator=(const Xrl& rhs);
00087 
00091     string str() const;
00092 
00096     const string& protocol() const      { return _protocol; }
00097 
00101     const string& target() const        { return _target; }
00102 
00106     const string& string_no_args() const;
00107 
00111     const string& command() const       { return _command; }
00112 
00116     XrlArgs& args()             { return *_argp; }
00117 
00121     const XrlArgs& args() const         { return *_argp; }
00122 
00128     bool operator==(const Xrl& x) const;
00129 
00133     bool is_resolved() const;
00134 
00138     size_t packed_bytes() const;
00139 
00149     size_t pack(uint8_t* buffer, size_t buffer_bytes) const;
00150 
00160     size_t unpack(const uint8_t* buffer, size_t buffer_bytes);
00161 
00162     void set_args(const Xrl& x) const;
00163 
00164     size_t fill(const uint8_t* buffer, size_t buffer_bytes);
00165 
00166     static size_t unpack_command(string& cmd, const uint8_t* in, size_t len);
00167 
00168     bool to_finder() const;
00169 
00170     bool resolved() const { return _resolved; }
00171     void set_resolved(bool r) const { _resolved = r; }
00172 
00173     ref_ptr<XrlPFSender> resolved_sender() const {
00174         return _resolved_sender;
00175     }
00176 
00177     void set_resolved_sender(ref_ptr<XrlPFSender>& s) const {
00178         _resolved_sender = s;
00179     }
00180 
00181     void set_target(const char* target);
00182 
00183 private:
00184     const char* parse_xrl_path(const char* xrl_path);
00185     void        clear_cache();
00186     void    copy(const Xrl& xrl);
00187 
00188 private:
00189     // if protocol != finder, target = protocol params
00190     string      _protocol;
00191     string      _target;
00192     string      _command;
00193 
00194     // XXX we got a const problem.  Factor out all cached stuff into a struct
00195     // and make that mutable.
00196     mutable XrlArgs         _args; // XXX packed_bytes() and pack()
00197     mutable string          _string_no_args;
00198     mutable XrlAtom*            _sna_atom;
00199     mutable size_t          _packed_bytes;
00200     mutable XrlArgs*            _argp; // XXX shouldn't be mutable
00201     mutable int             _to_finder;
00202     mutable bool            _resolved; // XXX ditto
00203     mutable ref_ptr<XrlPFSender> _resolved_sender; // XXX ditto
00204 
00205     static const string _finder_protocol;
00206 };
00207 
00208 typedef Xrl XrlTemplate;
00209 
00210 // ----------------------------------------------------------------------------
00211 // Inline methods
00212 
00213 inline const string&
00214 Xrl::string_no_args() const
00215 {
00216     if (!_string_no_args.size())
00217     _string_no_args =  _protocol 
00218              + string(XrlToken::PROTO_TGT_SEP) 
00219              + _target 
00220              + string(XrlToken::TGT_CMD_SEP)
00221              + _command;
00222 
00223     return _string_no_args;
00224 }
00225 
00226 #endif // __LIBXIPC_XRL_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations