1 Example 1

1.1 Scenario

Let's say we need to have two programs interact with each other. They need to be able to access each others' functionality - that is, one of them is providing a service to the other. Let's say that this service (it's a simple example, OK?) is to write out a log entry to a file.

1.2 The Code

All this code is available in the distribution, in the "test/MpsSimpleExample" directory. You will find there

  • a working Java Logserver implementation
  • a working C++ Logserver implementation
  • a working C++ Logserver client
  • the MPSIDL Logserver specification

1.2.1 MPS IDL File

We might choose to define part of the interface like this (logging.mps - [:loggingmps:]):

 namespace Logging {
	interface LogServer {
		// Log a string to our current file.
		// Returns true for success, and false for failure.
		// We could have chosen to use exceptions in case of failure.
		bool logString(string toLog);

Performing the command

 mpsidl -c -o logging logging.mps

produces the files logging.h and, which contain definitions for C++ server stubs and client proxies implementing this interface.

1.2.2 Client code

One of our C++ programs needs to make use of this new functionality. Read the [:cplusplusclient:] code to see how to do so. Here's a fragment of that file:

 MPS::InetTransport::initialise("", 0);
 ref<MPS::NamingService> ns = MPS::getNamingService();
 ref<Logging::LogServer> logServer = Logging::LogServer::_attach(ns->resolve("Logserver"));

 if (logServer->logString("Startup successful") == false) {
   // Couldn't write log message...

1.2.3 Server code

We choose to implement the server in Java. Running the command

 mpsidl -j logging.mps

creates the directory Logging/ which contains Java source code implementing package Logging, which provides both client- and server-side interfaces to the interface defined in logging.mps.

We write our implementation of the server like this ([:javaserver:]):

 import org.hebe.mps.*;
 import org.hebe.mps.naming.*;
 import Logging.*;

 class LogServerImpl implements LogServer {
    public static void main(String [] args) {
	try {
	    NamingService ns = new NamingServiceProxy("mps:inet:localhost:9091:1");
	    LogServerServer lss = new LogServerServer("Logserver", new LogServerImpl());
	    ns.bind("Logserver", lss.getBoundName(), true);
	} catch (Exception e) {

    private int index;

    private LogServerImpl() {
	index = 1;

    public boolean logString(String str) {
	System.out.println(Integer.toString(index++) + ": " + str);
	return true;

The equivalent C++ code can be found at [:cplusplusserver:].

Go to Table of Contents

(last modified 06 August 2001 by surazal) Logo