00001 #include "IOManagerIS.h"
00002 #include "IStreamIS.h"
00003 #include "OStreamIS.h"
00004 #include "IONameIS.h"
00005 #include "../Serializable.h"
00006 #include "../LogicErrors.h"
00007 #include "../SctNames.h"
00008 #include "../MultiMessageDebugStream.h"
00009
00010 #include <is/info.h>
00011 #include <is/infodictionary.h>
00012 #include <is/infoiterator.h>
00013 #include <is/inforeceiver.h>
00014
00015 namespace Sct {
00016 namespace IS {
00017
00018 namespace detail {
00019
00020 class ISAdapter : public ISInfo {
00021 public:
00022 ISAdapter(const string& className, const IOManagerIS& manager) :
00023 ISInfo(className.c_str()), manager(manager), writeOb(0), className(className) {}
00024
00025 ISAdapter(const Serializable& writeOb, const IOManagerIS& manager) :
00026 ISInfo(writeOb.getClassName().c_str()), manager(manager), writeOb(&writeOb) {}
00027
00028 virtual void refreshGuts(ISistream& inIS) {
00029 IStreamIS in(inIS);
00030 readOb = boost::dynamic_pointer_cast<Serializable>(manager.readImpl(in));
00031 }
00032
00033 virtual void publishGuts(ISostream& outIS) {
00034
00035
00036 if (dynamic_cast<ISType*>(&outIS)!=0 ) return;
00037 if (!writeOb) {
00038 #warning 'This needs to be revisited'
00039 } else {
00040 OStreamIS out(outIS);
00041 manager.writeImpl(out, *writeOb, true);
00042 };
00043 }
00044
00045 shared_ptr<Serializable> getOb() {
00046 return readOb;
00047 }
00048
00049 private:
00050 const IOManagerIS& manager;
00051 const Serializable* writeOb;
00052 shared_ptr<Serializable> readOb;
00053 string className;
00054 };
00055 }
00056
00057 using namespace detail;
00058
00059 IOManagerIS::IOManagerIS() throw() {}
00060
00061 IOManagerIS& IOManagerIS::instance() throw() {
00062 static IOManagerIS bertha;
00063 return bertha;
00064 }
00065
00066 void IOManagerIS::write(const Serializable& ob, const IOParams* params) const throw(LogicError, IoError) {
00067 IONameIS IOName(ob.getUniqueID(), ob.getClassName());
00068 if (params) {
00069 const IOParamsIS* p = dynamic_cast<const IOParamsIS*>(params);
00070 if (p) IOName.setServer(p->serverName);
00071 }
00072
00073 ISAdapter adapter(ob, *this);
00074
00075 ISInfoDictionary& id = SctNames::getISDictionary();
00076 ISInfo::Status result = id.insert(IOName.getIOName().c_str(), adapter);
00077
00078 if (result != ISInfo::Success) {
00079 throw IsException(result, "Error writing to IS server. Couldn't write: " + IOName.getIOName(), __FILE__, __LINE__);
00080 }
00081
00082 }
00083
00084 shared_ptr<Serializable> IOManagerIS::read(const string& name, const IOParams* params) const throw(LogicError, IoError) {
00085 IONameIS IOName(name);
00086 ISAdapter adapter(IOName.getClassName(), *this);
00087
00088 ISInfoDictionary& id = SctNames::getISDictionary();
00089 ISInfo::Status result = id.findValue(name.c_str(), adapter);
00090
00091 if (result != ISInfo::Success) {
00092 throw IsException(result, "Error reading from IS server. Couldn't read: " + name, __FILE__, __LINE__);
00093 }
00094
00095 return adapter.getOb();
00096 }
00097
00098 shared_ptr<Serializable> IOManagerIS::read(const ISCallbackInfo& info) const throw(LogicError, IoError) {
00099 IONameIS IOName(info.name());
00100 ISAdapter adapter(IOName.getClassName(), *this);
00101
00102 ISInfo::Status result = info.value(adapter);
00103
00104 if (result != ISInfo::Success) {
00105 throw IsException(result, string("Error reading from IS server. Couldn't read: ") + info.name(), __FILE__, __LINE__);
00106 }
00107
00108 return adapter.getOb();
00109 }
00110
00111 shared_ptr<Serializable> IOManagerIS::read(ISInfoIterator& it) const throw(LogicError, IoError) {
00112 IONameIS IOName(it.name());
00113 ISAdapter adapter(IOName.getClassName(), *this);
00114
00115 ISInfo::Status result = it.value(adapter);
00116
00117 if (result != ISInfo::Success) {
00118 throw IsException(result, string("Error reading from IS server. Couldn't read: ") + it.name(), __FILE__, __LINE__);
00119 }
00120
00121 return adapter.getOb();
00122 }
00123
00124 }
00125 }