source: trunk/ETSI-Testsuites/ETSI_auto_IOT/adapter/src/Dispatcher/Dispatcher.cpp @ 55

Last change on this file since 55 was 22, checked in by rings, 14 years ago
  • Property svn:executable set to *
File size: 4.0 KB
Line 
1#include "Dispatcher.h"
2#include "DispatcherFactory.h"
3#include "DissectorFactory.h"
4#include "Logger/Logger.h"
5
6Dispatcher::Dispatcher(std::string sName)
7  :m_sName(sName) {
8}
9
10Dispatcher::~Dispatcher() {
11}
12
13const std::string& Dispatcher::GetName() const {
14  return m_sName;
15}
16
17bool Dispatcher::Dispatch(DispatchInfo & info, ComponentFilter & filter) {
18
19  // Dissect packet
20  Dissector * pDissector = DissectorFactory::Instance().Create(GetName());
21 
22  if(!pDissector) {
23    Logger::Instance().LogError("Unable to dispatch packet: No dissector for " + GetName());
24   
25    // Return what we have so far
26    return false;
27  }
28
29  DispatchInfo * pUnprocessed = 0;
30  ComponentFilter * pUnprocessedFilter = 0;
31  if (pDissector->CreatesUnprocessedData())
32  {
33          // create copies (for handling unprocessed data)
34          pUnprocessed = new DispatchInfo();
35          ProtocolInfo * pProtocolInfo = info.GetProtocolInfo();
36          if (pProtocolInfo)
37          {
38                int nCount = pProtocolInfo->Count();
39                for (int i = 0; i < nCount; i++)
40                        pUnprocessed->AddProtocolInfo((*pProtocolInfo)[i]->Clone());
41          }
42          pUnprocessedFilter = new ComponentFilter(filter);
43  }
44
45  if (!pDissector->Dissect(info.GetData(), info.GetDataSize())) // no information for higher levels
46  {
47          delete pDissector;
48          delete pUnprocessed;
49          delete pUnprocessedFilter;
50          return false;
51  }
52
53  // Reassembly
54  bool bRes = true;
55  if(pDissector->NeedReassembly()) {
56    ProtocolInfo * pProtocolInfo = info.GetProtocolInfo();
57    std::list<Dissector *>::iterator it;
58    for(it = m_fragments.begin(); (it != m_fragments.end()) &&
59                !((*it)->Reassemble(pDissector, pProtocolInfo)); ++it);
60    if(it == m_fragments.end()) {
61      if (!pDissector->FinishDissection())
62          {
63            // Fragment belongs to a new packet
64                info.DetachProtocolInfo();
65                pDissector->SetProtocolInfo(pProtocolInfo);
66                m_fragments.push_front(pDissector);
67                pDissector = 0;
68          }
69          // otherwise the fragment has been assembled
70    }
71    else {
72      // Fragment has been assembled. Pending packet may be complete
73      if(!(*it)->NeedReassembly()) {
74        pDissector = (*it);
75        m_fragments.erase(it);
76      }
77      else {
78        pDissector = 0;
79      }
80    }
81    if (pDissector && !pDissector->IsPacketValid()) {
82      delete pDissector;
83      pDissector = 0;
84      bRes = false;
85    }
86  }
87 
88  if(!pDissector) {
89    // Not complete packet or error...
90        delete pUnprocessed;
91        delete pUnprocessedFilter;
92    return bRes;
93  }
94
95  pDissector->SaveResult(info);
96  // Check filter
97  ProtocolInfoElement * pProtocol = pDissector->GetLayerInfo();
98  if (pProtocol)
99  {
100          if (!filter.Match(pProtocol))
101          {
102                  Logger::Instance().LogDebug("Message discarded by filter");
103                  delete pDissector;
104                  delete pUnprocessed;
105              delete pUnprocessedFilter;
106                  return false;
107          }
108          filter.MoveToNextLayer();
109  }
110
111  // Transfer to upper layer
112  const EProtocolType upperLayerType = pDissector->GetUpperLayerType();
113 
114  bRes = false;
115  if (upperLayerType == EProtocolType_None) // all headers have been removed
116  {
117          filter.EnqueueMessage(info.GetData(), info.GetDataSize());
118          std::stringstream ss;
119          ss << "Captured message enqueued to the TE (len " << info.GetDataSize() << ")";
120      Logger::Instance().LogInfo(ss.str());
121          bRes = true;
122  }
123  else
124  {
125        Dispatcher * pUpperLayer = DispatcherFactory::Instance().Get(upperLayerType);
126        if(pUpperLayer)
127                bRes = pUpperLayer->Dispatch(info, filter);
128  }
129  if (bRes && pDissector->ContainsUnprocessedData())
130  {
131        std::pair<const unsigned char *, ssize_t> res = pDissector->GetUnprocessedData();
132        pUnprocessed->SetData(res.first, res.second);
133    bRes = Dispatch(*pUnprocessed, *pUnprocessedFilter);
134  }
135
136  delete pDissector;
137  delete pUnprocessed;
138  delete pUnprocessedFilter;
139  return bRes;
140}
141
142void Dispatcher::AddExplicitUpperLayer(EProtocolType upperLayer/*, Filter */) {
143  Dispatcher * pUpperLayer = DispatcherFactory::Instance().Get(upperLayer);
144  if(pUpperLayer) {
145    // TODO: store Upper Layer Filter
146    m_upperLayers.push_back(pUpperLayer);
147  }
148  else {
149    Logger::Instance().LogWarning("No dispatcher for requested upper layer");
150  }
151}
Note: See TracBrowser for help on using the repository browser.