demo works ...

pull/9/head
Ondřej Hruška 7 years ago
parent 46883a9baa
commit 8415f15c99
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 97
      demo/demo.c
  2. 2
      demo/hello/master.c
  3. 12
      demo/hello/slave.c
  4. 2
      demo/utils.c

@ -6,6 +6,10 @@
#include "demo.h"
#define _GNU_SOURCE
#define __USE_GNU
#include <sched.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
@ -35,9 +39,10 @@ void TF_WriteImpl(const uint8_t *buff, size_t len)
}
}
static bool demo_client(void)
static int demo_client(void* unused)
{
pid_t childPID;
(void)unused;
ssize_t n = 0;
uint8_t recvBuff[1024];
struct sockaddr_in serv_addr;
@ -66,42 +71,29 @@ static bool demo_client(void)
return false;
}
childPID = fork();
if (childPID >= 0) { // fork was successful
if (childPID == 0) {// child process
printf("\n Child Process \n");
while ((n = read(sockfd, recvBuff, sizeof(recvBuff) - 1)) > 0 && !conn_disband) {
while ((n = read(sockfd, recvBuff, sizeof(recvBuff) - 1)) > 0) {
dumpFrame(recvBuff, (size_t) n);
TF_Accept(recvBuff, (size_t) n);
}
printf("\n End read \n");
if (n < 0) {
printf("\n Read error \n");
}
printf("\n Close sock \n");
close(sockfd);
sockfd = -1;
return true;
}
else { //Parent process
printf("\n Parent process \n");
return true;
}
}
else { // fork failed
printf("\n Fork failed!!!!!! \n");
return false;
}
// printf("\n End read \n");
//
// if (n < 0) {
// printf("\n Read error \n");
// }
//
// printf("\n Close sock \n");
// close(sockfd);
// sockfd = -1;
//
// return true;
return 0;
}
static bool demo_server(void)
static int demo_server(void* unused)
{
pid_t childPID;
(void)unused;
ssize_t n;
int listenfd = 0;
uint8_t recvBuff[1024];
@ -122,19 +114,14 @@ static bool demo_server(void)
if (bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("Failed to bind");
return false;
return 1;
}
if (listen(listenfd, 10) < 0) {
perror("Failed to listen");
return false;
return 1;
}
childPID = fork();
if (childPID >= 0) { // fork was successful
if (childPID == 0) {// child process
printf("\n Child Process \n");
while (1) {
printf("\nWaiting for client...\n");
sockfd = accept(listenfd, (struct sockaddr *) NULL, NULL);
@ -156,19 +143,7 @@ static bool demo_server(void)
close(sockfd);
sockfd = -1;
}
return true;
}
else { //Parent process
printf("\n Parent process \n");
return true;
}
}
else { // fork failed
printf("\n Fork failed!!!!!! \n");
return false;
}
return 0;
}
void signal_handler(int sig)
@ -176,7 +151,7 @@ void signal_handler(int sig)
(void)sig;
printf("Shutting down...");
demo_disconn();
exit(0);
exit(sig);
}
void demo_init(TF_PEER peer)
@ -184,14 +159,26 @@ void demo_init(TF_PEER peer)
signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler);
bool suc;
int retc;
void *stack = malloc(8192);
if (stack == NULL) {
perror("Oh fuck");
signal_handler(9);
return;
}
printf("Starting %s...\n", peer == TF_MASTER ? "MASTER" : "SLAVE");
if (peer == TF_MASTER) {
suc = demo_client();
retc = clone(&demo_client, (char *)stack+8192, CLONE_VM|CLONE_FILES, 0);
} else {
suc = demo_server();
retc = clone(&demo_server, (char *)stack+8192, CLONE_VM|CLONE_FILES, 0);
}
if (!suc) {
if (retc == 0) {
perror("Clone fail");
signal_handler(9);
return;
}
printf("Thread started\n");
}

@ -32,7 +32,7 @@ int main(void)
TF_SendSimple(1, (pu8)"Ahoj", 5);
TF_SendSimple(1, (pu8)"Hello", 6);
TF_QuerySimple(1, (pu8)"Query!", 6, testIdListener, 0);
TF_QuerySimple(2, (pu8)"Query!", 6, testIdListener, 0);
while(1) usleep(10);
}

@ -12,9 +12,18 @@ bool helloListener(TF_MSG *msg)
{
printf("helloListener()\n");
dumpFrameInfo(msg);
msg->data = (const uint8_t *) "jak se mas?";
return true;
}
bool replyListener(TF_MSG *msg)
{
printf("replyListener()\n");
dumpFrameInfo(msg);
msg->data = (const uint8_t *) "response to query";
msg->len = (TF_LEN) strlen((const char *) msg->data);
TF_Respond(msg);
TF_SendSimple(77, (const uint8_t *) "NAZDAR", 7);
return true;
}
@ -22,6 +31,7 @@ int main(void)
{
TF_Init(TF_SLAVE);
TF_AddTypeListener(1, helloListener);
TF_AddTypeListener(2, replyListener);
demo_init(TF_SLAVE);
printf("MAIN PROCESS CONTINUES...\n");

@ -10,7 +10,7 @@ void dumpFrame(const uint8_t *buff, size_t len)
{
size_t i;
for(i = 0; i < len; i++) {
printf("%3u \033[34m%02X\033[0m", buff[i], buff[i]);
printf("%3u \033[94m%02X\033[0m", buff[i], buff[i]);
if (buff[i] >= 0x20 && buff[i] < 127) {
printf(" %c", buff[i]);
} else {

Loading…
Cancel
Save