tcp recorder

For developers writing C++, Fortran, Java, code who have questions or comments to make.

Moderators: silvia, selimgunay, Moderators

Post Reply
dborello
Posts: 26
Joined: Fri Sep 05, 2008 11:00 am
Location: University of Illinois at Urbana-Champaign

tcp recorder

Post by dborello » Thu Jul 08, 2010 3:00 pm

I noticed that version 2.2.1 added support for sending recorder output over a tcp connection.

This is very interesting and could alleviate having to dump output to disk. It has a lot of potential and is exciting.

I am giving it a shot with a simple model (below). I am using this program to listen to the socket (www.rjlsoftware.com/software/utility/portlistener/). I have connected to it and verified that it reports the activity on the port.

For short analyses (just a couple of steps), the client connects and then disconnects. For large analyses (500 steps), the client connects, sends some garbage (non plain text) and then disconnects.

What is the expected behavior of this function? Any information is appreciated.

Thanks,
Dan

[code]
wipe;
model BasicBuilder -ndm 2 -ndf 3;

node 1 -120 0;
node 2 -120 144;

fix 1 1 1 1;

uniaxialMaterial ElasticPP 1 29000 0.001724;

section Fiber 1 {
patch quad 1 30 30 -5 -9 5 -9 5 9 -5 9;
}

geomTransf Corotational 1;

element nonlinearBeamColumn 1 1 2 2 1 1;


pattern Plain 1 Linear {
load 2 1.0 0.0 0.0
}

recorder Node -file Out.txt -time -node 2 -dof 1 disp;
recorder Node -file Reaction.txt -time -node 1 -dof 3 reaction;

recorder Node -tcp 127.0.0.1 4114 -time -node 2 -dof 1 disp;

constraints Plain;
numberer Plain;
system BandGeneral;
test NormUnbalance 1e-3 50 2;
algorithm Newton;
integrator DisplacementControl 2 1 0.1
analysis Static;

analyze 10;
[/code]

fmk
Site Admin
Posts: 5866
Joined: Fri Jun 11, 2004 2:33 pm
Location: UC Berkeley
Contact:

Post by fmk » Thu Jul 08, 2010 3:53 pm

the garbage, as you called it, is the data coming across in binary form plus an additional double value (first field) .. the additional double is used to carry size and information about when remote side is done sending data .. it's not sent as text to both reduce the amount of data being sent and also eliminate the potential loss of information in the data.

here is an example main that takes that will process the data & send it to the screen (you would of course want to do something else with it):

[code]
int main(int argc, char **argv)
{
Vector data(1);
int dataSize = 0;

if (argc != 2) {
opserr << "ERROR - Usage testTCP inetPort\n";
exit(0);
}

int inetPort = atoi(argv[1]);
TCP_Socket *theSocket = new TCP_Socket(inetPort);
if (theSocket == 0) {
opserr << "ERROR - Failed to Create a TCP_Socket\n";
exit(0);
}
theSocket->setUpConnection();

theSocket->recvVector(0,0,data);

int sizeReceived = data(0);
while (sizeReceived != -1) {
if (sizeReceived != dataSize) {
data.resize(sizeReceived+1);
dataSize = sizeReceived;
}

if (theSocket->recvVector(0,0, data) == 0) {
sizeReceived = data(0);
if (sizeReceived == dataSize) {
for (int i=1; i<=dataSize; i++)
opserr << data(i) << " " ;
opserr << endln;
}
}
}

delete theSocket;
exit(0);
[/code]

dborello
Posts: 26
Joined: Fri Sep 05, 2008 11:00 am
Location: University of Illinois at Urbana-Champaign

Re: tcp recorder

Post by dborello » Tue Mar 29, 2016 2:02 pm

To bump a 6 year old thread, I finally got back around to addressing this.

For those of you interested in using the code Frank posted, it requires some other OpenSees framework. It is available in its entirety in SRC/handler. 'make test' will produce a binary that can be used.

When the socket is opened, 8 bytes are sent representing the number of values for each step. Then for each step, this value is repeated, followed by 8 bytes for each piece of data for the recorder. The data is sent little-endian.

Python3 implementation to receive recorder socket:

[code]
#!/usr/bin/env python

import socket
import struct
import binascii

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('', 4117))

serversocket.listen(5)
unpacker = struct.Struct('<d')
(clientsocket, address) = serversocket.accept()
print('Connected!')

#Get data size
raw = clientsocket.recv(unpacker.size)
DataSize = unpacker.unpack(raw)[0]

print('Data Size == {0}'.format(DataSize))

#Loop over each step
SizeReceived = 0
Data = []
while 1:
raw = clientsocket.recv(unpacker.size)

#Socket done
if not raw:
break

#Do not record first value
if SizeReceived > 0:
Data.append(unpacker.unpack(raw)[0])

#All data received
if SizeReceived == DataSize:
SizeReceived = 0
print(Data)
Data = []
else:
SizeReceived += 1

clientsocket.close()

[/code]

Post Reply