root/ossiedev/branches/jeongo9/components/readBytesfromFile/readBytesfromFile.cpp @ 10557

Revision 10557, 6.6 KB (checked in by jeongo9, 2 years ago)

checking in contributed component

  • Property svn:executable set to *
Line 
1/****************************************************************************
2Copyright 2007 Virginia Polytechnic Institute and State University
3This file is part of the OSSIE __COMP_NAME__.
4OSSIE __COMP_NAME__ is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7"(at your option) any later version
8OSSIE __COMP_NAME__ is distributed in the hope that it will be useful,
9but WITHOUT ANY WARRANTY; without even the implied warranty of
10MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11GNU General Public License for more details.
12You should have received a copy of the GNU General Public License
13along with OSSIE __COMP_NAME__; if not, write to the Free Software
14Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15****************************************************************************/
16
17#include <string>
18#include <iostream>
19#include <fcntl.h>
20#include <unistd.h>
21#include <stdio.h>
22
23#include "readBytesfromFile.h"
24
25#define INPUT_FILE "/sdr/input"
26
27readBytesfromFile_i::readBytesfromFile_i(const char *uuid, omni_condition *condition) :
28    Resource_impl(uuid), component_running(condition)
29{
30    dataOut_0 = new standardInterfaces_i::realChar_u("outputBits");
31    start();
32}
33
34readBytesfromFile_i::~readBytesfromFile_i(void)
35{
36    delete dataOut_0;
37}
38
39// Static function for omni thread
40void readBytesfromFile_i::Run( void * data )
41{
42    ((readBytesfromFile_i*)data)->ProcessData();
43}
44
45CORBA::Object_ptr readBytesfromFile_i::getPort( const char* portName ) throw (
46    CORBA::SystemException, CF::PortSupplier::UnknownPort)
47{
48    DEBUG(3, readBytesfromFile, "getPort() invoked with " << portName)
49
50    CORBA::Object_var p;
51
52    p = dataOut_0->getPort(portName);
53
54    if (!CORBA::is_nil(p))
55        return p._retn();
56
57    /*exception*/
58    throw CF::PortSupplier::UnknownPort();
59}
60
61void readBytesfromFile_i::start() throw (CORBA::SystemException,
62    CF::Resource::StartError)
63{
64    DEBUG(3, readBytesfromFile, "start() invoked")
65        omni_mutex_lock  l(processing_mutex);
66        if( false == thread_started )
67        {
68                thread_started = true;
69                // Create the thread for the writer's processing function
70                processing_thread = new omni_thread(Run, (void *) this);
71
72                // Start the thread containing the writer's processing function
73                processing_thread->start();
74
75                /// open destination file if not opened
76                if(!inputFileOpen){
77                        inputFD = open(INPUT_FILE, O_RDONLY);
78                        if(inputFD < 0){
79                                perror("\n Error during opening input");
80                                abort();
81                        }
82                        inputFileOpen = true;
83                }
84        }
85}
86
87void readBytesfromFile_i::stop() throw (CORBA::SystemException, CF::Resource::StopError)
88{
89    DEBUG(3, readBytesfromFile, "stop() invoked")
90        omni_mutex_lock l(processing_mutex);
91        thread_started = false;
92
93        /// close destination file if not opened
94        if(inputFileOpen){
95                close(inputFD);
96                inputFileOpen = false;
97        }
98}
99
100void readBytesfromFile_i::releaseObject() throw (CORBA::SystemException,
101    CF::LifeCycle::ReleaseError)
102{
103    DEBUG(3, readBytesfromFile, "releaseObject() invoked")
104
105    component_running->signal();
106}
107
108void readBytesfromFile_i::initialize() throw (CF::LifeCycle::InitializeError,
109    CORBA::SystemException)
110{
111    DEBUG(3, readBytesfromFile, "initialize() invoked")
112
113        inputFileOpen = false;
114}
115
116void readBytesfromFile_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties)
117{
118        if( configProperties.length() == 0 )
119        {
120                configProperties.length( propertySet.length() );
121                for( int i = 0; i < propertySet.length(); i++ )
122                {
123                        configProperties[i].id = CORBA::string_dup( propertySet[i].id );
124                        configProperties[i].value = propertySet[i].value;
125                }
126                return;
127        } else {
128                for( int i = 0; i < configProperties.length(); i++ ) {
129                        for( int j = 0; j < propertySet.length(); j++ ) {
130                                if( strcmp(configProperties[i].id, propertySet[j].id) == 0 ) {
131                                        configProperties[i].value = propertySet[j].value;
132                                }
133                        }
134                }
135        } // end if-else
136}
137
138void readBytesfromFile_i::configure(const CF::Properties& props)
139throw (CORBA::SystemException,
140    CF::PropertySet::InvalidConfiguration,
141    CF::PropertySet::PartialConfiguration)
142{
143    DEBUG(3, readBytesfromFile, "configure() invoked")
144
145    static int init = 0;
146    if( init == 0 ) {
147        if( props.length() == 0 ) {
148            std::cout << "configure called with invalid props.length - " << props.length() << std::endl;
149            return;
150        }
151        propertySet.length(props.length());
152        for( int j=0; j < props.length(); j++ ) {
153            propertySet[j].id = CORBA::string_dup(props[j].id);
154            propertySet[j].value = props[j].value;
155        }
156        init = 1;
157    }
158
159    std::cout << "props length : " << props.length() << std::endl;
160
161    for ( int i = 0; i <props.length(); i++)
162    {
163        std::cout << "Property id : " << props[i].id << std::endl;
164
165        if (strcmp(props[i].id, "DCE:eb12dbd0-078f-11e0-a6e8-0024bec489f8") == 0)
166        {
167            CORBA::ULong simple_temp;
168            props[i].value >>= simple_temp;
169            simple_0_value = simple_temp;
170            for( int k = 0; k < propertySet.length(); k++ ) {
171                if( strcmp(propertySet[k].id, props[i].id) == 0 ) {
172                    propertySet[k].value = props[i].value;
173                    break;
174                }
175            }
176        }
177
178    }
179
180    /// open destination file if not opened
181    if(!inputFileOpen){
182        inputFD = open(INPUT_FILE, O_RDONLY);
183        if(inputFD < 0){
184                perror("\n Error during opening input");
185                abort();
186        }
187        inputFileOpen = true;
188    }
189}
190
191void readBytesfromFile_i::ProcessData()
192{
193    DEBUG(3, readBytesfromFile, "ProcessData() invoked")
194
195        PortTypes::CharSequence I_out_0;
196
197    unsigned char byte;
198
199    while(continue_processing())
200    {
201        // output length is set from the configuration mask
202        std::cout << "In readbytesfromfile" << std::endl;
203        I_out_0.length(simple_0_value);
204
205        for(int i = 0; i < simple_0_value;){
206                int nbyte = read(inputFD, &byte, sizeof(char));                                         ///< read one byte
207                if (nbyte == 0){                                                                                                        ///< EOF reached
208                        lseek(inputFD,0,SEEK_SET);                                                                      ///< set the file selector to the beginning
209                        read(inputFD, &byte, sizeof(char));
210                }
211                for(int j=7; j >= 0; --j){                                                                                      ///< transform each byte in a sequence of 8 byte
212                        if((byte & (1 << j)) > 0)
213                                I_out_0[i] = 1;
214                        else
215                                I_out_0[i] = 0;
216                        i++;
217                }
218        }
219
220        dataOut_0->pushPacket(I_out_0);
221    }
222}
223
224bool readBytesfromFile_i::continue_processing()
225{
226        omni_mutex_lock l(processing_mutex);
227        return thread_started;
228}
229
230
Note: See TracBrowser for help on using the browser.