package kraft.gateway;
import  kraft.TermInString;

//
// Message format class for JAVA
// Author:	K.Y. Hui
// Version:	1.20
// Date:	10 July 1998
//
//
// this is a message-format specific class that understands message format
// (aka internal data structure on the Linda server)
//
// use it to compose/decompose messages and receiving templates
//

public class MessagingHandler
{
public static final String VERSION="1.10";	//messaging version no.

//
// get the receiver from a message in string
//
public static String get_message_receiver(String s)
	{
	TermInString term,context;

	try	{
		term=new TermInString(s);			//create prolog term from string
		return(get_message_receiver(term));
		} catch (Exception e)
			{
			return(null);
			}
	}

//
// get the receiver from a message in term
//
public static String get_message_receiver(TermInString term)
	{
	TermInString context;

	try	{
		context=new TermInString(term.argument(1));	//make 1st argument into a term
		return(context.argument(4));			//receiver is the 3rd arg in context
		} catch (Exception e)
			{
			return(null);
			}
	}


//
// get the sender from context
//

public static String get_message_sender(String s)
	{
	TermInString term,context;

	try	{
		term=new TermInString(s);			//create prolog term from string
		return(get_message_sender(term));
		} catch (Exception e)
			{
			return(null);
			}
	}


//
// get the sender from context
//
public static String get_message_sender(TermInString term)
	{
	TermInString context;

	try	{
		context=new TermInString(term.argument(1));	//make 1st argument into a term
		return(context.argument(3));			//sender is the 2nd arg in context
		} catch (Exception e)
			{
			return(null);
			}
	}


//
// get a KRAFT name's site from a string
//
public static String get_name_site(String s)
	{
	TermInString term;

	try	{
		term=new TermInString(s);			//create a term out of it
		return(get_name_site(term));			//call the overloaded class method
		} catch (Exception e)
			{
			return(null);
			}
	}

//
// get a KRAFT name's site from a term
//
public static String get_name_site(TermInString name)
	{
	if (!name.functor.equals("krl") || name.arity!=2)	//check for functor "krl" & arity 2
		return(null);
	return(name.argument(1));				//return 1st argument of term
	}

//
// get msg core from a message in a string
//
public static String get_core(String s)
	{
	TermInString term;

	try	{
		term=new TermInString(s);			//create prolog term from string
		return(get_core(term));
		} catch (Exception e)
			{
			return(null);
			}
	}

//
// get msg core from a term
//
public static String get_core(TermInString term)
	{
	return(term.argument(2));			//2nd argument is core
	}


//
// given the reciever's name
// compose the Linda "in" template
//
// kraft_msg(context(Version,Sender,Receiver,Time),ccql(_))
//
public static String compose_in_template_from_receiver_name(String receiver)
	{
	return("kraft_msg(context(1,_,_,"+receiver+",_),_)");
	}

//
// given the site
// compose the Linda "in template
//
public static String compose_in_template_from_receiving_site(String site)
	{
	return("kraft_msg(context(1,_,_,krl("+site+",_),_),_)");
	}
}

