Skip to content
Snippets Groups Projects
Commit bded2b52 authored by Sven Gestegård Robertz's avatar Sven Gestegård Robertz
Browse files

added client for labcomm/JGrafchart + test server

parent 7810c9ee
No related branches found
No related tags found
No related merge requests found
#SAMPLENAME=foo
LCFILE=jg
LCDIR=../..
LCC=java -jar ${LCDIR}/compiler/labComm.jar
LCLIBDIR=${LCDIR}/lib/c
AUX=enc.c dec.c
client: client.c ${LCFILE}.c ${AUX} ${AUX:.c=.h}
${CC} -o $@ client.c ${AUX} ${LCFILE}.c -I${LCLIBDIR} -L${LCLIBDIR} -llabcomm
testserver: testserver.c ${LCFILE}.c ${AUX} ${AUX:.c=.h}
${CC} -o $@ testserver.c ${AUX} ${LCFILE}.c -I${LCLIBDIR} -L${LCLIBDIR} -llabcomm
${LCFILE}.c : ${LCFILE}.lc
${LCC} -C ${LCFILE}.lc
.PHONY: clean, runclient
clean :
rm ${LCFILE}.c ${LCFILE}.h client testserver
runclient : client
LD_LIBRARY_PATH=${LCLIBDIR} ./$< localhost 9999
runserver : testserver
LD_LIBRARY_PATH=${LCLIBDIR} ./$< 9999
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "dec.h"
#include "enc.h"
void error(const char *msg)
{
perror(msg);
exit(0);
}
void do_labcomm(int sockfd)
{
void *enc = enc_init(sockfd);
void *dec = dec_init(sockfd, enc);
dec_run(dec);
dec_cleanup(dec);
enc_cleanup(enc);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
do_labcomm(sockfd);
close(sockfd);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_reader.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include <stdio.h>
#include "jg.h"
static struct labcomm_encoder *encoder;
static void handle_foo(jg_foo *v, void *context) {
printf("got foo: b=%f, e=%s, f=%d\n", v->b, v->e, v->f);
v->d = v->d+1;
labcomm_encode_jg_foo(encoder, v);
usleep(500000);
v->d = v->d+1;
labcomm_encode_jg_foo(encoder, v);
}
struct labcomm_decoder *dec_init(int fd, struct labcomm_encoder *e) {
struct labcomm_decoder *decoder;
encoder = e;
void *context = NULL;
decoder = labcomm_decoder_new(labcomm_fd_reader_new(
labcomm_default_memory, fd, 1),
labcomm_default_error_handler,
labcomm_default_memory,
labcomm_default_scheduler);
if (!decoder) {
printf("Failed to allocate decoder %s:%d\n", __FUNCTION__, __LINE__);
return (void *)0;
}
labcomm_decoder_register_jg_foo(decoder, handle_foo, context);
return decoder;
}
void dec_run(struct labcomm_decoder *decoder) {
printf("Decoding:\n");
labcomm_decoder_run(decoder);
printf("--- End Of File ---:\n");
}
void dec_cleanup(struct labcomm_decoder *decoder) {
labcomm_decoder_free(decoder);
}
#include <labcomm_fd_reader.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include <stdio.h>
struct labcomm_decoder * dec_init(int fd, struct labcomm_encoder *e) ;
void dec_run(struct labcomm_decoder *decoder) ;
void dec_cleanup(struct labcomm_decoder *decoder) ;
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <labcomm_fd_writer.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
#include "jg.h"
#include <stdio.h>
struct labcomm_encoder *enc_init( int fd) {
struct labcomm_encoder *encoder;
encoder = labcomm_encoder_new(labcomm_fd_writer_new(
labcomm_default_memory, fd, 1),
labcomm_default_error_handler,
labcomm_default_memory,
labcomm_default_scheduler);
labcomm_encoder_register_jg_foo(encoder);
return encoder;
}
void enc_run(struct labcomm_encoder * encoder) {
jg_foo v;
v.b = 17.17;
v.c = 1742;
v.d = 4217;
v.e = "hello";
v.f = 17;
v.g = 42;
v.h = 2;
v.i = 42.42;
labcomm_encode_jg_foo(encoder, &v);
usleep(500000);
v.f += 42;
v.h += 42017040;
labcomm_encode_jg_foo(encoder, &v);
}
void enc_cleanup(struct labcomm_encoder *encoder) {
labcomm_encoder_free(encoder);
}
#include <labcomm_fd_writer.h>
#include <labcomm_default_error_handler.h>
#include <labcomm_default_memory.h>
#include <labcomm_default_scheduler.h>
struct labcomm_encoder *enc_init(int fd) ;
void enc_run(struct labcomm_encoder *encoder) ;
void enc_cleanup(struct labcomm_encoder *encoder) ;
sample struct {
double b;
int c;
int d;
string e;
boolean f;
short g;
long h;
float i;
} foo;
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include "dec.h"
#include "enc.h"
void error(const char *msg)
{
perror(msg);
exit(0);
}
void do_labcomm(int sockfd)
{
void *enc = enc_init(sockfd);
enc_run(enc);
enc_cleanup(enc);
}
int main(int argc, char *argv[])
{
int sockfd, portno, n;
int newsockfd, clilen;
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
char buffer[256];
if (argc < 2) {
fprintf(stderr,"usage %s port\n", argv[0]);
exit(0);
}
portno = atoi(argv[1]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR on binding");
exit(1);
}
listen(sockfd,2);
clilen = sizeof(cli_addr);
/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
if (newsockfd < 0) {
perror("ERROR on accept");
exit(1);
}
do_labcomm(newsockfd);
close(newsockfd);
close(sockfd);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment