I have an python application using protocol buffers, and a Java application using protocol buffers too. What i want to do is just to be able to print the message (binary string after serializing) to the standard output. For this purpose i do the following in the Python app:
def printMessage(self, protobuf_msg):
data = protobuf_msg.SerializeToString()
sys.stdout.write(data)
sys.stdout.flush()
def main():
protobuf_msg = create_message()
controller.printMessage(protobuf_msg)
And after that, i want to pipe this output (python pytonApp | java javaApp) and get this data with the javaApp and parse it. I've tried two options, doing this with the Protobuf API:
protected ProtobufMsg receiveMsg() throws Exception{
ProtobufMsg message = null;
message = protobuf_msg.parseFrom(System.in);
return message;
}
I have also try to do this with the BufferedInputStream in the following way:
protected ProtobufMsg receiveMsg() throws Exception{
ProtobufMsg message = null;
byte[] data = receiveFromStd();
message = protobuf_msg.parseFrom(data);
return message;
}
public byte[] receiveFromStd() throws Exception{
BufferedInputStream input = new BufferedInputStream(System.in);
byte[] out = new byte[1024];
int i=0; System.out.println("Entering While"); while((out[i] = (byte)input.read())!= -1){ i++;System.out.println("One byte readed");
}
byte[] data_out = new byte[i]; for(int l=0; l<data_out.length; l++){ data_out[l]=out[l]; } return data_out;}
So it's obvious i'm doing something wrong but i'm not able to realized what i'm doing wrong, because it stays inside input.read()...
EDIT: I have decided to change strategy and now i get first the size of packet, and afterwards the packet, as i'm using input.read(byte []) function... The script i'm using is the following:
FIFO_FILE=/tmp/named_$$ # unique name ($$ is the PID of the bash process running this script)
mkfifo $FIFO_FILE
export FIFO_FILE # export the env variable
ant run & # start a background process that reads the env variable and reads the fifo
cat > $FIFO_FILE # reads the standard input and writes to the fifo
rm $FIFO_FILE
And I call this as: python pythonApp.py | ./script .
sys.stdout.write('\n'){}button in the Stackoverflow editor turns the selected lines into properly indented, highlighted code.