root/components/RandomBits/trunk/RandomBits/RandomBits.cpp @ 2260

Revision 2260, 4.5 KB (checked in by balister, 6 years ago)

Fix inter packet dealy calculation.

Line 
1/****************************************************************************
2
3Copyright 2006 Virginia Polytechnic Institute and State University
4
5This file is part of the OSSIE RandomBits.
6
7OSSIE RandomBits is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12OSSIE RandomBits is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OSSIE RandomBits; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21****************************************************************************/
22
23
24#include <string>
25#include <iostream>
26
27#include <math.h>
28
29#ifdef HAVE_STDLIB_H
30#include <stdlib.h>
31#endif
32#ifdef HAVE_ERRNO_H
33#include <errno.h>
34#endif
35
36#include "ossie/cf.h"
37#include "RandomBits.h"
38#include "ossie/PortTypes.h"
39
40#include "ossie/Resource_impl.h"
41
42RandomBits_i::RandomBits_i(const char *uuid, omni_condition *condition) : Resource_impl(uuid), thread_started(false), component_running(condition)
43{
44    dataOut = new standardInterfaces_i::realChar_u("DataOut");
45}
46
47
48CORBA::Object_ptr RandomBits_i::getPort( const char* portName ) throw (CORBA::SystemException, CF::PortSupplier::UnknownPort)
49{
50    std::cout << "RandomBits_i getPort called with : " << portName << std::endl;
51   
52    CORBA::Object_var p;
53
54    p = dataOut->getPort(portName);
55
56    if (!CORBA::is_nil(p))
57        return p._retn();
58
59    if (strcmp(portName, "Control") == 0)
60        return myObjectRef._retn();
61
62    /*exception*/
63    throw CF::PortSupplier::UnknownPort();
64}
65
66void RandomBits_i::start() throw (CORBA::SystemException, CF::Resource::StartError)
67{
68    std::cout << "start called on RandomBits" << std::endl;
69    processing_thread = new omni_thread(do_process_data, (void *) this);
70    thread_started = true;
71    processing_thread->start();
72}
73
74void RandomBits_i::stop() throw (CORBA::SystemException, CF::Resource::StopError)
75
76    std::cout << "stop called on RandomBits" << std::endl;
77    thread_started = false;
78}
79
80void RandomBits_i::releaseObject() throw (CORBA::SystemException, CF::LifeCycle::ReleaseError)
81{
82    std::cout << "releaseObject called on RandomBits" << std::endl;
83   
84    component_running->signal();
85}
86
87void RandomBits_i::initialize() throw (CF::LifeCycle::InitializeError, CORBA::SystemException)
88{
89    std::cout << "initialize called on RandomBits" << std::endl;
90}
91
92void RandomBits_i::configure(const CF::Properties& props) throw (CORBA::SystemException, CF::PropertySet::InvalidConfiguration, CF::PropertySet::PartialConfiguration)
93{
94    std::cout << "configure called on RandomBits" << std::endl;
95   
96    std::cout << "props length : " << props.length() << std::endl;
97
98    for (unsigned int i = 0; i <props.length(); i++) {
99        // Packet Size property, short
100        if (strcmp(props[i].id, "DCE:377d6aa8-fe71-4297-8a74-3ca0dd008cb4") == 0) {
101            CORBA::Short j;
102            props[i].value >>= j;
103
104            omni_mutex_lock l(privateDataAccess);
105            packetSize = j;
106        }
107
108        // Delay between packets in seconds, float
109        if (strcmp(props[i].id, "DCE:9048e51b-a132-44a6-8f31-adf272bd62ce") == 0) {
110            CORBA::Float delay;
111            props[i].value >>= delay;
112
113            omni_mutex_lock l(privateDataAccess);
114            packetDelay.tv_sec = (time_t) floor(delay);
115            packetDelay.tv_nsec = (long) ((delay - floor(delay)) * 1e9);
116
117            std::cout << "Delay = " << delay << " seconds = " << packetDelay.tv_sec << " nanoseconds = " << packetDelay.tv_nsec << std::endl;
118        }
119    }
120}
121
122void RandomBits_i::process_data()
123{
124    std::cout << "process_data thread started" << std::endl;
125
126    PortTypes::CharSequence bits;
127
128    while(thread_started) {
129        struct timespec delayTime, rem;
130
131        { // Read private data
132            omni_mutex_lock l(privateDataAccess);
133            bits.length(packetSize);
134            delayTime.tv_sec = packetDelay.tv_sec;
135            delayTime.tv_nsec = packetDelay.tv_nsec;
136        }
137
138        for (unsigned int i = 0; i < bits.length(); i++)
139            bits[i] = (rand() > RAND_MAX/2 ? 1 : 0);
140
141        dataOut->pushPacket(bits);
142
143        bool done(false);
144        while (!done) {
145            int ret = nanosleep(&delayTime, &rem);
146
147            if (ret == EINTR) {
148                delayTime = rem;
149            } else
150                done = true;
151        }
152    }
153    processing_thread->exit();
154}
155   
Note: See TracBrowser for help on using the browser.