[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: interface generator - Mudsy MCPDispatch

The mudsy client contains a dispatch generator.  The generator parses an MCP declaration and some Java code fragments to handle the declaration.  It then generates Java code which dispatches based on the mcp message name, parses the arguments into Java objects, and then invokes a method  generated  from the code stub.

The dispatch generator is available in the src/org/mitre/cvw/dispatch directory within  Mudsy ZIP .   I have enclosed MCPDispatch documentation as an attachment.

Richard Godard fixed the negotiation code in Mudsy.  The patch will appear in  Mudsy 0.6 ZIP  very soon.


Title: MCPDispatch Documentation

MCPDispatch Documentation

This program creates an MCP message parser from a prefix file and a method specification file.

Usage: java org.mitre.cvw.dispatch.MCPDispatch prefix-file [method-spec-file]

MCPDispatch helps automate the creation of a Java object which interprets MCP messages. The input to this program consists of two files. The first file, called the prefix file, is an unfinished Java class definition. The second file is a method specification file. MCPDispatch parses the method specification file, copies the first file to its output, and then completes the Java class definition by generating Java methods which process the MCP messages.

A method specification file has an entry for each request to be processed. An entry is syntactially similar to a Java method definition. An entry to handle the display-url request

#$#display-url AUTH url: URL [target: TARGET]
might look like:
// MCP message: #$#display-url AUTH url: URL [target: TARGET]
display_url(String url, String target @null@) 
     throws MalformedURLException
  URL u = new URL(url);
  if (target == null)
    getAppletContext().showDocument(u, target);
The method specification invokes showDocument in response to a display-url request. It assumes the prefix file defines a class which inherits from java.applet.Applet.

MCP requests and keywords are symbols, which may contain hyphens but not dollar signs. When generating a symbol from a Java identifier, underscores are translated into hyphens and then dollar signs are translated into underscore. To specify a symbol separately from the Java identifier, follow the identifier by a colon and the symbol.

// MCP message: #$#class AUTH throws: THROW-LIST
aClass:class(String thr:throws) {

private void ShowWhatYouGot(String str) {
  System.out.println("Got class with throws = " + thr);

There are two types of MCP message parameters, required and optional. Optional parameters have an at-sign delimited default value which is used when an optional parameter is missing. The absence of a required parameter in an MCP message causes an exception to be thrown instead of executing the associated method.

Data types other than strings can be handled if the prefix file contains the appropriate converters. Converter names are constructed by prefixing "string2" to the name of the type. Suggested converters for int's and boolean's follows.

private static int string2int(String str)
     throws NumberFormatException
  return Integer.parseInt(str);
private static boolean string2boolean(String str) {
  return ((str != null) && str.toLowerCase().equals("true"));
They allow method specifications such as:
message(String s, int i, boolean b) { ... }

Arrays can also be handled. Array converter names are constructed by suffixing "_array" to the converter name for the type of the array's elements. Converters for arrays of strings and integers follow.

 * Breaks a string into an array of strings.
 * A space character marks a breaking point.
private static String[] string2String_array(String str) {
  Vector v = new Vector();
  int s = 0;                    // s is the start index

  while (s < str.length()) {
    if (str.charAt(s) == ' ')
    else {
      int e = str.indexOf(' ', s); // e is the end index
      if (e < 0) {
      v.addElement(str.substring(s, e));
      s = e + 1;
  String[] str_array = new String[v.size()];
  return str_array;
private static int[] string2int_array(String str) {
  String[] str_array = string2String_array(str);
  int[] int_array = new int[str_array.length];
  for (int i = 0; i < str_array.length; i++)
    int_array[i] = string2int(str_array[i]);
  return int_array;
They allow method specifications such as:
message2(String s[], int i[]) { ... }

If the method to be used to process a message is defined in the prefix file, the body in the method specification file can be replaced by a semicolon to suppress the generation of a method.

message3(String s);

The input syntax accepted by MCPDispatch is:

input ::= ( method )*
method ::= method-name "(" args ")" ( ";" | body )
method-name ::= name
name ::= ident ( ":" symbol )?
args ::= | arg ( "," arg )*
arg ::= type parameter ( code )?
type ::= ident ( "[]" )?
parameter ::= name
code ::= "@" any text without at-sign "@"
body ::= "{" any text "\n@@@" | "throws" any text "\n@@@"
ident ::= alpha ( jalpha )*
symbol ::= alpha ( xalpha )*
alpha ::= ["a"-"z"] | ["A" - "Z"]
jalpha ::= alpha | ["0"-"9"] | "_" | "$"
xalpha ::= alpha | ["0"-"9"] | "_" | "-"
John D. Ramsdell
E-mail: ramsdell@mitre.org
The MITRE Corporation