/*
    Copyright 2000 (c) by Claudio Jeker,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public 
    Licence. A full copy of the GNU GPL is provided in the file COPYING 
    found in the development root of Topsy.
*/
/*
	
    File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/Net/NetModulesNew.c,v $
    Author(s):             
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Creation Date:         
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $  by: $Author: baderma $
 
*/
#include "NetModulesNew.h"
#include "NetModulesDef.h"
#include "NetDebug.h"
#include "Syscall.h"
#include "Support.h"

void netmodSend(LWPool lw, NetModuleId to,  NetBuf data, NetAttrs meta)
{
  Message msg;

  msg.id = NET_QUEUE_DATA;
  msg.msg.netqMsg.netData = data;
  msg.msg.netqMsg.netMeta = meta;
  msg.msg.netqMsg.netId = to;

  //	printf("###### LW SEND 1 lw %p\n", lw);
  if ( lwMsgSend(lw, NET_MODULE_LOOKUP_THREAD, &msg) != LW_OK )
  {
    netdbgPrintf(0, "netmodSend: failed to send message to lookup module\n");
    tmExit();
  }
  //	printf("###### LW SEND 2 lw %p\n", lw);
  return;
}

void netmodRecv(LWPool lw, NetBuf *data, NetAttrs *meta)
{
  Message msg;
  ThreadId id;

  if ( lwMsgRecv(lw, &id, 0, &msg, 0 ) != LW_OK )
  {
    netdbgPrintf(0, "netmodRecv: failed to recv message\n");
    tmExit();
  }
  if ( msg.id != NET_QUEUE_DATA )
  {
    netdbgPrintf(0, "netmodRecv: recieved bad message %i from %i\n",
                 (int) msg.id, (int) msg.from);
    tmExit();
  }
  if ( data )
    *data = msg.msg.netqMsg.netData;
  if ( meta )
    *meta = msg.msg.netqMsg.netMeta;
}

void netmodRIP(LWPool lw, NetBuf data, NetAttrs meta, char *s)
{
  Message msg;

  if ( s ) //netdbgPrintf(0, "%s", s);
    printf("%s", s);
  netbufFree(data);
  NETATTR_FREE(meta);

  msg.id = NET_QUEUE_DATA;
  msg.msg.netqMsg.netData = NULL;
  msg.msg.netqMsg.netMeta = NULL;

  if ( lwMsgSend(lw, 0, &msg) != LW_OK )
  {
    netdbgPrintf(0, "netmodSend: failed to send message to main thread\n");
    tmExit();
  }
}

