Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
76260c7
commit necessaire pour pull - pas de change particuliers
Apr 30, 2019
b18d7b5
Merge branch 'master' of github.com:Tywacol/SimulPhys
Apr 30, 2019
0080876
Ajout d'etages et déplacement modifiés
Psayc May 30, 2019
2702b0c
Update pause.gd
Psayc May 30, 2019
177dcca
Create lol.txt
seb-60180 May 30, 2019
eb44c42
Ajout début commande
seb-60180 May 30, 2019
0ae8d89
one way networking python->godot, need cleaning
Tywacol May 31, 2019
d71bc1e
two way networking with 2 script control simluation and change variables
Tywacol Jun 1, 2019
2186063
Add shell command with recording, output to csv for UDPServ.py and op…
Tywacol Jun 1, 2019
de66395
Completed : simili playback works and repo / code cleaned. Last idea …
Tywacol Jun 2, 2019
02750ad
fixed playback, good enough
Tywacol Jun 2, 2019
a0f605d
fixed playback, good enough
Tywacol Jun 2, 2019
fd7c041
fixed playback, good enough
Tywacol Jun 2, 2019
8a54cf8
added exit message
Tywacol Jun 2, 2019
a8bf88a
Ajout du menu option
Jun 2, 2019
a1ebc41
Merge branch 'master' of https://github.com/Tywacol/SimulPhys
Jun 2, 2019
1173c5d
fixed little bugs
Tywacol Jun 2, 2019
e3901ab
Merge branch 'master' of github.com:Tywacol/SimulPhys
Tywacol Jun 2, 2019
6a3f812
Add menu option
Jun 2, 2019
e925450
Merge branch 'master' of github.com:Tywacol/SimulPhys
Jun 2, 2019
14ed37f
cleaned my laptop
Jun 2, 2019
b0e5179
cleaned my laptop
Jun 2, 2019
2090504
Amélioration des options
Jun 2, 2019
b1504da
Petit oublie menu option
Jun 2, 2019
b60c152
Update README.md
Tywacol Jun 2, 2019
7080381
Update README.md
Tywacol Jun 2, 2019
cf71803
Create tmp
Tywacol Jun 2, 2019
03e7bd2
Add files via upload
Tywacol Jun 2, 2019
aac64aa
Delete tmp
Tywacol Jun 2, 2019
3127560
Update README.md
Tywacol Jun 2, 2019
78b5962
Update README.md
Tywacol Jun 2, 2019
da05fba
Update README.md
Tywacol Jun 2, 2019
7f338c7
Update README.md
Tywacol Jun 2, 2019
16a63a4
Update README.md
Tywacol Jun 2, 2019
488cd7e
Update README.md
Tywacol Jun 2, 2019
1854c12
harmonisatoni menu option et commande
Jun 3, 2019
6ab73c7
Correction bug et reprendre + exportation
Jun 3, 2019
5e8b49e
add rapport and source
Tywacol Jun 3, 2019
94d8585
Update README.md
Tywacol Jun 3, 2019
f76cd4e
reglage bug
Jun 3, 2019
3d6498c
Merge branch 'master' of https://github.com/Tywacol/SimulPhys
Jun 3, 2019
557fc19
qqs modifs
Tywacol Jun 3, 2019
4af1aa1
Synchro option avec console serveur + export
Jun 3, 2019
7ef28d3
merged clement code and mine
Tywacol Jun 4, 2019
b345071
ascenseur simulation now queue cmds if busy
Tywacol Jun 4, 2019
4ea21d5
demo.csv special
Tywacol Jun 4, 2019
38f2754
Create tmp
Tywacol Jun 4, 2019
923da41
Add plot of height of lift in function of time
Tywacol Jun 4, 2019
6afeb2a
demo.csv remise a zero
Tywacol Jun 4, 2019
f42a464
Merge branch 'master' of github.com:Tywacol/SimulPhys
Tywacol Jun 4, 2019
b49e8b1
add plot and clean
Tywacol Jun 4, 2019
cf9c7c4
clean
Tywacol Jun 4, 2019
5e8e75b
add test ip range
Tywacol Jun 4, 2019
81cfc95
Delete tmp
Tywacol Jun 4, 2019
1dd9c2a
Changes
Tywacol Apr 9, 2020
a80170f
add module modbuspp+python and client.gd
Tywacol May 1, 2020
4ee2e17
push sauvegarde travail
Tywacol May 3, 2020
4b94838
cleanup
Tywacol May 3, 2020
8fc495a
add modbuspp directory
Tywacol May 3, 2020
63d58ab
mostly working with back and forth
Tywacol May 3, 2020
e1b3078
Working well!
Tywacol May 5, 2020
37d8764
Update repo after S8
Tywacol May 8, 2020
6f45122
Update repo after S8
Tywacol May 8, 2020
96419c7
Update repo after S8
Tywacol May 8, 2020
5ec26d9
Update repo after S8
Tywacol May 8, 2020
98af19a
Update repo after S8
Tywacol May 8, 2020
34a63e1
Update repo after S8
Tywacol May 8, 2020
c5b1214
Add pipe.py
Tywacol May 11, 2020
1d6ee63
Add pipe.cpp
Tywacol May 11, 2020
aca7aab
create dir
Tywacol May 11, 2020
6127efe
Add pipe.cpp
Tywacol May 11, 2020
97d1748
Delete t
Tywacol May 11, 2020
2baaff6
Create pipe.py
Tywacol May 11, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
232 changes: 232 additions & 0 deletions Divers/Pipes/pipe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
#include <iostream>
#include <sstream>
#include <unistd.h>
#include <cstring>
#include <vector>


/*
APIs to be accessed by the script.
*/

static void foo_api(std::ostream &os, const std::string &arg)
{
os << "Foo was called with arg " << arg;
}

static void bar_api(std::ostream &os, const std::string &arg)
{
os <<"Bar was called with arg " << arg;
}

/* end API section */


/* return true if val is set, false for EOF */
static bool read_uint32(int read_fd, uint32_t &val)
{
unsigned char msgSizeBuf[4];
unsigned iBuf = 0;

while (iBuf < sizeof(msgSizeBuf))
{
ssize_t rc = ::read(read_fd, msgSizeBuf + iBuf, sizeof(msgSizeBuf) - iBuf);

if (rc == 0)
{
return false;
}
else if (rc < 0 )
{
std::cout << __func__ << "@" << __LINE__ << ":::Read ERROR" << std::endl;
exit(1);
}
else
{
iBuf += rc;
}
}

val = *(static_cast<uint32_t *>(static_cast<void *>(&msgSizeBuf[0])));

return true;
}


static void send_msg(int write_fd, std::string msg)
{
uint32_t msgSize = msg.size();
unsigned char msgSizeBuf[4];

::memcpy(msgSizeBuf, &msgSize, sizeof(msgSize));

unsigned iBuf = 0;
while (iBuf < 4)
{
ssize_t rc = ::write(write_fd, msgSizeBuf + iBuf, sizeof(msgSizeBuf) - iBuf);
if ( rc < 0 )
{
std::cout << "Error writing message size" << std::endl;
::exit(1);
}
else if ( rc == 0 )
{
std::cout << "rc == 0, what does that mean?" << std::endl;
::exit(1);
}
else
{
iBuf += rc;
}
}

iBuf = 0;
const char *msgBuf = msg.c_str();
while (iBuf < msgSize)
{
ssize_t rc = ::write(write_fd, msgBuf + iBuf, msgSize - iBuf);
if ( rc < 0 )
{
std::cout << "Error writing message" << std::endl;
::exit(1);
}
else if ( rc == 0 )
{
std::cout << "rc == 0, what does that mean?" << std::endl;
::exit(1);
}
else
{
iBuf += rc;
}
}
}

static std::string read_string(int read_fd, uint32_t sz)
{
std::vector<char> msgBuf( sz + 1 );
msgBuf[ sz ] = '\0';
unsigned iBuf = 0;

while (iBuf < sz)
{
ssize_t rc = ::read(read_fd, &(msgBuf[0]) + iBuf, sz - iBuf);

if ( rc == 0 )
{
std::cout << __func__ << "@" << __LINE__ << ":::EOF read" << std::endl;
exit(1);
}
else if ( rc < 0 )
{
std::cout << __func__ << "@" << __LINE__ << ":::Read ERROR during message" << std::endl;
exit(1);
}
else
{
iBuf += rc;
}
}

return std::string( &(msgBuf[0]) );
}


static void read_loop(int read_fd, int write_fd)
{
while ( 1 )
{
/* Python sends format
[apiNameSize][apiName][apiArgSize][apiArg]
on the pipe */
std::cout << "Waiting for message from python..." << std::endl;
uint32_t apiNameSize;
if (!read_uint32(read_fd, apiNameSize))
{
// EOF waiting for a message, script ended
std::cout << "EOF waiting for message, script ended" << std::endl;
return;
}
std::string apiName = read_string(read_fd, apiNameSize);
uint32_t apiArgSize;
if (!read_uint32(read_fd, apiArgSize))
{
std::cout << "EOF white reading apiArgSize" << std::endl;
::exit(1);
}
std::string apiArg = read_string(read_fd, apiArgSize);

std::cout << "apiName: " << apiName << std::endl
<< "apiArg: " << apiArg << std::endl;


// Response comes as [resultSize][resultString]
if (apiName == "foo")
{
std::ostringstream os;
foo_api(os, apiArg);
send_msg(write_fd, os.str());
}
else if (apiName == "bar")
{
std::ostringstream os;
bar_api(os, apiArg);
send_msg(write_fd, os.str());
}
else
{
std::cout << "UNSUPPORTED API " << apiName << std::endl;
send_msg(write_fd, "__BAD API__");
}
}
}


int main()
{
std::cout << "Hello World!" << std::endl;

int pipe_cpp_to_py[2];
int pipe_py_to_cpp[2];

if (::pipe(pipe_cpp_to_py) || ::pipe(pipe_py_to_cpp))
{
std::cout << "Couldn't open pipes" << std::endl;
::exit(1);
}

pid_t pid = fork();

if ( pid == 0 )
{
::close(pipe_py_to_cpp[0]);
::close(pipe_cpp_to_py[1]);
std::ostringstream oss;

oss << "export PY_READ_FD=" << pipe_cpp_to_py[0] << " && "
<< "export PY_WRITE_FD=" << pipe_py_to_cpp[1] << " && "
<< "export PYTHONUNBUFFERED=true && " // Force stdin, stdout and stderr to be totally unbuffered.
<< "python src/main.py";


::system(oss.str().c_str());
::close(pipe_py_to_cpp[1]);
::close(pipe_cpp_to_py[0]);

}
else if ( pid < 0 )
{
std::cout << "Fork failed." << std::endl;
::exit(1);
}
else
{
::close(pipe_py_to_cpp[1]);
::close(pipe_cpp_to_py[0]);
read_loop(pipe_py_to_cpp[0], pipe_cpp_to_py[1]);
::close(pipe_py_to_cpp[0]);
::close(pipe_cpp_to_py[1]);
}

return 0;
}
Loading