Skip to content

Commit

Permalink
Add node
Browse files Browse the repository at this point in the history
  • Loading branch information
uoo723 committed Jun 21, 2017
1 parent f1793b7 commit 922570c
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 12 deletions.
11 changes: 7 additions & 4 deletions include/msg.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#ifndef MSGTYPE_H
#define MSGTYPE_H

#define MTEXTSIZE 128
#define VALUESIZE 100
#define KEYID 2817

struct msgbuf {
typedef struct msgbuf {
long mtype;
char mtext[MTEXTSIZE];
};
unsigned int key;
char value[VALUESIZE];
} msgbuf_t;

#define MSGSIZE (sizeof(msgbuf_t) - sizeof(long))

#endif
17 changes: 17 additions & 0 deletions include/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef NODE_H
#define NODE_H

#include "msg.h"

typedef struct node {
unsigned int key;
char value[VALUESIZE];
struct node *next;
} node_t;

node_t init();
int insert(unsigned int, char[VALUESIZE]);
int delete(unsigned int);
node_t get(unsigned int);

#endif
12 changes: 8 additions & 4 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

void client() {
key_t key_id;
struct msgbuf msg;
msgbuf_t msg;
char *text;

key_id = msgget(KEYID, IPC_CREAT|0666);
Expand All @@ -23,17 +23,21 @@ void client() {
}

printf("Start client\n");

while (1) {
printf("input > ");
scanf("%s", text);
msg.mtype = 1;
strcpy(msg.mtext, text);

if (msgsnd(key_id, &msg, sizeof(msg.mtext), 0) < 0) {
// strcpy(msg.mtext, text);

if (msgsnd(key_id, &msg, MSGSIZE, 0) < 0) {
perror("msgsnd error ");
exit(0);
}
printf("client: send %s\n", msg.mtext);

// printf("client: send %s\n", msg.key);

sleep(1);
}
}
95 changes: 95 additions & 0 deletions src/node.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <stdlib.h>
#include <string.h>

#include "node.h"
#include "msg.h"

node_t init() {
node_t *head = NULL;
head = malloc(sizeof(node_t));
if (head == NULL) {
return NULL;
}

head->next = NULL;

return head;
}

int insert(node_t head, unsigned int key, char value[VALUESIZE]) {
if (head == NULL) {
return -1;
}

node_t *node = get(key);

if (node != NULL) {
return -1;
}

node = malloc(sizeof(node_t));

if (node == NULL) {
return -1;
}

node->key = key;
strcpy(node->value, value);

node_t *iter = head;

while (iter->next != NULL) {
iter = iter->next;
}

iter->next = node;

return 0;
}

int delete(node_t head, unsigned int key) {
if (head == NULL) {
return -1;
}

node_t *iter = head->next;

while (iter != NULL) {
if (iter->next != NULL && iter->next->key == key) {
break;
}

iter = iter->next;
}

if (iter->next == NULL) {
return -1;
}

node_t *temp = iter->next;

iter->next = iter->next->next;

free(temp);

return 0;
}

node_t get(node_t head, unsigned int key) {
if (head == NULL) {
return;
}

node_t *iter = head;
while ((iter = iter->next) != NULL) {
if (iter->key == key) {
break;
}
}

if (iter == NULL) {
return NULL;
}

return iter;
}
9 changes: 5 additions & 4 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

void server() {
key_t key_id;
struct msgbuf msg;
msgbuf_t msg;

key_id = msgget(KEYID, IPC_CREAT|0666);

Expand All @@ -20,12 +20,13 @@ void server() {
}

printf("Start server\n");

while (1) {
if (msgrcv(key_id, &msg, sizeof(msg.mtext), 0, 0) < 0) {
if (msgrcv(key_id, &msg, MSGSIZE, 0, 0) < 0) {
perror("msgrcv error ");
exit(0);
}
printf("server: recevied %s\n", msg.mtext);

// printf("server: recevied %s\n", msg.mtext);
}
}

0 comments on commit 922570c

Please sign in to comment.