diff options
author | Michi Henning <michi@zeroc.com> | 2008-04-10 15:15:12 +1000 |
---|---|---|
committer | Michi Henning <michi@zeroc.com> | 2008-04-10 15:15:12 +1000 |
commit | ff019fee471682420ef738c4fa2f30fd874993ac (patch) | |
tree | a58ab62c457972c77a41478ca968c78b7d874fdd /cpp/demo | |
parent | minor edits to Silverlight docs (diff) | |
download | ice-ff019fee471682420ef738c4fa2f30fd874993ac.tar.bz2 ice-ff019fee471682420ef738c4fa2f30fd874993ac.tar.xz ice-ff019fee471682420ef738c4fa2f30fd874993ac.zip |
Bug 2803.
Diffstat (limited to 'cpp/demo')
-rw-r--r-- | cpp/demo/book/freeze_filesystem/Client.cpp | 52 | ||||
-rw-r--r-- | cpp/demo/book/freeze_filesystem/Filesystem.ice | 22 | ||||
-rw-r--r-- | cpp/demo/book/freeze_filesystem/PersistentFilesystem.ice | 2 | ||||
-rw-r--r-- | cpp/demo/book/freeze_filesystem/PersistentFilesystemI.cpp | 141 | ||||
-rw-r--r-- | cpp/demo/book/freeze_filesystem/PersistentFilesystemI.h | 9 | ||||
-rw-r--r-- | cpp/demo/book/freeze_filesystem/Server.cpp | 11 | ||||
-rw-r--r-- | cpp/demo/book/lifecycle/FilesystemI.cpp | 78 | ||||
-rw-r--r-- | cpp/demo/book/lifecycle/FilesystemI.h | 11 | ||||
-rw-r--r-- | cpp/demo/book/lifecycle/Server.cpp | 3 | ||||
-rwxr-xr-x | cpp/demo/book/simple_filesystem/Client.cpp | 4 | ||||
-rwxr-xr-x | cpp/demo/book/simple_filesystem/FilesystemI.cpp | 61 | ||||
-rwxr-xr-x | cpp/demo/book/simple_filesystem/FilesystemI.h | 28 | ||||
-rwxr-xr-x | cpp/demo/book/simple_filesystem/Server.cpp | 18 |
13 files changed, 208 insertions, 232 deletions
diff --git a/cpp/demo/book/freeze_filesystem/Client.cpp b/cpp/demo/book/freeze_filesystem/Client.cpp index 53f360f38b3..f9ff7944e43 100644 --- a/cpp/demo/book/freeze_filesystem/Client.cpp +++ b/cpp/demo/book/freeze_filesystem/Client.cpp @@ -13,6 +13,31 @@ using namespace std; using namespace Filesystem; +// Recursively print the contents of directory "dir" in tree fashion. +// For files, show the contents of each file. The "depth" +// parameter is the current nesting level (for indentation). + +static void +listRecursive(const DirectoryPrx & dir, int depth = 0) +{ + string indent(++depth, '\t'); + + NodeDescSeq contents = dir->list(); + + for (NodeDescSeq::const_iterator i = contents.begin(); i != contents.end(); ++i) { + DirectoryPrx dir = DirectoryPrx::checkedCast(i->proxy); + FilePrx file = FilePrx::uncheckedCast(i->proxy); + cout << indent << i->name << (dir ? " (directory):" : " (file):") << endl; + if (dir) { + listRecursive(dir, depth); + } else { + Lines text = file->read(); + for (Lines::const_iterator j = text.begin(); j != text.end(); ++j) + cout << indent << "\t" << *j << endl; + } + } +} + class FilesystemClient : public Ice::Application { public: @@ -79,7 +104,7 @@ FilesystemClient::run(int argc, char* argv[]) } catch(const NameInUse&) { - NodeDesc desc = rootDir->resolve("Coleridge"); + NodeDesc desc = rootDir->find("Coleridge"); coleridge = DirectoryPrx::checkedCast(desc.proxy); assert(coleridge); } @@ -107,31 +132,16 @@ FilesystemClient::run(int argc, char* argv[]) } cout << "Contents of filesystem:" << endl; - NodeDict contents = rootDir->list(RecursiveList); - NodeDict::iterator p; - for(p = contents.begin(); p != contents.end(); ++p) - { - cout << " " << p->first << endl; - } - - NodeDesc desc = rootDir->resolve("Coleridge/Kubla_Khan"); - FilePrx file = FilePrx::checkedCast(desc.proxy); - assert(file); - Lines text = file->read(); - cout << "Contents of file Coleridge/Kubla_Khan:" << endl; - for(Lines::iterator i = text.begin(); i != text.end(); ++i) - { - cout << " " << *i << endl; - } + listRecursive(rootDir); // // Destroy the filesystem. // - contents = rootDir->list(NormalList); - for(p = contents.begin(); p != contents.end(); ++p) + NodeDescSeq contents = rootDir->list(); + for(NodeDescSeq::iterator i = contents.begin(); i != contents.end(); ++i) { - cout << "Destroying " << p->first << "..." << endl; - p->second.proxy->destroy(); + cout << "Destroying " << i->name << endl; + i->proxy->destroy(); } return EXIT_SUCCESS; diff --git a/cpp/demo/book/freeze_filesystem/Filesystem.ice b/cpp/demo/book/freeze_filesystem/Filesystem.ice index b8427f36525..b8b5b38d351 100644 --- a/cpp/demo/book/freeze_filesystem/Filesystem.ice +++ b/cpp/demo/book/freeze_filesystem/Filesystem.ice @@ -15,7 +15,6 @@ module Filesystem }; exception PermissionDenied extends GenericError {}; exception NameInUse extends GenericError {}; - exception IllegalName extends GenericError {}; exception NoSuchName extends GenericError {}; interface Node @@ -23,8 +22,7 @@ module Filesystem idempotent string name(); ["freeze:write"] - void destroy() - throws PermissionDenied; + void destroy() throws PermissionDenied; }; sequence<string> Lines; @@ -34,8 +32,7 @@ module Filesystem idempotent Lines read(); ["freeze:write"] - idempotent void write(Lines text) - throws GenericError; + idempotent void write(Lines text) throws GenericError; }; enum NodeType { DirType, FileType }; @@ -47,23 +44,18 @@ module Filesystem Node* proxy; }; - dictionary<string, NodeDesc> NodeDict; - - enum ListMode { NormalList, RecursiveList }; + sequence<NodeDesc> NodeDescSeq; interface Directory extends Node { - idempotent NodeDict list(ListMode mode); + idempotent NodeDescSeq list(); - idempotent NodeDesc resolve(string path) - throws NoSuchName; + idempotent NodeDesc find(string name) throws NoSuchName; ["freeze:write"] - File* createFile(string name) - throws NameInUse, IllegalName; + File* createFile(string name) throws NameInUse; ["freeze:write"] - Directory* createDirectory(string name) - throws NameInUse, IllegalName; + Directory* createDirectory(string name) throws NameInUse; }; }; diff --git a/cpp/demo/book/freeze_filesystem/PersistentFilesystem.ice b/cpp/demo/book/freeze_filesystem/PersistentFilesystem.ice index 774d2603eab..2895d2f5d14 100644 --- a/cpp/demo/book/freeze_filesystem/PersistentFilesystem.ice +++ b/cpp/demo/book/freeze_filesystem/PersistentFilesystem.ice @@ -24,6 +24,8 @@ module Filesystem Lines text; }; + dictionary<string, NodeDesc> NodeDict; + class PersistentDirectory extends PersistentNode implements Directory { ["freeze:write"] diff --git a/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.cpp b/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.cpp index 7bfe9e9f3e5..984a4c282e1 100644 --- a/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.cpp +++ b/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.cpp @@ -14,18 +14,17 @@ using namespace std; // // Filesystem::NodeI // -Ice::ObjectAdapterPtr Filesystem::NodeI::_adapter; Freeze::EvictorPtr Filesystem::NodeI::_evictor; Filesystem::NodeI::NodeI() #ifdef __SUNPRO_CC - : _ID(Ice::Identity()) + : _id(Ice::Identity()) #endif { } Filesystem::NodeI::NodeI(const Ice::Identity& id) - : _ID(id) + : _id(id) { } @@ -42,7 +41,7 @@ void Filesystem::FileI::destroy(const Ice::Current&) { parent->removeNode(nodeName); - _evictor->remove(_ID); + _evictor->remove(_id); } Filesystem::Lines @@ -72,24 +71,24 @@ Filesystem::FileI::FileI(const Ice::Identity& id) // Filesystem::DirectoryI // string -Filesystem::DirectoryI::name(const Ice::Current& current) +Filesystem::DirectoryI::name(const Ice::Current& c) { IceUtil::Mutex::Lock lock(*this); if(_destroyed) { - throw Ice::ObjectNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation); + throw Ice::ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } return nodeName; } void -Filesystem::DirectoryI::destroy(const Ice::Current& current) +Filesystem::DirectoryI::destroy(const Ice::Current& c) { if(!parent) { - throw Filesystem::PermissionDenied("cannot remove root directory"); + throw Filesystem::PermissionDenied("cannot destroy root directory"); } NodeDict children; @@ -99,7 +98,7 @@ Filesystem::DirectoryI::destroy(const Ice::Current& current) if(_destroyed) { - throw Ice::ObjectNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation); + throw Ice::ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } children = nodes; @@ -107,7 +106,7 @@ Filesystem::DirectoryI::destroy(const Ice::Current& current) } // - // We must iterate over the children outside of synchronization. + // We must iterate over the children outside the synchronization. // for(NodeDict::iterator p = children.begin(); p != children.end(); ++p) { @@ -117,107 +116,65 @@ Filesystem::DirectoryI::destroy(const Ice::Current& current) assert(nodes.empty()); parent->removeNode(nodeName); - _evictor->remove(_ID); + _evictor->remove(_id); } -Filesystem::NodeDict -Filesystem::DirectoryI::list(Filesystem::ListMode mode, const Ice::Current& current) +Filesystem::NodeDescSeq +Filesystem::DirectoryI::list(const Ice::Current& c) { - NodeDict result; - { - IceUtil::Mutex::Lock lock(*this); - - if(_destroyed) - { - throw Ice::ObjectNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation); - } + IceUtil::Mutex::Lock lock(*this); - result = nodes; + if(_destroyed) + { + throw Ice::ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } - if(mode == RecursiveList) + NodeDict::const_iterator p; + NodeDescSeq result; + for(p = nodes.begin(); p != nodes.end(); ++p) { - for(NodeDict::iterator p = result.begin(); p != result.end(); ++p) - { - if(p->second.type == DirType) - { - DirectoryPrx dir = DirectoryPrx::uncheckedCast(p->second.proxy); - NodeDict d = dir->list(mode); - for(NodeDict::iterator q = d.begin(); q != d.end(); ++q) - { - result[p->second.name + "/" + q->second.name] = q->second; - } - } - } + result.push_back(p->second); } - return result; } Filesystem::NodeDesc -Filesystem::DirectoryI::resolve(const string& path, const Ice::Current& current) +Filesystem::DirectoryI::find(const string& name, const Ice::Current& c) { - string::size_type pos = path.find('/'); - string child, remainder; - if(pos == string::npos) - { - child = path; - } - else - { - child = path.substr(0, pos); - pos = path.find_first_not_of("/", pos); - if(pos != string::npos) - { - remainder = path.substr(pos); - } - } - IceUtil::Mutex::Lock lock(*this); if(_destroyed) { - throw Ice::ObjectNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation); + throw Ice::ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } - NodeDict::iterator p = nodes.find(child); + NodeDict::iterator p = nodes.find(name); if(p == nodes.end()) { - throw NoSuchName("no node exists with name `" + child + "'"); - } - - if(remainder.empty()) - { - return p->second; - } - else - { - if(p->second.type != DirType) - { - throw NoSuchName("node `" + child + "' is not a directory"); - } - DirectoryPrx dir = DirectoryPrx::checkedCast(p->second.proxy); - assert(dir); - return dir->resolve(remainder); + throw NoSuchName(name); } + return p->second; } Filesystem::DirectoryPrx -Filesystem::DirectoryI::createDirectory(const string& name, const Ice::Current& current) +Filesystem::DirectoryI::createDirectory(const string& name, const Ice::Current& c) { IceUtil::Mutex::Lock lock(*this); if(_destroyed) { - throw Ice::ObjectNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation); + throw Ice::ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } - checkName(name); + if(name.empty() || nodes.find(name) != nodes.end()) + { + throw NameInUse(name); + } - Ice::Identity id = current.adapter->getCommunicator()->stringToIdentity(IceUtil::generateUUID()); + Ice::Identity id = c.adapter->getCommunicator()->stringToIdentity(IceUtil::generateUUID()); PersistentDirectoryPtr dir = new DirectoryI(id); dir->nodeName = name; - dir->parent = PersistentDirectoryPrx::uncheckedCast(current.adapter->createProxy(current.id)); + dir->parent = PersistentDirectoryPrx::uncheckedCast(c.adapter->createProxy(c.id)); DirectoryPrx proxy = DirectoryPrx::uncheckedCast(_evictor->add(dir, id)); NodeDesc nd; @@ -230,21 +187,24 @@ Filesystem::DirectoryI::createDirectory(const string& name, const Ice::Current& } Filesystem::FilePrx -Filesystem::DirectoryI::createFile(const string& name, const Ice::Current& current) +Filesystem::DirectoryI::createFile(const string& name, const Ice::Current& c) { IceUtil::Mutex::Lock lock(*this); if(_destroyed) { - throw Ice::ObjectNotExistException(__FILE__, __LINE__, current.id, current.facet, current.operation); + throw Ice::ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } - checkName(name); + if(name.empty() || nodes.find(name) != nodes.end()) + { + throw NameInUse(name); + } - Ice::Identity id = current.adapter->getCommunicator()->stringToIdentity(IceUtil::generateUUID()); + Ice::Identity id = c.adapter->getCommunicator()->stringToIdentity(IceUtil::generateUUID()); PersistentFilePtr file = new FileI(id); file->nodeName = name; - file->parent = PersistentDirectoryPrx::uncheckedCast(current.adapter->createProxy(current.id)); + file->parent = PersistentDirectoryPrx::uncheckedCast(c.adapter->createProxy(c.id)); FilePrx proxy = FilePrx::uncheckedCast(_evictor->add(file, id)); NodeDesc nd; @@ -276,23 +236,6 @@ Filesystem::DirectoryI::DirectoryI(const Ice::Identity& id) : { } -void -Filesystem::DirectoryI::checkName(const string& name) const -{ - if(name.empty() || name.find('/') != string::npos) - { - IllegalName e; - e.reason = "illegal name `" + name + "'"; - throw e; - } - - NodeDict::const_iterator p = nodes.find(name); - if(p != nodes.end()) - { - throw NameInUse("name `" + name + "' is already in use"); - } -} - // // Filesystem::NodeFactory // @@ -330,5 +273,5 @@ Filesystem::NodeInitializer::initialize(const Ice::ObjectAdapterPtr&, { NodeIPtr node = NodeIPtr::dynamicCast(obj); assert(node); - const_cast<Ice::Identity&>(node->_ID) = id; + const_cast<Ice::Identity&>(node->_id) = id; } diff --git a/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.h b/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.h index 4533f63ac48..bdbbea08374 100644 --- a/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.h +++ b/cpp/demo/book/freeze_filesystem/PersistentFilesystemI.h @@ -22,7 +22,6 @@ class NodeI : virtual public PersistentNode, { public: - static Ice::ObjectAdapterPtr _adapter; static Freeze::EvictorPtr _evictor; protected: @@ -32,7 +31,7 @@ protected: public: - const Ice::Identity _ID; + const Ice::Identity _id; }; typedef IceUtil::Handle<NodeI> NodeIPtr; @@ -59,8 +58,8 @@ public: virtual std::string name(const Ice::Current&); virtual void destroy(const Ice::Current&); - virtual NodeDict list(ListMode, const Ice::Current&); - virtual NodeDesc resolve(const std::string&, const Ice::Current&); + virtual NodeDescSeq list(const Ice::Current&); + virtual NodeDesc find(const std::string&, const Ice::Current&); virtual DirectoryPrx createDirectory(const std::string&, const Ice::Current&); virtual FilePrx createFile(const std::string&, const Ice::Current&); virtual void removeNode(const std::string&, const Ice::Current&); @@ -70,8 +69,6 @@ public: private: - void checkName(const std::string&) const; - bool _destroyed; }; diff --git a/cpp/demo/book/freeze_filesystem/Server.cpp b/cpp/demo/book/freeze_filesystem/Server.cpp index a67bb146c3e..86d7e349e3a 100644 --- a/cpp/demo/book/freeze_filesystem/Server.cpp +++ b/cpp/demo/book/freeze_filesystem/Server.cpp @@ -31,17 +31,18 @@ public: communicator()->addObjectFactory(factory, PersistentDirectory::ice_staticId()); // - // Create an object adapter (stored in the NodeI::_adapter static member). + // Create an object adapter. // - NodeI::_adapter = communicator()->createObjectAdapterWithEndpoints("FreezeFilesystem", "default -p 10000"); + Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints( + "FreezeFilesystem", "default -p 10000"); // // Create the Freeze evictor (stored in the NodeI::_evictor static member). // Freeze::ServantInitializerPtr init = new NodeInitializer; - NodeI::_evictor = Freeze::createBackgroundSaveEvictor(NodeI::_adapter, _envName, "evictorfs", init); + NodeI::_evictor = Freeze::createBackgroundSaveEvictor(adapter, _envName, "evictorfs", init); - NodeI::_adapter->addServantLocator(NodeI::_evictor, ""); + adapter->addServantLocator(NodeI::_evictor, ""); // // Create the root node if it doesn't exist. @@ -57,7 +58,7 @@ public: // // Ready to accept requests now. // - NodeI::_adapter->activate(); + adapter->activate(); // // Wait until we are done. diff --git a/cpp/demo/book/lifecycle/FilesystemI.cpp b/cpp/demo/book/lifecycle/FilesystemI.cpp index fa3ad899597..9d564dd9675 100644 --- a/cpp/demo/book/lifecycle/FilesystemI.cpp +++ b/cpp/demo/book/lifecycle/FilesystemI.cpp @@ -21,13 +21,13 @@ FilesystemI::DirectoryI::ReapMap FilesystemI::DirectoryI::_reapMap; // Slice Node::name() operation. std::string -FilesystemI::NodeI::name(const Current&) +FilesystemI::NodeI::name(const Current& c) { IceUtil::Mutex::Lock lock(_m); if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } return _name; @@ -41,21 +41,46 @@ FilesystemI::NodeI::id() const return _id; } +// Activate the servant and add it to the parent's contents map. + +ObjectPrx +FilesystemI::NodeI::activate(const ObjectAdapterPtr& a) +{ + ObjectPrx node = a->add(this, _id); + if(_parent) + { + _parent->addChild(_name, this); + } + return node; +} + +// NodeI constructor. + FilesystemI::NodeI::NodeI(const string& name, const DirectoryIPtr& parent) : _name(name), _parent(parent), _destroyed(false) { + // Create an identity. The root directory has the fixed identity "RootDir". + // + if(!parent) + { + _id.name = "RootDir"; + } + else + { + _id.name = IceUtil::generateUUID(); + } } // Slice File::read() operation. Lines -FilesystemI::FileI::read(const Current&) +FilesystemI::FileI::read(const Current& c) { IceUtil::Mutex::Lock lock(_m); if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } return _lines; @@ -64,13 +89,13 @@ FilesystemI::FileI::read(const Current&) // Slice File::write() operation. void -FilesystemI::FileI::write(const Lines& text, const Current&) +FilesystemI::FileI::write(const Lines& text, const Current& c) { IceUtil::Mutex::Lock lock(_m); if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } _lines = text; @@ -86,7 +111,7 @@ FilesystemI::FileI::destroy(const Current& c) if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } _destroyed = true; } @@ -99,12 +124,9 @@ FilesystemI::FileI::destroy(const Current& c) // FileI constructor. -FilesystemI::FileI::FileI(const ObjectAdapterPtr& a, const string& name, const DirectoryIPtr& parent) +FilesystemI::FileI::FileI(const string& name, const DirectoryIPtr& parent) : NodeI(name, parent) { - _id.name = IceUtil::generateUUID(); - parent->addChild(name, this); - a->add(this, _id); } // Slice Directory::list() operation. @@ -117,7 +139,7 @@ FilesystemI::DirectoryI::list(const Current& c) if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } } @@ -147,7 +169,7 @@ FilesystemI::DirectoryI::find(const string& name, const Current& c) if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } } @@ -179,7 +201,7 @@ FilesystemI::DirectoryI::createFile(const string& name, const Current& c) if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } } @@ -187,13 +209,13 @@ FilesystemI::DirectoryI::createFile(const string& name, const Current& c) reap(); - if(_contents.find(name) != _contents.end()) + if(name.empty() || _contents.find(name) != _contents.end()) { throw NameInUse(name); } - FileIPtr f = new FileI(c.adapter, name, this); - return FilePrx::uncheckedCast(c.adapter->createProxy(f->id())); + FileIPtr f = new FileI(name, this); + return FilePrx::uncheckedCast(f->activate(c.adapter)); } // Slice Directory::createDirectory() operation. @@ -206,7 +228,7 @@ FilesystemI::DirectoryI::createDirectory(const string& name, const Current& c) if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } } @@ -214,13 +236,13 @@ FilesystemI::DirectoryI::createDirectory(const string& name, const Current& c) reap(); - if(_contents.find(name) != _contents.end()) + if(name.empty() || _contents.find(name) != _contents.end()) { throw NameInUse(name); } - DirectoryIPtr d = new DirectoryI(c.adapter, name, this); - return DirectoryPrx::uncheckedCast(c.adapter->createProxy(d->id())); + DirectoryIPtr d = new DirectoryI(name, this); + return DirectoryPrx::uncheckedCast(d->activate(c.adapter)); } // Slice Directory::destroy() operation. @@ -238,7 +260,7 @@ FilesystemI::DirectoryI::destroy(const Current& c) if(_destroyed) { - throw ObjectNotExistException(__FILE__, __LINE__); + throw ObjectNotExistException(__FILE__, __LINE__, c.id, c.facet, c.operation); } IceUtil::StaticMutex::Lock lcLock(_lcMutex); @@ -257,19 +279,9 @@ FilesystemI::DirectoryI::destroy(const Current& c) // DirectoryI constructor. -FilesystemI::DirectoryI::DirectoryI(const ObjectAdapterPtr& a, const string& name, const DirectoryIPtr& parent) +FilesystemI::DirectoryI::DirectoryI(const string& name, const DirectoryIPtr& parent) : NodeI(name, parent) { - if(!parent) - { - _id.name = "RootDir"; - } - else - { - _id.name = IceUtil::generateUUID(); - _parent->addChild(name, this); - } - a->add(this, _id); } // Add the passed name-node pair to the _contents map. diff --git a/cpp/demo/book/lifecycle/FilesystemI.h b/cpp/demo/book/lifecycle/FilesystemI.h index 01e24b00ace..bf86be54f8f 100644 --- a/cpp/demo/book/lifecycle/FilesystemI.h +++ b/cpp/demo/book/lifecycle/FilesystemI.h @@ -23,10 +23,13 @@ namespace FilesystemI class NodeI : virtual public Filesystem::Node { public: + virtual std::string name(const Ice::Current&); Ice::Identity id() const; + Ice::ObjectPrx activate(const Ice::ObjectAdapterPtr&); protected: + NodeI(const ::std::string&, const DirectoryIPtr&); const ::std::string _name; @@ -41,13 +44,15 @@ namespace FilesystemI class FileI : virtual public Filesystem::File, virtual public NodeI { public: + virtual Filesystem::Lines read(const Ice::Current&); virtual void write(const Filesystem::Lines&, const Ice::Current&); virtual void destroy(const Ice::Current&); - FileI(const Ice::ObjectAdapterPtr&, const std::string&, const DirectoryIPtr&); + FileI(const std::string&, const DirectoryIPtr&); private: + Filesystem::Lines _lines; }; @@ -56,13 +61,14 @@ namespace FilesystemI class DirectoryI : virtual public NodeI, virtual public Filesystem::Directory { public: + virtual Filesystem::NodeDescSeq list(const Ice::Current&); virtual Filesystem::NodeDesc find(const std::string& name, const Ice::Current&); Filesystem::FilePrx createFile(const ::std::string&, const Ice::Current&); Filesystem::DirectoryPrx createDirectory(const ::std::string&, const Ice::Current&); virtual void destroy(const Ice::Current&); - DirectoryI(const Ice::ObjectAdapterPtr&, const std::string& = "/", const DirectoryIPtr& = 0); + DirectoryI(const std::string& = "/", const DirectoryIPtr& = 0); void addChild(const ::std::string&, const NodeIPtr&); void addReapEntry(const ::std::string&); @@ -70,6 +76,7 @@ namespace FilesystemI static IceUtil::StaticMutex _lcMutex; private: + typedef ::std::map< ::std::string, NodeIPtr> Contents; Contents _contents; diff --git a/cpp/demo/book/lifecycle/Server.cpp b/cpp/demo/book/lifecycle/Server.cpp index a30462fb127..ff99757a233 100644 --- a/cpp/demo/book/lifecycle/Server.cpp +++ b/cpp/demo/book/lifecycle/Server.cpp @@ -29,7 +29,8 @@ public: // Create the root directory. // - new DirectoryI(adapter); + DirectoryIPtr root = new DirectoryI(); + root->activate(adapter); // All objects are created, allow client requests now. // diff --git a/cpp/demo/book/simple_filesystem/Client.cpp b/cpp/demo/book/simple_filesystem/Client.cpp index c2a30428ba3..51c24e6951b 100755 --- a/cpp/demo/book/simple_filesystem/Client.cpp +++ b/cpp/demo/book/simple_filesystem/Client.cpp @@ -20,7 +20,7 @@ using namespace Filesystem; // parameter is the current nesting level (for indentation). static void -listRecursive(const DirectoryPrx & dir, int depth = 0) +listRecursive(const DirectoryPrx& dir, int depth = 0) { string indent(++depth, '\t'); @@ -41,7 +41,7 @@ listRecursive(const DirectoryPrx & dir, int depth = 0) } int -main(int argc, char * argv[]) +main(int argc, char* argv[]) { int status = 0; Ice::CommunicatorPtr ic; diff --git a/cpp/demo/book/simple_filesystem/FilesystemI.cpp b/cpp/demo/book/simple_filesystem/FilesystemI.cpp index 81839b84fe3..ea0dbbc5e4a 100755 --- a/cpp/demo/book/simple_filesystem/FilesystemI.cpp +++ b/cpp/demo/book/simple_filesystem/FilesystemI.cpp @@ -3,7 +3,7 @@ // Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved. // // This copy of Ice is licensed to you under the terms described in the -// ICE_LICENSE file included in this distribution. +// ICE_LICENSE file include, in this distribution. // // ********************************************************************** @@ -12,52 +12,55 @@ using namespace std; -Ice::ObjectAdapterPtr Filesystem::NodeI::_adapter; // static member - // Slice Node::name() operation std::string -Filesystem::NodeI::name(const Ice::Current &) +Filesystem::NodeI::name(const Ice::Current&) { return _name; } // NodeI constructor -Filesystem::NodeI::NodeI(const Ice::CommunicatorPtr & ic, const string & name, const DirectoryIPtr & parent) : +Filesystem::NodeI::NodeI(const Ice::CommunicatorPtr& communicator, const string& name, const DirectoryIPtr& parent) : _name(name), _parent(parent) { - // Create an identity. The parent has the fixed identity "RootDir" + // Create an identity. The root directory has the fixed identity "RootDir" // // COMPILERFIX: // - // The line below causes a "thread synchronization error" exception on HP-UX (64 bit only): + // The line below causes "thread synchronization error" exception on HP-UX (64-bit only): // - // Ice::Identity myID = Ice::stringToIdentity(parent ? IceUtil::generateUUID() : "RootDir"); + // _id = communicator->stringToIdentity(parent ? IceUtil::generateUUID() : "RootDir"); // // We've rearranged the code to avoid the problem. // - Ice::Identity myID; - if (parent) - myID = ic->stringToIdentity(IceUtil::generateUUID()); + if(parent) + { + _id = communicator->stringToIdentity(IceUtil::generateUUID()); + } else - myID = ic->stringToIdentity("RootDir"); + { + _id = communicator->stringToIdentity("RootDir"); + } +} - // Create a proxy for the new node and add it as a child to the parent - // - NodePrx thisNode = NodePrx::uncheckedCast(_adapter->createProxy(myID)); - if (parent) - parent->addChild(thisNode); +// NodeI activate() member function - // Activate the servant - // - _adapter->add(this, myID); +void +Filesystem::NodeI::activate(const Ice::ObjectAdapterPtr& a) +{ + NodePrx thisNode = NodePrx::uncheckedCast(a->add(this, _id)); + if(_parent) + { + _parent->addChild(thisNode); + } } // Slice File::read() operation Filesystem::Lines -Filesystem::FileI::read(const Ice::Current &) +Filesystem::FileI::read(const Ice::Current&) { return _lines; } @@ -65,31 +68,31 @@ Filesystem::FileI::read(const Ice::Current &) // Slice File::write() operation void -Filesystem::FileI::write(const Filesystem::Lines & text, - const Ice::Current &) +Filesystem::FileI::write(const Filesystem::Lines& text, const Ice::Current&) { _lines = text; } // FileI constructor -Filesystem::FileI::FileI(const Ice::CommunicatorPtr & ic, const string & name, const DirectoryIPtr & parent) : - NodeI(ic, name, parent) +Filesystem::FileI::FileI(const Ice::CommunicatorPtr& communicator, const string& name, const DirectoryIPtr& parent) : + NodeI(communicator, name, parent) { } // Slice Directory::list() operation Filesystem::NodeSeq -Filesystem::DirectoryI::list(const Ice::Current &) +Filesystem::DirectoryI::list(const Ice::Current& c) { return _contents; } // DirectoryI constructor -Filesystem::DirectoryI::DirectoryI(const Ice::CommunicatorPtr & ic, const string & name, const DirectoryIPtr & parent) : - NodeI(ic, name, parent) +Filesystem::DirectoryI::DirectoryI(const Ice::CommunicatorPtr& communicator, const string& name, + const DirectoryIPtr& parent) : + NodeI(communicator, name, parent) { } @@ -97,7 +100,7 @@ Filesystem::DirectoryI::DirectoryI(const Ice::CommunicatorPtr & ic, const string // itself to the _contents member of the parent void -Filesystem::DirectoryI::addChild(const NodePrx child) +Filesystem::DirectoryI::addChild(const NodePrx& child) { _contents.push_back(child); } diff --git a/cpp/demo/book/simple_filesystem/FilesystemI.h b/cpp/demo/book/simple_filesystem/FilesystemI.h index ea51a9eae45..226bd99ddec 100755 --- a/cpp/demo/book/simple_filesystem/FilesystemI.h +++ b/cpp/demo/book/simple_filesystem/FilesystemI.h @@ -14,38 +14,44 @@ #include <Filesystem.h> namespace Filesystem { + class DirectoryI; typedef IceUtil::Handle<DirectoryI> DirectoryIPtr; class NodeI : virtual public Node { public: - virtual std::string name(const Ice::Current &); - NodeI(const Ice::CommunicatorPtr &, const std::string &, const DirectoryIPtr & parent); - static Ice::ObjectAdapterPtr _adapter; + virtual std::string name(const Ice::Current&); + NodeI(const Ice::CommunicatorPtr&, const std::string&, const DirectoryIPtr&); + void activate(const Ice::ObjectAdapterPtr&); private: std::string _name; + Ice::Identity _id; DirectoryIPtr _parent; }; + typedef IceUtil::Handle<NodeI> NodeIPtr; + class FileI : virtual public File, virtual public NodeI { public: - virtual Lines read(const Ice::Current &); - virtual void write(const Lines &, - const Ice::Current &); - FileI(const Ice::CommunicatorPtr &, const std::string &, const DirectoryIPtr &); + virtual Lines read(const Ice::Current&); + virtual void write(const Lines&, + const Ice::Current& = Ice::Current()); + FileI(const Ice::CommunicatorPtr&, const std::string&, const DirectoryIPtr&); private: Lines _lines; }; + typedef IceUtil::Handle<FileI> FileIPtr; + class DirectoryI : virtual public Directory, virtual public NodeI { public: - virtual NodeSeq list(const Ice::Current &); - DirectoryI(const Ice::CommunicatorPtr &, const std::string &, const DirectoryIPtr &); - void addChild(NodePrx child); + virtual NodeSeq list(const Ice::Current&); + DirectoryI(const Ice::CommunicatorPtr&, const std::string&, const DirectoryIPtr&); + void addChild(const Filesystem::NodePrx&); private: - NodeSeq _contents; + Filesystem::NodeSeq _contents; }; } diff --git a/cpp/demo/book/simple_filesystem/Server.cpp b/cpp/demo/book/simple_filesystem/Server.cpp index 664ae8ecb4c..9a86a002d8e 100755 --- a/cpp/demo/book/simple_filesystem/Server.cpp +++ b/cpp/demo/book/simple_filesystem/Server.cpp @@ -7,6 +7,7 @@ // // ********************************************************************** +#include <IceUtil/UUID.h> #include <Ice/Ice.h> #include <FilesystemI.h> @@ -15,32 +16,34 @@ using namespace Filesystem; class FilesystemApp : virtual public Ice::Application { public: - virtual int run(int, char * []) { + virtual int run(int, char*[]) { // Terminate cleanly on receipt of a signal // shutdownOnInterrupt(); - // Create an object adapter (stored in the NodeI::_adapter - // static member) + // Create an object adapter. // - NodeI::_adapter = + Ice::ObjectAdapterPtr adapter = communicator()->createObjectAdapterWithEndpoints( "SimpleFilesystem", "default -p 10000"); // Create the root directory (with name "/" and no parent) // DirectoryIPtr root = new DirectoryI(communicator(), "/", 0); + root->activate(adapter); // Create a file called "README" in the root directory // - FilePtr file = new FileI(communicator(), "README", root); + FileIPtr file = new FileI(communicator(), "README", root); Lines text; text.push_back("This file system contains a collection of poetry."); file->write(text); + file->activate(adapter); // Create a directory called "Coleridge" in the root directory // DirectoryIPtr coleridge = new DirectoryI(communicator(), "Coleridge", root); + coleridge->activate(adapter); // Create a file called "Kubla_Khan" in the Coleridge directory // @@ -52,10 +55,11 @@ public: text.push_back("Through caverns measureless to man"); text.push_back("Down to a sunless sea."); file->write(text); + file->activate(adapter); // All objects are created, allow client requests now // - NodeI::_adapter->activate(); + adapter->activate(); // Wait until we are done // @@ -65,8 +69,6 @@ public: << ": received signal, shutting down" << endl; } - NodeI::_adapter = 0; - return 0; }; }; |