|
1 | 1 | /* |
2 | | - * Copyright (c) 2015 HARBRICK TECHNOLOGIES, INC |
| 2 | + * Copyright (c) 2015 PolySync |
3 | 3 | * |
4 | 4 | * |
5 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
22 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23 | 23 | * THE SOFTWARE. |
24 | 24 | */ |
25 | | - |
26 | | -/** |
27 | | - * \example LogSessionImport.cpp |
28 | | - * |
29 | | - * Log session import example. |
30 | | - * |
31 | | - * Demonstrates how to use the PolySync log session transfer API to import a previously |
32 | | - * exported log session to a new distributed system. |
33 | | - * |
34 | | - */ |
35 | | - |
36 | | -#include <iostream> |
37 | | - |
38 | | -#include <PolySyncCore.hpp> |
39 | | -#include <PolySyncDataModel.hpp> |
40 | 25 | #include <PolySyncApplication.hpp> |
41 | | -#include <PolySyncLogSessionTransfer.hpp> |
| 26 | +#include <PolySyncDataModel.hpp> |
42 | 27 |
|
| 28 | +#include "LogSessionImport.hpp" |
43 | 29 |
|
44 | | -using namespace std; |
45 | | -using namespace polysync; |
46 | | -using namespace datamodel; |
47 | 30 |
|
48 | | -/** |
49 | | - * @brief SessionImportExample class |
50 | | - */ |
51 | | -class SessionImportExample : public DataSubscriber |
| 31 | +SessionImportExample::SessionImportExample( const std::string & sessionPath ) |
| 32 | + : |
| 33 | + _sessionPath( sessionPath ), |
| 34 | + _importer() |
52 | 35 | { |
| 36 | + // Subscribe to ApplicationEventMessage to determine when |
| 37 | + // the application connects to the PolySync bus. |
| 38 | + connectSubscriberMethod< SessionImportExample >( |
| 39 | + polysync::ApplicationEventMessage::getName(), |
| 40 | + &SessionImportExample::handleEvent, |
| 41 | + this ); |
| 42 | + |
| 43 | + polysync::Application::getInstance()->attachSubscriber( this ); |
| 44 | +} |
53 | 45 |
|
54 | | -public: |
55 | 46 |
|
56 | | - SessionImportExample( int sessionId ) |
57 | | - : |
58 | | - _sessionId( sessionId ) |
| 47 | +void SessionImportExample::handleEvent( |
| 48 | + std::shared_ptr< polysync::Message > message ) |
| 49 | +{ |
| 50 | + if( auto event = polysync::datamodel::getSubclass< |
| 51 | + polysync::ApplicationEventMessage >( message ) ) |
59 | 52 | { |
60 | | - // Subscribe to ApplicationEventMessage to determine when |
61 | | - // the application connects to the PolySync bus. |
62 | | - connectSubscriberMethod< SessionImportExample >( |
63 | | - ApplicationEventMessage::getName(), |
64 | | - &SessionImportExample::handleEvent, |
65 | | - this ); |
66 | | - |
67 | | - _application = Application::getInstance(); |
68 | | - |
69 | | - _application->attachSubscriber( this ); |
70 | | - } |
71 | | - |
72 | | - |
73 | | -private: |
| 53 | + auto eventKind = event->getEventKind(); |
74 | 54 |
|
75 | | - void handleEvent( shared_ptr< Message > message ) |
76 | | - { |
77 | | - if( auto event = getSubclass< ApplicationEventMessage >( message ) ) |
| 55 | + if( eventKind == polysync::EventKind::Init ) |
78 | 56 | { |
79 | | - if( event->getEventKind() == EventKind::Init ) |
| 57 | + // This is the actual usage of the import API. |
| 58 | + _importer = std::unique_ptr< polysync::LogSessionImport >{ |
| 59 | + new polysync::LogSessionImport( _sessionPath ) }; |
| 60 | + |
| 61 | + // |
| 62 | + // Assign imported nodes to new destination host. |
| 63 | + // |
| 64 | + |
| 65 | + // Destination hosts are specified using 'LogSessionTransferTarget'. |
| 66 | + // The import API requires the transfer target to provide one of |
| 67 | + // the following: |
| 68 | + // * Interface Address String ("127.0.0.1") |
| 69 | + // * Manager GUID (562950866709306) |
| 70 | + // * Host's Id in the SDF |
| 71 | + polysync::LogSessionTransferTarget transferTarget; |
| 72 | + |
| 73 | + for( const auto & availableNode : _importer->getAvailableNodes() ) |
80 | 74 | { |
81 | | - // This is the actual usage of the import API. |
82 | | - _importer = new LogSessionImport( _sessionId ); |
83 | | - |
84 | | - // |
85 | | - // Assign imported nodes to new destination host. |
86 | | - // |
87 | | - |
88 | | - // Destination hosts are specified using 'LogSessionTransferTarget'. |
89 | | - // The import API requires the transfer target to provide one of the following: |
90 | | - // * Interface Address String ("127.0.0.1") |
91 | | - // * Manager GUID (562950866709306) |
92 | | - // * Host's Id in the SDF |
93 | | - LogSessionTransferTarget transferTarget; |
94 | | - |
95 | | - for( const auto & availableNode : _importer->getAvailableNodes() ) |
96 | | - { |
97 | | - cout << "assigning " << availableNode.getName() << " to 127.0.0.1" << endl; |
98 | | - |
99 | | - transferTarget.setInterfaceAddress("127.0.0.1"); |
100 | | - |
101 | | - _importer->updateNodeTarget( |
102 | | - transferTarget, |
103 | | - availableNode ); |
104 | | - } |
105 | | - |
106 | | - // Import can be started with or without a progress callback. |
107 | | - // When complete, results will be located at "PSYNC_HOME/rnr_logs/[sessionId]" |
108 | | - _importer->start( |
109 | | - this, |
110 | | - &SessionImportExample::handleTransferStatus ); |
111 | | - } |
112 | | - } |
113 | | - } |
| 75 | + std::cout << "assigning " |
| 76 | + << availableNode.getName() |
| 77 | + << " to 127.0.0.1" << std::endl; |
114 | 78 |
|
| 79 | + transferTarget.setInterfaceAddress("127.0.0.1"); |
115 | 80 |
|
116 | | - void handleTransferStatus( |
117 | | - const LogSessionTransferStatus & status ) |
118 | | - { |
119 | | - // Status has other information available as well |
120 | | - auto state = status.getState(); |
121 | | - |
122 | | - cout << "State: "; |
| 81 | + _importer->updateNodeTarget( |
| 82 | + transferTarget, |
| 83 | + availableNode ); |
| 84 | + } |
123 | 85 |
|
124 | | - switch( state ) |
125 | | - { |
126 | | - case LogSessionTransferState::Invalid : |
127 | | - cout << "Invalid" << endl; |
128 | | - break; |
129 | | - case LogSessionTransferState::Error : |
130 | | - cout << "Error" << endl; |
131 | | - break; |
132 | | - case LogSessionTransferState::Initial : |
133 | | - cout << "Initial" << endl; |
134 | | - break; |
135 | | - case LogSessionTransferState::Enumeration : |
136 | | - cout << "Enumeration" << endl; |
137 | | - break; |
138 | | - case LogSessionTransferState::TransferringSystemFiles : |
139 | | - cout << "TransferringSystemFiles" << endl; |
140 | | - break; |
141 | | - case LogSessionTransferState::TransformingSystemFile : |
142 | | - cout << "TransformingSystemFile" << endl; |
143 | | - break; |
144 | | - case LogSessionTransferState::TransferringLogfiles : |
145 | | - cout << "TransferringLogfiles" << endl; |
146 | | - break; |
147 | | - case LogSessionTransferState::Complete : |
148 | | - cout << "Complete" << endl; |
149 | | - _application->disconnectPolySync(); |
150 | | - break; |
| 86 | + // Import can be started with or without a progress callback. |
| 87 | + // When complete, results will be located at |
| 88 | + // "PSYNC_HOME/rnr_logs/[sessionId]" |
| 89 | + _importer->start( |
| 90 | + this, |
| 91 | + &SessionImportExample::handleTransferStatus ); |
151 | 92 | } |
152 | 93 | } |
153 | | - |
154 | | - int _sessionId; |
155 | | - |
156 | | - LogSessionImport * _importer; |
157 | | - |
158 | | - Application * _application; |
159 | | -}; |
| 94 | +} |
160 | 95 |
|
161 | 96 |
|
162 | | -int main( int argc, char ** argv ) |
| 97 | +void SessionImportExample::handleTransferStatus( |
| 98 | + const polysync::LogSessionTransferState state, |
| 99 | + const std::shared_ptr< polysync::datamodel::FileSyncStatusMessage > & ) |
163 | 100 | { |
164 | | - if( argc != 2 ) |
| 101 | + std::cout << "State: "; |
| 102 | + |
| 103 | + switch( state ) |
165 | 104 | { |
166 | | - cerr << "Usage: " << argv[ 0 ] << " [sessionId]" << endl; |
167 | | - return -1; |
| 105 | + case polysync::LogSessionTransferState::Invalid : |
| 106 | + std::cout << "Invalid" << std::endl; |
| 107 | + break; |
| 108 | + case polysync::LogSessionTransferState::Error : |
| 109 | + std::cout << "Error" << std::endl; |
| 110 | + break; |
| 111 | + case polysync::LogSessionTransferState::Initial : |
| 112 | + std::cout << "Initial" << std::endl; |
| 113 | + break; |
| 114 | + case polysync::LogSessionTransferState::Enumeration : |
| 115 | + std::cout << "Enumeration" << std::endl; |
| 116 | + break; |
| 117 | + case polysync::LogSessionTransferState::TransferringSystemFiles : |
| 118 | + std::cout << "TransferringSystemFiles" << std::endl; |
| 119 | + break; |
| 120 | + case polysync::LogSessionTransferState::TransformingSystemFile : |
| 121 | + std::cout << "TransformingSystemFile" << std::endl; |
| 122 | + break; |
| 123 | + case polysync::LogSessionTransferState::TransferringLogfiles : |
| 124 | + std::cout << "TransferringLogfiles" << std::endl; |
| 125 | + break; |
| 126 | + case polysync::LogSessionTransferState::Complete : |
| 127 | + std::cout << "Complete" << std::endl; |
| 128 | + polysync::Application::getInstance()->disconnectPolySync(); |
| 129 | + break; |
| 130 | + default: |
| 131 | + std::cout << "Unknown" << std::endl; |
168 | 132 | } |
169 | | - |
170 | | - SessionImportExample importExample{ atoi( argv[ 1 ] ) }; |
171 | | - |
172 | | - auto application = Application::getInstance(); |
173 | | - |
174 | | - application->setNodeName( "polysync-log-session-import-cpp" ); |
175 | | - |
176 | | - application->connectPolySync(); |
177 | 133 | } |
0 commit comments