root/ossiedev/branches/jeongo9/components/liquid-components/src/PacketSource.cpp @ 10566

Revision 10566, 7.6 KB (checked in by jeongo9, 2 years ago)

commiting liquid components

Line 
1/****************************************************************************
2
3Copyright 2010 by your_name_or_organization, all rights reserved.
4
5****************************************************************************/
6
7
8#include <cstdio>
9#include <string>
10#include <iostream>
11
12#include "config.h"
13#include "PacketSource.h"
14
15// include main.cpp (must define COMPONENT_OBJECT)
16#define  COMPONENT_OBJECT PacketSource_i
17#include "main.cpp"
18
19PacketSource_i::PacketSource_i(const char *uuid, omni_condition *condition) :
20    Resource_impl(uuid), component_running(condition)
21{
22    packetHeaderDataOut = new standardInterfaces_i::realChar_u("HeaderDataOut");
23    packetPayloadDataOut = new standardInterfaces_i::realChar_u("PayloadDataOut");
24
25    header_len = 24;
26    payload_len = 64;
27    verbose = true;
28    packet_delay = 1000;
29
30    packet_id = 0;
31}
32
33PacketSource_i::~PacketSource_i(void)
34{
35    delete packetHeaderDataOut;
36    delete packetPayloadDataOut;
37}
38
39// Static function for omni thread
40void PacketSource_i::Run( void * data )
41{
42    ((PacketSource_i*)data)->ProcessData();
43}
44
45CORBA::Object_ptr PacketSource_i::getPort( const char* portName ) throw (
46    CORBA::SystemException, CF::PortSupplier::UnknownPort)
47{
48    DEBUG(3, PacketSource, "getPort() invoked with " << portName)
49
50    CORBA::Object_var p;
51
52    p = packetHeaderDataOut->getPort(portName);
53
54    if (!CORBA::is_nil(p))
55        return p._retn();
56
57    p = packetPayloadDataOut->getPort(portName);
58
59    if (!CORBA::is_nil(p))
60        return p._retn();
61
62    /*exception*/
63    throw CF::PortSupplier::UnknownPort();
64}
65
66void PacketSource_i::start() throw (CORBA::SystemException,
67    CF::Resource::StartError)
68{
69    DEBUG(3, PacketSource, "start() invoked")
70    omni_mutex_lock  l(processing_mutex);
71    if( false == thread_started )
72    {
73        thread_started = true;
74        // Create the thread for the writer's processing function
75        processing_thread = new omni_thread(Run, (void *) this);
76
77        // Start the thread containing the writer's processing function
78        processing_thread->start();
79    }
80}
81
82void PacketSource_i::stop() throw (CORBA::SystemException, CF::Resource::StopError)
83{
84    DEBUG(3, PacketSource, "stop() invoked")
85    omni_mutex_lock l(processing_mutex);
86    thread_started = false;
87}
88
89void PacketSource_i::releaseObject() throw (CORBA::SystemException,
90    CF::LifeCycle::ReleaseError)
91{
92    DEBUG(3, PacketSource, "releaseObject() invoked")
93
94    component_running->signal();
95}
96
97void PacketSource_i::initialize() throw (CF::LifeCycle::InitializeError,
98    CORBA::SystemException)
99{
100    DEBUG(3, PacketSource, "initialize() invoked")
101}
102
103void PacketSource_i::query( CF::Properties & configProperties ) throw (CORBA::SystemException, CF::UnknownProperties)
104{
105    if( configProperties.length() == 0 )
106    {
107        configProperties.length( propertySet.length() );
108        for( unsigned int i = 0; i < propertySet.length(); i++ )
109        {
110            configProperties[i].id = CORBA::string_dup( propertySet[i].id );
111            configProperties[i].value = propertySet[i].value;
112        }
113        return;
114    } else {
115        for( unsigned int i = 0; i < configProperties.length(); i++ ) {
116            for( unsigned int j = 0; j < propertySet.length(); j++ ) {
117                if( strcmp(configProperties[i].id, propertySet[i].id) == 0 ) {
118                    configProperties[i].value = propertySet[i].value;
119                }
120            }
121        }
122    } // end if-else
123}
124
125void PacketSource_i::configure(const CF::Properties& props)
126throw (CORBA::SystemException,
127    CF::PropertySet::InvalidConfiguration,
128    CF::PropertySet::PartialConfiguration)
129{
130    DEBUG(3, PacketSource, "configure() invoked")
131
132    static int init = 0;
133    if( init == 0 ) {
134        if( props.length() == 0 ) {
135            std::cout << "PacketSource::configure() called with invalid props.length - " << props.length() << std::endl;
136            return;
137        }
138        propertySet.length(props.length());
139        for( unsigned int j=0; j < props.length(); j++ ) {
140            propertySet[j].id = CORBA::string_dup(props[j].id);
141            propertySet[j].value = props[j].value;
142        }
143        init = 1;
144    }
145
146    std::cout << "PacketSource::configure(), props length : " << props.length() << std::endl;
147
148    for ( unsigned int i = 0; i <props.length(); i++) {
149        std::cout << "PacketSource::configure(), property id : " << props[i].id << std::endl;
150
151        if (strcmp(props[i].id, "DCE:27ffa4d2-0f35-11df-baab-001aa089d644") == 0) {
152            // header_len (number of bytes in header)
153            CORBA::Short simple_temp;
154            props[i].value >>= simple_temp;
155            header_len = simple_temp;
156            std::cout << "  PacketSource::configure(), header_len = " << header_len << std::endl;
157            for( unsigned int k = 0; k < propertySet.length(); k++ ) {
158                if( strcmp(propertySet[k].id, props[i].id) == 0 ) {
159                    propertySet[i].value = props[i].value;
160                    break;
161                }
162            }
163        } else if (strcmp(props[i].id, "DCE:3320e0ba-0f35-11df-baab-001aa089d644") == 0) {
164            // payload_len (number of bytes in payload)
165            CORBA::Short simple_temp;
166            props[i].value >>= simple_temp;
167            payload_len = simple_temp;
168            std::cout << "  PacketSource::configure(), payload_len = " << payload_len << std::endl;
169            for( unsigned int k = 0; k < propertySet.length(); k++ ) {
170                if( strcmp(propertySet[k].id, props[i].id) == 0 ) {
171                    propertySet[i].value = props[i].value;
172                    break;
173                }
174            }
175        } else if (strcmp(props[i].id, "DCE:45ce96c6-0f35-11df-baab-001aa089d644") == 0) {
176            // verbose?
177            CORBA::Boolean simple_temp;
178            props[i].value >>= simple_temp;
179            verbose = simple_temp;
180            std::cout << "  PacketSource::configure(), verbose = " << verbose << std::endl;
181        } else if (strcmp(props[i].id, "DCE:908059a9-6632-4bc6-8e95-aec06fed3244") == 0) {
182            // packet delay (ms)
183            CORBA::Short simple_temp;
184            props[i].value >>= simple_temp;
185            packet_delay = simple_temp;
186            std::cout << "  PacketSource::configure(), packet_delay(ms) = " << packet_delay << std::endl;
187        } else {
188            std::cerr << "PacketSource::configure() : invalid property" << std::endl;
189            throw CF::PropertySet::InvalidConfiguration();
190        }
191    }
192}
193
194void PacketSource_i::ProcessData()
195{
196    DEBUG(3, PacketSource, "ProcessData() invoked")
197
198    // TODO : wait for component to be configured before actually starting
199
200    PortTypes::CharSequence headerData;
201    PortTypes::CharSequence payloadData;
202
203    // define lengths
204    headerData.length(header_len);
205    payloadData.length(payload_len);
206    while(continue_processing())
207    {
208        // wait some time between each packet
209        usleep(packet_delay*1000);
210
211        // generate header data (packet id)
212        unsigned int i;
213        for (i=0; i<header_len; i++)
214            headerData[i] = (i == 0) ? packet_id & 0xff : 0;
215        for (i=0; i<payload_len; i++)
216            payloadData[i] = rand() & 0xff;
217
218        if (verbose) {
219            printf("PacketSource generating packet [%4u] %4u bytes\n",
220                    packet_id,
221                    payload_len);
222        }
223
224        // only push header if length is greater than zero
225        if (header_len > 0)
226            packetHeaderDataOut->pushPacket(headerData);
227
228        packetPayloadDataOut->pushPacket(payloadData);
229
230        packet_id++;
231        packet_id &= 0xff; // for now strip id to only last 8 bits
232    }
233}
234
235bool PacketSource_i::continue_processing()
236{
237    omni_mutex_lock l(processing_mutex);
238    return thread_started;
239}
240
241
Note: See TracBrowser for help on using the browser.