1 | // Connections.cpp: implementation of the CConnections class.
|
---|
2 | //
|
---|
3 | //////////////////////////////////////////////////////////////////////
|
---|
4 |
|
---|
5 | #include "stdafx.h"
|
---|
6 | #include "Connections.h"
|
---|
7 |
|
---|
8 | #ifdef _DEBUG
|
---|
9 | #undef THIS_FILE
|
---|
10 | static char THIS_FILE[]=__FILE__;
|
---|
11 | #define new DEBUG_NEW
|
---|
12 | #endif
|
---|
13 |
|
---|
14 |
|
---|
15 | IMPLEMENT_DYNAMIC( CConnections , CObject)
|
---|
16 |
|
---|
17 |
|
---|
18 | //////////////////////////////////////////////////////////////////////
|
---|
19 | // Construction/Destruction
|
---|
20 | //////////////////////////////////////////////////////////////////////
|
---|
21 |
|
---|
22 | CConnections::CConnections()
|
---|
23 | {
|
---|
24 |
|
---|
25 | }
|
---|
26 |
|
---|
27 | CConnections::~CConnections()
|
---|
28 | {
|
---|
29 | RemoveAll();
|
---|
30 | }
|
---|
31 |
|
---|
32 | CConnection* CConnections::Add(LPCTSTR sKey, WORD wType)
|
---|
33 | {
|
---|
34 |
|
---|
35 | CConnection* pConnection=NULL;
|
---|
36 |
|
---|
37 | if (Lookup(sKey,pConnection)) {
|
---|
38 |
|
---|
39 | ASSERT(FALSE);
|
---|
40 | return NULL;
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | pConnection = new CConnection(wType,sKey);
|
---|
45 |
|
---|
46 | SetAt(sKey,pConnection);
|
---|
47 |
|
---|
48 | return pConnection;
|
---|
49 | }
|
---|
50 |
|
---|
51 | void CConnections::Remove(LPCTSTR sKey)
|
---|
52 | {
|
---|
53 | CConnection* pConnection =NULL;
|
---|
54 |
|
---|
55 | if (Lookup(sKey,pConnection)) {
|
---|
56 |
|
---|
57 | pConnection->Delete();
|
---|
58 |
|
---|
59 | if (!RemoveKey(sKey)) {
|
---|
60 | ASSERT(FALSE);
|
---|
61 | }
|
---|
62 |
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | }
|
---|
67 |
|
---|
68 | void CConnections::RemoveAll()
|
---|
69 | {
|
---|
70 |
|
---|
71 | POSITION pos;
|
---|
72 | CString sKey;
|
---|
73 | pos = GetStartPosition();
|
---|
74 | CConnection* pConnection;
|
---|
75 |
|
---|
76 | while (NULL!=pos) {
|
---|
77 | GetNextAssoc(pos,sKey,pConnection);
|
---|
78 | Remove(sKey);
|
---|
79 | pos = GetStartPosition();
|
---|
80 | }
|
---|
81 |
|
---|
82 | }
|
---|
83 |
|
---|
84 | void CConnections::Serialize( CArchive& archive )
|
---|
85 | {
|
---|
86 | int iCount=0;
|
---|
87 | POSITION pos;
|
---|
88 | CString sKey;
|
---|
89 | CConnection* pConnection;
|
---|
90 | WORD wType;
|
---|
91 | int i=0;
|
---|
92 |
|
---|
93 | // call base class function first
|
---|
94 | // base class is CObject in this case
|
---|
95 | CObject::Serialize( archive );
|
---|
96 |
|
---|
97 |
|
---|
98 | // now do the stuff for our specific class
|
---|
99 | if( archive.IsStoring() ){
|
---|
100 | iCount=GetCount();
|
---|
101 | archive <<iCount;
|
---|
102 |
|
---|
103 | pos = GetStartPosition();
|
---|
104 |
|
---|
105 | while (NULL!=pos) {
|
---|
106 | GetNextAssoc(pos,sKey,pConnection);
|
---|
107 | //Serialize each connection
|
---|
108 | archive <<sKey<< pConnection->Type();
|
---|
109 | pConnection->Serialize(archive);
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 |
|
---|
114 | else {
|
---|
115 | archive >>iCount;
|
---|
116 |
|
---|
117 | for (i=0;i<iCount;i++){
|
---|
118 | archive >>sKey>>wType;
|
---|
119 | pConnection= Add(sKey,wType);
|
---|
120 | pConnection->Serialize(archive);
|
---|
121 | if (CConnection::AUTO==pConnection->Type()) {
|
---|
122 | if (!pConnection->AutoPtr()->m_pIModbusSrv) {
|
---|
123 | Remove(sKey);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | } |
---|