00001
00002
00003
00004
00005 #include <qstringlist.h>
00006 #include <string.h>
00007
00008 #include "dsmcccache.h"
00009 #include "dsmccbiop.h"
00010 #include "dsmccreceiver.h"
00011 #include "dsmcc.h"
00012
00013 #include "mythcontext.h"
00014
00030 DSMCCCache::DSMCCCache(Dsmcc *dsmcc)
00031 {
00032
00033 m_Dsmcc = dsmcc;
00034 }
00035
00036 DSMCCCache::~DSMCCCache()
00037 {
00038 QMap<DSMCCCacheReference, DSMCCCacheDir*>::Iterator dir;
00039 QMap<DSMCCCacheReference, DSMCCCacheFile*>::Iterator fil;
00040
00041 for (dir = m_Directories.begin(); dir != m_Directories.end(); ++dir)
00042 delete *dir;
00043
00044 for (dir = m_Gateways.begin(); dir != m_Gateways.end(); ++dir)
00045 delete *dir;
00046
00047 for (fil = m_Files.begin(); fil != m_Files.end(); ++fil)
00048 delete *fil;
00049 }
00050
00051
00052 QString DSMCCCacheKey::toString() const
00053 {
00054 QString result;
00055 for (uint i = 0; i < 4 && i < size(); i++)
00056 {
00057 int x = at(i);
00058 if (x < 16)
00059 result += QString("0%1").arg(x, 1, 16);
00060 else
00061 result += QString("%1").arg(x, 2, 16);
00062 }
00063
00064 return result;
00065 }
00066
00067
00068 bool operator < (const DSMCCCacheKey &key1, const DSMCCCacheKey &key2)
00069 {
00070 const char *data1 = key1.data();
00071 const char *data2 = key2.data();
00072 uint size1 = key1.size(), size2 = key2.size();
00073 uint size;
00074 if (size1 < size2)
00075 size = size1;
00076 else
00077 size = size2;
00078 int res = memcmp(data1, data2, size);
00079 if (res < 0)
00080 return true;
00081 else if (res > 0)
00082 return false;
00083
00084 return size1 < size2;
00085 }
00086
00087
00088
00089 bool DSMCCCacheReference::Equal(const DSMCCCacheReference &r) const
00090 {
00091 return m_nCarouselId == r.m_nCarouselId && m_nModuleId == r.m_nModuleId &&
00092 m_nStreamTag == r.m_nStreamTag && m_Key == r.m_Key;
00093 }
00094
00095 bool DSMCCCacheReference::Equal(const DSMCCCacheReference *p) const
00096 {
00097 return p != NULL && Equal(*p);
00098 }
00099
00100
00101
00102 QString DSMCCCacheReference::toString(void) const
00103 {
00104 return QString("%1-%2-%3-")
00105 .arg(m_nCarouselId).arg(m_nStreamTag)
00106 .arg(m_nModuleId) + m_Key.toString();
00107 }
00108
00109
00110 bool operator < (const DSMCCCacheReference &ref1,
00111 const DSMCCCacheReference &ref2)
00112 {
00113 if (ref1.m_nCarouselId < ref2.m_nCarouselId)
00114 return true;
00115 else if (ref1.m_nCarouselId > ref2.m_nCarouselId)
00116 return false;
00117 else if (ref1.m_nStreamTag < ref2.m_nStreamTag)
00118 return true;
00119 else if (ref1.m_nStreamTag > ref2.m_nStreamTag)
00120 return false;
00121 else if (ref1.m_nModuleId < ref2.m_nModuleId)
00122 return true;
00123 else if (ref1.m_nModuleId > ref2.m_nModuleId)
00124 return false;
00125 else if (ref1.m_Key < ref2.m_Key)
00126 return true;
00127
00128 return false;
00129 }
00130
00131
00132 DSMCCCacheDir *DSMCCCache::Srg(const DSMCCCacheReference &ref)
00133 {
00134
00135 QMap<DSMCCCacheReference, DSMCCCacheDir*>::Iterator dir =
00136 m_Gateways.find(ref);
00137
00138 if (dir != m_Gateways.end())
00139 {
00140 VERBOSE(VB_DSMCC, QString("[DSMCCCache] Already seen gateway %1")
00141 .arg(ref.toString()));
00142 return NULL;
00143 }
00144
00145 DSMCCCacheDir *pSrg = new DSMCCCacheDir(ref);
00146 m_Gateways.insert(ref, pSrg);
00147
00148 return pSrg;
00149 }
00150
00151
00152 DSMCCCacheDir *DSMCCCache::Directory(const DSMCCCacheReference &ref)
00153 {
00154
00155 QMap<DSMCCCacheReference, DSMCCCacheDir*>::Iterator dir =
00156 m_Directories.find(ref);
00157
00158 if (dir != m_Directories.end())
00159 {
00160 VERBOSE(VB_DSMCC, QString("[DSMCCCache] Already seen directory %1")
00161 .arg(ref.toString()));
00162 return NULL;
00163 }
00164
00165 DSMCCCacheDir *pDir = new DSMCCCacheDir(ref);
00166 m_Directories.insert(ref, pDir);
00167
00168 return pDir;
00169 }
00170
00171
00172 void DSMCCCache::CacheFileData(const DSMCCCacheReference &ref,
00173 const QByteArray &data)
00174 {
00175 DSMCCCacheFile *pFile;
00176
00177
00178 VERBOSE(VB_DSMCC,
00179 QString("[DSMCCCache] Adding file data size %1 for reference %2")
00180 .arg(data.size()).arg(ref.toString()));
00181
00182 QMap<DSMCCCacheReference, DSMCCCacheFile*>::Iterator fil =
00183 m_Files.find(ref);
00184
00185 if (fil == m_Files.end())
00186 {
00187 pFile = new DSMCCCacheFile(ref);
00188 m_Files.insert(ref, pFile);
00189 }
00190 else
00191 {
00192 pFile = *fil;
00193 }
00194
00195 pFile->m_Contents = data;
00196 }
00197
00198
00199 void DSMCCCache::AddFileInfo(DSMCCCacheDir *pDir, const BiopBinding *pBB)
00200 {
00201 QString name;
00202 name.setAscii(pBB->m_name.m_comps[0].m_id
00203 );
00204
00205 const DSMCCCacheReference *entry =
00206 pBB->m_ior.m_profile_body->GetReference();
00207
00208 pDir->m_Files.insert(name, *entry);
00209
00210 VERBOSE(VB_DSMCC,
00211 QString("[DSMCCCache] Adding file with name %1 reference %2")
00212 .arg(name.ascii()).arg(entry->toString()));
00213 }
00214
00215
00216 void DSMCCCache::AddDirInfo(DSMCCCacheDir *pDir, const BiopBinding *pBB)
00217 {
00218
00219 QString name;
00220 name.setAscii(pBB->m_name.m_comps[0].m_id
00221 );
00222 const DSMCCCacheReference *entry =
00223 pBB->m_ior.m_profile_body->GetReference();
00224
00225 pDir->m_SubDirectories.insert(name, *entry);
00226
00227 VERBOSE(VB_DSMCC,
00228 QString("[DSMCCCache] Adding directory with name %1 reference %2")
00229 .arg(name.ascii()).arg(entry->toString()));
00230 }
00231
00232
00233 DSMCCCacheFile *DSMCCCache::FindFileData(DSMCCCacheReference &ref)
00234 {
00235
00236 QMap<DSMCCCacheReference, DSMCCCacheFile*>::Iterator fil =
00237 m_Files.find(ref);
00238
00239 if (fil == m_Files.end())
00240 return NULL;
00241
00242 return *fil;
00243 }
00244
00245 DSMCCCacheDir *DSMCCCache::FindDir(DSMCCCacheReference &ref)
00246 {
00247
00248 QMap<DSMCCCacheReference, DSMCCCacheDir*>::Iterator dir =
00249 m_Directories.find(ref);
00250
00251 if (dir == m_Directories.end())
00252 return NULL;
00253
00254 return *dir;
00255 }
00256
00257 DSMCCCacheDir *DSMCCCache::FindGateway(DSMCCCacheReference &ref)
00258 {
00259
00260 QMap<DSMCCCacheReference, DSMCCCacheDir*>::Iterator dir =
00261 m_Gateways.find(ref);
00262
00263 if (dir == m_Gateways.end())
00264 return NULL;
00265
00266 return *dir;
00267 }
00268
00269
00270
00271
00272
00273
00274 int DSMCCCache::GetDSMObject(QStringList &objectPath, QByteArray &result)
00275 {
00276 DSMCCCacheDir *dir = FindGateway(m_GatewayRef);
00277 if (dir == NULL)
00278 return 1;
00279
00280 QStringList::Iterator it = objectPath.begin();
00281 while (it != objectPath.end())
00282 {
00283 QString name = *it;
00284 ++it;
00285 if (it == objectPath.end())
00286 {
00287 QMap<QString, DSMCCCacheReference>::Iterator ref =
00288 dir->m_Files.find(name);
00289
00290 if (ref == dir->m_Files.end())
00291 return -1;
00292
00293 DSMCCCacheFile *fil = FindFileData(*ref);
00294
00295 if (fil == NULL)
00296 return 1;
00297
00298 result = fil->m_Contents;
00299 return 0;
00300 }
00301 else
00302 {
00303 QMap<QString, DSMCCCacheReference>::Iterator ref =
00304 dir->m_SubDirectories.find(name);
00305
00306 if (ref == dir->m_SubDirectories.end())
00307 return -1;
00308
00309 dir = FindDir(*ref);
00310
00311 if (dir == NULL)
00312 return 1;
00313
00314 }
00315 }
00316
00317 return -1;
00318 }
00319
00320
00321 void DSMCCCache::SetGateway(const DSMCCCacheReference &ref)
00322 {
00323 VERBOSE(VB_DSMCC, QString("[DSMCCCache] Setting gateway to reference %1")
00324 .arg(ref.toString()));
00325
00326 m_GatewayRef = ref;
00327 }