aboutsummaryrefslogtreecommitdiff
path: root/share/examples/sunrpc/msg
diff options
context:
space:
mode:
Diffstat (limited to 'share/examples/sunrpc/msg')
-rw-r--r--share/examples/sunrpc/msg/Makefile36
-rw-r--r--share/examples/sunrpc/msg/msg.x9
-rw-r--r--share/examples/sunrpc/msg/msg_proc.c30
-rw-r--r--share/examples/sunrpc/msg/printmsg.c45
-rw-r--r--share/examples/sunrpc/msg/rprintmsg.c74
5 files changed, 194 insertions, 0 deletions
diff --git a/share/examples/sunrpc/msg/Makefile b/share/examples/sunrpc/msg/Makefile
new file mode 100644
index 000000000000..2f3f5ddf1655
--- /dev/null
+++ b/share/examples/sunrpc/msg/Makefile
@@ -0,0 +1,36 @@
+#
+# @(#)Makefile 2.1 88/08/11 4.0 RPCSRC
+#
+BIN = printmsg msg_svc rprintmsg
+GEN = msg_clnt.c msg_svc.c msg.h
+LIB = -lrpclib
+RPCCOM = rpcgen
+
+all: $(BIN)
+
+#
+# This is the non-networked version of the program
+#
+printmsg: printmsg.o
+ $(CC) -o $@ printmsg.o
+
+#
+# note: no xdr routines are generated here, due this service's
+# use of basic data types.
+#
+$(GEN): msg.x
+ $(RPCCOM) msg.x
+
+msg_svc: msg_proc.o msg_svc.o
+ $(CC) -o $@ msg_proc.o msg_svc.o $(LIB)
+
+rprintmsg: rprintmsg.o msg_clnt.o
+ $(CC) -o $@ rprintmsg.o msg_clnt.o $(LIB)
+
+rprintmsg.o: rprintmsg.c msg.h
+
+msg_proc.o: msg_proc.c msg.h
+
+clean cleanup:
+ rm -f $(GEN) *.o $(BIN)
+
diff --git a/share/examples/sunrpc/msg/msg.x b/share/examples/sunrpc/msg/msg.x
new file mode 100644
index 000000000000..d3113520d822
--- /dev/null
+++ b/share/examples/sunrpc/msg/msg.x
@@ -0,0 +1,9 @@
+/* @(#)msg.x 2.1 88/08/11 4.0 RPCSRC */
+/*
+ * msg.x: Remote message printing protocol
+ */
+program MESSAGEPROG {
+ version MESSAGEVERS {
+ int PRINTMESSAGE(string) = 1;
+ } = 1;
+} = 99;
diff --git a/share/examples/sunrpc/msg/msg_proc.c b/share/examples/sunrpc/msg/msg_proc.c
new file mode 100644
index 000000000000..ed088839e52a
--- /dev/null
+++ b/share/examples/sunrpc/msg/msg_proc.c
@@ -0,0 +1,30 @@
+/* @(#)msg_proc.c 2.1 88/08/11 4.0 RPCSRC */
+/* $FreeBSD$ */
+/*
+ * msg_proc.c: implementation of the remote procedure "printmessage"
+ */
+#include <paths.h>
+#include <stdio.h>
+#include <rpc/rpc.h> /* always need this here */
+#include "msg.h" /* need this too: msg.h will be generated by rpcgen */
+
+/*
+ * Remote verson of "printmessage"
+ */
+int *
+printmessage_1(msg)
+ char **msg;
+{
+ static int result; /* must be static! */
+ FILE *f;
+
+ f = fopen(_PATH_CONSOLE, "w");
+ if (f == NULL) {
+ result = 0;
+ return (&result);
+ }
+ fprintf(f, "%s\n", *msg);
+ fclose(f);
+ result = 1;
+ return (&result);
+}
diff --git a/share/examples/sunrpc/msg/printmsg.c b/share/examples/sunrpc/msg/printmsg.c
new file mode 100644
index 000000000000..681007c4b1fe
--- /dev/null
+++ b/share/examples/sunrpc/msg/printmsg.c
@@ -0,0 +1,45 @@
+/* @(#)printmsg.c 2.1 88/08/11 4.0 RPCSRC */
+/* $FreeBSD$ */
+/*
+ * printmsg.c: print a message on the console
+ */
+#include <paths.h>
+#include <stdio.h>
+
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ char *message;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: %s <message>\n", argv[0]);
+ exit(1);
+ }
+ message = argv[1];
+
+ if (!printmessage(message)) {
+ fprintf(stderr, "%s: sorry, couldn't print your message\n",
+ argv[0]);
+ exit(1);
+ }
+ printf("Message delivered!\n");
+}
+
+/*
+ * Print a message to the console.
+ * Return a boolean indicating whether the message was actually printed.
+ */
+printmessage(msg)
+ char *msg;
+{
+ FILE *f;
+
+ f = fopen(_PATH_CONSOLE, "w");
+ if (f == NULL) {
+ return (0);
+ }
+ fprintf(f, "%s\n", msg);
+ fclose(f);
+ return(1);
+}
diff --git a/share/examples/sunrpc/msg/rprintmsg.c b/share/examples/sunrpc/msg/rprintmsg.c
new file mode 100644
index 000000000000..b9301de5e0d5
--- /dev/null
+++ b/share/examples/sunrpc/msg/rprintmsg.c
@@ -0,0 +1,74 @@
+/* @(#)rprintmsg.c 2.1 88/08/11 4.0 RPCSRC */
+/*
+ * rprintmsg.c: remote version of "printmsg.c"
+ */
+#include <stdio.h>
+#include <rpc/rpc.h> /* always need this */
+#include "msg.h" /* need this too: will be generated by rpcgen*/
+
+main(argc, argv)
+ int argc;
+ char *argv[];
+{
+ CLIENT *cl;
+ int *result;
+ char *server;
+ char *message;
+
+ if (argc < 3) {
+ fprintf(stderr, "usage: %s host message\n", argv[0]);
+ exit(1);
+ }
+
+ /*
+ * Remember what our command line arguments refer to
+ */
+ server = argv[1];
+ message = argv[2];
+
+ /*
+ * Create client "handle" used for calling MESSAGEPROG on the
+ * server designated on the command line. We tell the rpc package
+ * to use the "tcp" protocol when contacting the server.
+ */
+ cl = clnt_create(server, MESSAGEPROG, MESSAGEVERS, "tcp");
+ if (cl == NULL) {
+ /*
+ * Couldn't establish connection with server.
+ * Print error message and die.
+ */
+ clnt_pcreateerror(server);
+ exit(1);
+ }
+
+ /*
+ * Call the remote procedure "printmessage" on the server
+ */
+ result = printmessage_1(&message, cl);
+ if (result == NULL) {
+ /*
+ * An error occurred while calling the server.
+ * Print error message and die.
+ */
+ clnt_perror(cl, server);
+ exit(1);
+ }
+
+ /*
+ * Okay, we successfully called the remote procedure.
+ */
+ if (*result == 0) {
+ /*
+ * Server was unable to print our message.
+ * Print error message and die.
+ */
+ fprintf(stderr, "%s: sorry, %s couldn't print your message\n",
+ argv[0], server);
+ exit(1);
+ }
+
+ /*
+ * The message got printed on the server's console
+ */
+ printf("Message delivered to %s!\n", server);
+}