summaryrefslogtreecommitdiff
path: root/src/devreaderfactory.cpp
blob: c2fc656e95b3da16bcaf722cda81cd75d3d5ca93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/***************************************************************************
                             devreaderfactory.cpp
                             --------------------
    begin                : Fri Nov 16 2007
    copyright            : (C) 2007 - 2012 by Roland Riegel
    email                : feedback@roland-riegel.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "devreaderfactory.h"

#include "config.h"

#include "devreader.h"
#include "devreader-bsd.h"
#include "devreader-hpux.h"
#include "devreader-linux-proc.h"
#include "devreader-linux-sys.h"
#include "devreader-linux.h"
#include "devreader-solaris.h"

#include <string>
#include <list>
#include <map>

using namespace std;

map<string, DevReader*> DevReaderFactory::m_devReaders;
DevReaderFactory DevReaderFactory::m_instance;

DevReaderFactory::DevReaderFactory()
{
}

DevReaderFactory::~DevReaderFactory()
{
    for(map<string, DevReader*>::const_iterator it = m_devReaders.begin(); it != m_devReaders.end(); ++it)
        delete it->second;

    m_devReaders.clear();
}

int DevReaderFactory::findAllDevices()
{
#if defined HAVE_BSD
    list<string> interfaceNames = DevReaderBsd::findAllDevices();
#elif defined HAVE_HPUX
    list<string> interfaceNames = DevReaderHpux::findAllDevices();
#elif defined HAVE_LINUX
    list<string> interfaceNames = DevReaderLinux::findAllDevices();
#elif defined HAVE_SOLARIS
    list<string> interfaceNames = DevReaderSolaris::findAllDevices();
#endif

    map<string, DevReader*>::iterator devReaderIt = m_devReaders.begin();
    while(devReaderIt != m_devReaders.end())
    {
        list<string>::iterator interfaceIt = interfaceNames.begin();
        list<string>::iterator interfaceItEnd = interfaceNames.end();
        
        while(*interfaceIt != devReaderIt->first && interfaceIt != interfaceItEnd)
            ++interfaceIt;

        // delete all devices which disappeared
        if(interfaceIt == interfaceItEnd)
        {
            delete devReaderIt->second;
            m_devReaders.erase(devReaderIt++);
        }
        // delete all entries in the interface name list which we know of already
        else
        {
            interfaceNames.erase(interfaceIt);
            devReaderIt++;
        }
    }
    
    // the interface name list now contains only devices which just appeared in the system
    for(list<string>::const_iterator it = interfaceNames.begin(); it != interfaceNames.end(); ++it)
    {
        DevReader* newReader = createDevReader(*it);
        if(newReader)
            m_devReaders[*it] = newReader;
    }
    
    return m_devReaders.size();
}

int DevReaderFactory::getDeviceCount()
{
    return m_devReaders.size();
}

const map<string, DevReader*>& DevReaderFactory::getAllDevReaders()
{
    return m_devReaders;
}

DevReader* DevReaderFactory::createDevReader(const string& deviceName)
{
    DevReader* reader = 0;
    
#if defined HAVE_BSD
    reader = new DevReaderBsd(deviceName);
#elif defined HAVE_HPUX
    reader = new DevReaderHpux(deviceName);
#elif defined HAVE_LINUX
    if(DevReaderLinuxSys::isAvailable())
        reader = new DevReaderLinuxSys(deviceName);
    else if(DevReaderLinuxProc::isAvailable())
        reader = new DevReaderLinuxProc(deviceName);
    else
        reader = 0;
#elif defined HAVE_SOLARIS
    reader = new DevReaderSolaris(deviceName);
#endif

    return reader;
}