Innovative Integration
 
Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Copying Data from PacketStream into External Buffer

 
Post new topic   Reply to topic    II Support Forum Index -> X5-400M
View previous topic :: View next topic  
Author Message
bree



Joined: 11 Jul 2011
Posts: 7

PostPosted: Tue Jul 19, 2011 6:46 pm    Post subject: Copying Data from PacketStream into External Buffer Reply with quote

I want to make a function call in Snap which access data from PacketStream and copy it into my external buffer. For the first time, I already made my own code into HandleDataAvailable, which has data I want to pass into my buffer.

But, now I face a problem. I can't make a function call from SnapLib.cpp. This is the code.
Code:
err = wombat.StartConsole(myBuf); // Entry

myBuf is array of integer as my external buffer. After that, it goes to this code:
Code:
consoleSnapApplicationIo->Open(myBuf);

Then:
Code:
void  SnapApplicationIo::Open(int myBuf[])
{
   ...
   #if 1
   Stream.OnDataAvailable.SetEvent(this, &SnapApplicationIo::HandleDataAvailable);
   ...
}

Then the callback function goes here.
Code:
 void  SnapApplicationIo::HandleDataAvailable(PacketStreamDataEvent & Event)
{
   ...
   Buffer Packet;
   IntegerDG Packet_DG(Packet);
   int buffer_size = (Packet_DG.SizeInBytes());
   memcpy(&myBuf[0], &Packet_DG[0], buffer_size);
   // Only for testing
   ofstream output;
   output.open("output.txt");
   for (int i = 0; i < 32768; i++)
   {
      output << Packet_DG.at(i) << "\t" << myBuf[i] << endl;
   }
   output.close();
}

So, my problem lies here. I want to access data (Packet) in HandleDataAvailable function. But, it's a callback function. Am I able to access the packet?

Then, I thought about overwriting the input parameter of SetEvent function in BaseEvents_Mb.h so it can receive my external buffer. But, seems I can't do that, too.

Is my approach appropriate? Is there any way I can receive PacketData from HandleDataAvailable in void SnapApplicationIo::Open(int myBuf[]) above? Or, am I missing something here?
Sorry for my bad english. And thank you..
Back to top
View user's profile Send private message
bree



Joined: 11 Jul 2011
Posts: 7

PostPosted: Wed Jul 20, 2011 12:24 am    Post subject: Reply with quote

Here I attach my code, just in case you need it... My WriteBuffer code is in SnapAppIoCon.cpp. Thank you!
Back to top
View user's profile Send private message
hakan



Joined: 20 Jul 2011
Posts: 7

PostPosted: Wed Jul 20, 2011 4:14 pm    Post subject: Reply with quote

I think your approach should work fine even though I think it is sort of convoluted. But one problem is that you will see only the last buffer you received since your application is single threaded and you seem to wait on a timer Sleep(20000). At the time this sleep returns, the application stops and myBuf should contain the last piece of received data.

Moreover, I would like to point out a few things that, I think, you should correct.
Code:

//Instead of WriteBuffer(Innovative::Buffer Packet)
 
WriteBuffer(Innovative::Buffer &Packet)

since one would not want to copy the whole buffer but pass a reference to it.

Another issue is the way you are copying the data

Code:

//Instead of memcpy(&myBuf[0], &Packet_DG[0], buffer_size);

   for (int i = 0; i < Packet_DG.SizeInInts(); i++)
       myBuf[i] = Packet_DG[i];


memcpy makes an assumption that the IntegerDG class uses an array for its data. I do not think you should make such an assumption. I think a safer way would be to copy data in your array by looping.

Let me know if you have any further questions.

Hakan
Back to top
View user's profile Send private message Send e-mail
bree



Joined: 11 Jul 2011
Posts: 7

PostPosted: Wed Jul 20, 2011 6:14 pm    Post subject: Reply with quote

Okay. So, yes.. It only saves the last pieces of received data. But, my main problem is about accessing PacketStream from void SnapApplicationIo::Open(int myBuf[]).

Seems like I can't get that Packet from anywhere else but HandleDataAvailable callback function. I think, in order to make function call from SnapLib, I need to access those PacketStream in HandleDataAvailable from another function.

So, this is my approach. I send myBuf[] from the first time until it reaches HandleDataAvailable. But, I was stuck. I can't overwrite SetEvent function. So, is there any safer way to copy PacketStream into external buffer by accessing PacketStream from SnapLib, for instance?

Anyway, Hakan. Thanks for pointing those issues. I really help me to understand the whole picture. Smile
Back to top
View user's profile Send private message
bree



Joined: 11 Jul 2011
Posts: 7

PostPosted: Thu Jul 21, 2011 12:47 am    Post subject: Reply with quote

Sorry. I can't find Edit button in this page. I'd edited my last question.

Okay. So, yes.. It only saves the last pieces of received data. But, my main problem is about accessing PacketStream from void SnapApplicationIo::Open(int myBuf[]). Seems like I can't get that Packet from anywhere else but HandleDataAvailable callback function. In order to make function call from SnapLib, I need to access those PacketStream from another function.

So, this is my approach. I send myBuf[] from the first time (StartConsole()) until it reaches HandleDataAvailable. But, I was stuck. I can't overwrite SetEvent function. So, I made another try.

This time, I'd tried global variable in SnapAppIoCon.app. I copied Buffer Packet in HandleDataAvailable into Packet2 (Innovative::Buffer). Instead of sizeinbytes = 131072 (Test2.txt) as Packet in HandleDataAvailable is, I got sizeinbytes = 0 (Test.txt). So, I was stuck again.

So, is there any safer way to copy PacketStream from SnapLib.cpp into external buffer by accessing Buffer Packet? I need to get those Packet from a function call. This is what I try.
Code:

// In SnapApplicationIo.h
Innovative::Buffer Packet2;

// In SnapApplicationIo.cpp
void  SnapApplicationIo::HandleDataAvailable(PacketStreamDataEvent & Event)
{
   ...
   Packet2 = Packet;
   ofstream output;
   output.open("Test2.txt");
   output << Packet2.SizeInBytes()  << endl;
   output.close();
}

I collected Packet3 as I hope it contains same packet as Packet (or Packet2) does.
Code:

void  SnapApplicationIo::HandleDataAvailable(PacketStreamDataEvent & Event)
{
   Buffer Packet3;
   Packet3 = SnapApplicationIo::Packet2;

   ofstream output;
   output.open("Test.txt");
   output << Packet3.SizeInBytes() << endl;
   output.close();
}

Anyway, Hakan. Thanks for pointing those issues. I really help me to understand the whole picture. Smile
I attach SnapApplicationIo.cpp and the header file just in case you need it. The rest has been attached before.

Thank you!
Back to top
View user's profile Send private message
hakan



Joined: 20 Jul 2011
Posts: 7

PostPosted: Thu Jul 21, 2011 12:00 pm    Post subject: Reply with quote

If I understand you correctly, what you are trying to achieve is to export the received data outside of the ApplicationIo class.

All the methods you tried can be made to work but let's pick a cleaner method as opposed to defining global variables or changing function interfaces to pass around data.

Please look at the code I included. I added a new class into the ApplicationIo header which is a data exchange interface class. And changed the ApplicationIo constructor as to take this interface as a parameter. When the data is received data is pushed to this interface in the HandleDataAvailable.

Here is the code I added
Code:
 

// In the Application Io header
//======================================
//  CLASS IDataExportInterface  -- Interface for exporting data
//======================================

class IDataExportInterface
{
    public:
    ~IDataExportInterface() { }

    virtual void  Export(Innovative::Buffer &a_buffer) = 0;
   
};



// In your application please add your CollectData class

//===========================================================================
//  CLASS CollectData -- Store collected buffers
//===========================================================================
class CollectData : public IDataExportInterface
{
private:
    std::vector<Innovative::Buffer> &Storage;
public:
    CollectData(std::vector<Innovative::Buffer> &storage):
      Storage(storage) {}

    void  Export(Innovative::Buffer &a_buffer)
    {
        Storage.push_back(a_buffer);
    }
};


// Also pass a pointer to your IDataExportInterface class
    std::vector<Innovative::Buffer> Buffers;
    CollectData myExport(Buffers);

    UI = new UserInterface(this);
    Io = new ApplicationIo(UI, &myExport );
   GL = new GuiLoader;



I am adding ApplicationIo files also Form1 file. In Form1 only thing that is relevant is Form1::Form1 and CollectData class. Please compare the attached files to the standard files and see the changes I made to the standard example.

Hakan
Back to top
View user's profile Send private message Send e-mail
bpssoft



Joined: 07 Jun 2011
Posts: 29

PostPosted: Wed Nov 16, 2011 4:55 am    Post subject: Reply with quote

I want to copy the Buffer to another thread, but the following code makes it to slow. When i copy the buffer to the other thread it wont dispose in memory. I can't call Delete Buffer. How can i fast copy / move the data to another thread and dispose it?

Code:
   int PacketSize = Packet_DG.SizeInInts();
   int* Arrays = new int[PacketSize]();
   for (int i = 0; i < PacketSize; i++)
   {
      Arrays[i] = Packet_DG[i];
   }
Back to top
View user's profile Send private message
jhenderson
Site Admin


Joined: 07 Mar 2006
Posts: 2254
Location: So. Cal. USA

PostPosted: Mon Nov 21, 2011 11:40 am    Post subject: Reply with quote

I would recommend performing the allocation of Arrays early in the application, so that this time-consuming function need not be performed during data streaming. Likewise, it's deallocation can occur late in the application, not during streaming.

Secondly, if you include the call below in the constructor of ApplicationIo:


// Use IPP performance memory functions.
Init::UsePerformanceMemoryFunctions();

then you can use the fast IPP functions to perform copy operations.

#include <IppMemoryUtils_Mb.h>

void IppCopyInt32Buffer( const int *src_buffer, int *dst_buffer, size_t size );
void IppCopyInt64Buffer( const ii64 *src_buffer, ii64 *dst_buffer, size_t size );
void IppCopyByteBuffer( const unsigned char *src_buffer, unsigned char *dst_buffer, size_t size );
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    II Support Forum Index -> X5-400M (GMT - 8 Hours)
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

© Copyright 2006-2012 Innovative Integration
Powered by phpBB © 2001, 2002 phpBB Group
Based on iCGstation v1.0 Template By Ray © 2003, 2004 iOptional