Ice provides a communication solution that is simple to understand and easy to program with. Yet, despite its simplicity, Ice is flexible enough to accommodate even the most demanding and mission-critical applications.

ICE framework has a few primitive types to send data with the interfaces. Many times you need send binary data as JPG images, objects, music files, etc.The following source (C++) show how you can send easily binary data with ICE.

Firstly, define your own interface. Be sure to get a ByteSeq field, we use this field to save and send the binary data.

class Data {
	ByteSeq binary;
};
 
interface Communicator {
	void notify (Data data);	
};

The client part just read the binary data (file or memory) and copy in std::vector. Then you have to do memcpy to ByteSeq field. You notice that ByteSeq type is translated to std::vector in C++

int MAX_BUFFER_READ = 100*1024; // 100 Kb
std::vector buffer;
std::string filename = "/tmp/image.jpg";
 
ifstream input(filename);
 
if (input)
{
	buffer.resize( MAX_BUFFER_READ );
	input.read( (char*)(&buffer[0]), MAX_BUFFER_READ ) ;
	size_t counted = input.gcount();
	buffer.resize (counted);
	input.close();
 
	DataPtr mydata = new Data();
	memcpy(&(myData->binary[0]),(unsigned char *) &buffer[0], buffer.size());
 
	ProxyCommunicator->notify(mydata);
}

The server has to receive the data and save in file. This example sends a JPEG image file, but if you want you can send a serialized object to restore it in memory.

virtual void notify (const DataPtr& data , const Ice::Current& current )
{
	std::string filename = "/tmp/image2.jpg";
	ofstream of( filename.c_str() );
	if (of)
	{
		of.write(reinterpret_cast&<char*>(&data->binary[0]), data->binary.size());
 
		of.close();
	}
}

Remember that we have an ICE limitation in order to size. The max size allowed is 10 MBytes, so if you want send bigger files you will have to create a bit protocol for that.

facebooktwittergoogle_pluslinkedinmailby feather
Categories: jderobot

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

jderobot

Asus Xtion: usb soft reset

If you have an Asus Xtion pro live and usually you work under GNU/Linux, perhaps you notice than sometimes you can’t read from Asus Xtion because the usb bus has a problem. Unfortunately the only way Read more…

android

Video Surveillance HD: Raspberrypi + PiCam + Android

The video-surveillance field is active many years ago, but there is only products that show image and little else. It’s need to add intelligence to surveillance tasks. We are committed to create technology to care for people and avoid dangerous situations. For this Read more…

jderobot

RPI Camera Board

It’s a great pleasure have this cool gadget. This is the new RPI Camera Board, the essential part in every RPI project! The Raspberry Pi Camera Module is a custom designed add-on for Raspberry Pi. It Read more…