Annotation of prex/usr/sample/ipc/ipc.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2005, Kohsuke Ohtani
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: /*
31: * ipc.c - A sample program for IPC message transmission.
32: */
33:
34: #include <prex/prex.h>
35: #include <prex/message.h>
36: #include <stdio.h>
37: #include <string.h>
38:
39: /*
40: * Message structure
41: */
42: struct chat_msg {
43: struct msg_header hdr; /* Message header */
44: char str[128]; /* String */
45: };
46:
47:
48: static char stack[1024];
49:
50: /*
51: * Start client task/thread
52: */
53: static task_t
54: start_client(void (*func)(void), char *stack)
55: {
56: task_t task;
57: thread_t th;
58:
59: #ifdef CONFIG_MMU
60: if (task_create(task_self(), VM_COPY, &task) != 0)
61: return 0;
62: #else
63: task = task_self();
64: #endif
65: if (thread_create(task, &th) != 0)
66: return 0;
67:
68: if (thread_load(th, func, stack) != 0)
69: return 0;
70:
71: if (thread_resume(th) != 0)
72: return 0;
73:
74: return task;
75: }
76:
77: /*
78: * Send message to server
79: */
80: void
81: send_message(object_t obj, const char *str)
82: {
83: struct chat_msg msg;
84:
85: timer_sleep(2000, 0);
86: strcpy(msg.str, str);
87: msg_send(obj, &msg, sizeof(msg));
88: printf("client: Received \"%s\"\n", msg.str);
89: }
90:
91:
92: /*
93: * Client task
94: */
95: static void
96: client_task(void)
97: {
98: object_t obj;
99: int err;
100:
101: printf("Client is started\n");
102:
103: /*
104: * Find objects.
105: */
106: if ((err = object_lookup("/foo/bar", &obj)) != 0)
107: panic("can not find object");
108:
109: /*
110: * Send message per 2 second.
111: */
112: send_message(obj, "Hello!");
113: send_message(obj, "This is a client task.");
114: send_message(obj, "Who are you?");
115: send_message(obj, "How are you?");
116: send_message(obj, "....");
117: send_message(obj, "Bye!");
118: send_message(obj, "Exit");
119:
120: #ifdef CONFIG_MMU
121: printf("Exit client task...\n");
122: task_terminate(task_self());
123: #endif
124: }
125:
126: int
127: main(int argc, char *argv[])
128: {
129: object_t obj;
130: struct chat_msg msg;
131: int err, exit;
132:
133: printf("IPC sample program\n");
134:
135: /*
136: * Boost priority of this thread
137: */
138: thread_setprio(thread_self(), 90);
139:
140: /*
141: * Create object
142: */
143: if ((err = object_create("/foo/bar", &obj)) != 0)
144: panic("fail to create object");
145:
146: /*
147: * Start client task.
148: */
149: if (start_client(client_task, stack+1024) == 0)
150: panic("fail to create task");
151:
152: /*
153: * Message loop
154: */
155: exit = 0;
156: while (exit == 0) {
157: /*
158: * Wait for an incoming request.
159: */
160: err = msg_receive(obj, &msg, sizeof(msg));
161: if (err)
162: continue;
163: /*
164: * Process request.
165: */
166: printf("server: Received \"%s\"\n", msg.str);
167: if (!strcmp(msg.str, "Exit"))
168: exit = 1;
169:
170: if (!strcmp(msg.str, "Hello!"))
171: strcpy(msg.str, "Hi.");
172: else if (!strcmp(msg.str, "Bye!"))
173: strcpy(msg.str, "Bye.");
174: else
175: strcpy(msg.str, "OK.");
176:
177: /*
178: * Reply to the client.
179: */
180: msg_reply(obj, &msg, sizeof(msg));
181: }
182: timer_sleep(1000, 0);
183: printf("End...\n");
184: return 0;
185: }
CVSweb