| 1 | /**************************************************************************** |
|---|
| 2 | |
|---|
| 3 | Copyright 2007 Virginia Polytechnic Institute and State University |
|---|
| 4 | |
|---|
| 5 | This file is part of the OSSIE c2. |
|---|
| 6 | |
|---|
| 7 | OSSIE c2 is free software; you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU General Public License as published by |
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 10 | (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | OSSIE c2 is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU General Public License |
|---|
| 18 | along with OSSIE c2; if not, write to the Free Software |
|---|
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 20 | |
|---|
| 21 | ****************************************************************************/ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #include <string> |
|---|
| 25 | #include <iostream> |
|---|
| 26 | #include "c2.h" |
|---|
| 27 | |
|---|
| 28 | c2_i::c2_i(const char *uuid, omni_condition *condition) : |
|---|
| 29 | Resource_impl(uuid), component_running(condition) |
|---|
| 30 | { |
|---|
| 31 | start(); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | c2_i::~c2_i(void) |
|---|
| 35 | { |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | // Static function for omni thread |
|---|
| 39 | void c2_i::Run( void * data ) |
|---|
| 40 | { |
|---|
| 41 | ((c2_i*)data)->ProcessData(); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | CORBA::Object_ptr c2_i::getPort( const char* portName ) throw ( |
|---|
| 45 | CORBA::SystemException, CF::PortSupplier::UnknownPort) |
|---|
| 46 | { |
|---|
| 47 | DEBUG(3, c2, "getPort() invoked with " << portName) |
|---|
| 48 | |
|---|
| 49 | CORBA::Object_var p; |
|---|
| 50 | |
|---|
| 51 | /*exception*/ |
|---|
| 52 | throw CF::PortSupplier::UnknownPort(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | void c2_i::start() throw (CORBA::SystemException, |
|---|
| 56 | CF::Resource::StartError) |
|---|
| 57 | { |
|---|
| 58 | DEBUG(3, c2, "start() invoked") |
|---|
| 59 | omni_mutex_lock l(processing_mutex); |
|---|
| 60 | if( false == thread_started ) |
|---|
| 61 | { |
|---|
| 62 | thread_started = true; |
|---|
| 63 | // Create the thread for the writer's processing function |
|---|
| 64 | processing_thread = new omni_thread(Run, (void *) this); |
|---|
| 65 | |
|---|
| 66 | // Start the thread containing the writer's processing function |
|---|
| 67 | processing_thread->start(); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void c2_i::stop() throw (CORBA::SystemException, CF::Resource::StopError) |
|---|
| 72 | { |
|---|
| 73 | DEBUG(3, c2, "stop() invoked") |
|---|
| 74 | omni_mutex_lock l(processing_mutex); |
|---|
| 75 | thread_started = false; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void c2_i::releaseObject() throw (CORBA::SystemException, |
|---|
| 79 | CF::LifeCycle::ReleaseError) |
|---|
| 80 | { |
|---|
| 81 | DEBUG(3, c2, "releaseObject() invoked") |
|---|
| 82 | |
|---|
| 83 | component_running->signal(); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | void c2_i::initialize() throw (CF::LifeCycle::InitializeError, |
|---|
| 87 | CORBA::SystemException) |
|---|
| 88 | { |
|---|
| 89 | DEBUG(3, c2, "initialize() invoked") |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void c2_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties) |
|---|
| 93 | { |
|---|
| 94 | if( configProperties.length() == 0 ) |
|---|
| 95 | { |
|---|
| 96 | configProperties.length( propertySet.length() ); |
|---|
| 97 | for( int i = 0; i < propertySet.length(); i++ ) |
|---|
| 98 | { |
|---|
| 99 | configProperties[i].id = CORBA::string_dup( propertySet[i].id ); |
|---|
| 100 | configProperties[i].value = propertySet[i].value; |
|---|
| 101 | } |
|---|
| 102 | return; |
|---|
| 103 | } else { |
|---|
| 104 | for( int i = 0; i < configProperties.length(); i++ ) { |
|---|
| 105 | for( int j = 0; j < propertySet.length(); j++ ) { |
|---|
| 106 | if( strcmp(configProperties[i].id, propertySet[j].id) == 0 ) { |
|---|
| 107 | configProperties[i].value = propertySet[j].value; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } // end if-else |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | void c2_i::configure(const CF::Properties& props) |
|---|
| 115 | throw (CORBA::SystemException, |
|---|
| 116 | CF::PropertySet::InvalidConfiguration, |
|---|
| 117 | CF::PropertySet::PartialConfiguration) |
|---|
| 118 | { |
|---|
| 119 | DEBUG(3, c2, "configure() invoked") |
|---|
| 120 | |
|---|
| 121 | static int init = 0; |
|---|
| 122 | if( init == 0 ) { |
|---|
| 123 | if( props.length() == 0 ) { |
|---|
| 124 | std::cout << "configure called with invalid props.length - " << props.length() << std::endl; |
|---|
| 125 | return; |
|---|
| 126 | } |
|---|
| 127 | propertySet.length(props.length()); |
|---|
| 128 | for( int j=0; j < props.length(); j++ ) { |
|---|
| 129 | propertySet[j].id = CORBA::string_dup(props[j].id); |
|---|
| 130 | propertySet[j].value = props[j].value; |
|---|
| 131 | } |
|---|
| 132 | init = 1; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | std::cout << "props length : " << props.length() << std::endl; |
|---|
| 136 | |
|---|
| 137 | for ( int i = 0; i <props.length(); i++) |
|---|
| 138 | { |
|---|
| 139 | std::cout << "Property id : " << props[i].id << std::endl; |
|---|
| 140 | |
|---|
| 141 | } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | void c2_i::ProcessData() |
|---|
| 145 | { |
|---|
| 146 | DEBUG(3, c2, "ProcessData() invoked") |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | while(continue_processing()) |
|---|
| 151 | { |
|---|
| 152 | /*insert code here to do work*/ |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | bool c2_i::continue_processing() |
|---|
| 163 | { |
|---|
| 164 | omni_mutex_lock l(processing_mutex); |
|---|
| 165 | return thread_started; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|