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