Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators:
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *
 * Authors     :
 *   Paolo Gai           <pj@gandalf.sssup.it>
 *   Massimiliano Giorgi <massy@gandalf.sssup.it>
 *   Luca Abeni          <luca@gandalf.sssup.it>
 *   (see the web pages for full authors list)
 *
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */


/*
 * Copyright (C) 2000 Massimiliano Giorgi
 * Copyright (C) 2002 Paolo Gai
 * Copyright (C) 2002 Tomas Lenvall
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * CVS :        $Id: udpdump.c,v 1.1.1.1 2004-05-24 18:03:44 giacomo Exp $
 */


/* this example simply prints a Shark trace file */

// INCLUDES
#include <netinet/in.h>
 
#include "types.h"
#include "trace.h"
#include "util.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>/* close() */
#include <string.h> /* memset() */
#include <stdlib.h>


// GLOBALS
char server_ipaddr[20]; // Store the IP adress of the server

// DEFINES
#define LOCAL_SERVER_PORT 20000
#define MAX_MSG 10000

// FUNCTIONS
/* */
int dumpsys(trc_system_event_t *sys)
{
  printf("%02i\n",sys->task);
  return 0;
}

/* */
int dumpusr(trc_user_event_t *usr)
{
  printf("%8li ",usr->n);
  printf("\n");
  return 0;
}

/* */
int dumpsem(trc_sem_event_t *sem)
{
  printf("on [%i]\n",sem->id);
  return 0;
}

/* */
int dumpfunc(trc_event_t *ev)
{
  static int counter=0;
 
  printf("\t%4i ",counter);
  counter++;
  printf("%12s ",format_time(ev->time));
  printf("%-10s ",event_name(ev->event));

  //printf("%08x\n",(unsigned)ev->sys.event);
  //return 0;
 
  switch(event_class(ev->event)) {
    case TRC_CLASS_SYSTEM: return dumpsys(&ev->x.sys);
    case TRC_CLASS_USER:   return dumpusr(&ev->x.usr);
    case TRC_CLASS_SEM:    return dumpsem(&ev->x.sem);
  }
  printf("\nEVENT %i... CLASS %i UNKNOWN!\n",ev->event,event_class(ev->event));
  return -1;
}


/* Use UDP/IP to receive the events from the client computer */
int main(int argc, char *argv[])
{
  int sd, rc, n, cliLen;
  struct sockaddr_in cliAddr, servAddr;
  char msg[MAX_MSG];

  // socket creation
  sd = socket(AF_INET, SOCK_DGRAM, 0);
  if(sd < 0) {
    printf("%s: cannot open socket \n",argv[0]);
    exit(1);
  }

  // bind local server port
  servAddr.sin_family = AF_INET;

  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servAddr.sin_port = htons(LOCAL_SERVER_PORT);

  rc = bind (sd, (struct sockaddr *)&servAddr,sizeof(servAddr));
  if(rc < 0) {
    printf("%s: cannot bind port number %d \n",
           argv[0], LOCAL_SERVER_PORT);
    exit(1);
  }

  while(1){
   
    // init buffer
    memset(msg, 0x0, MAX_MSG);

    // receive message
    cliLen = sizeof(cliAddr);
    n = recvfrom(sd, msg, MAX_MSG, 0,(struct sockaddr *)&cliAddr, &cliLen);

    printf("Packet received, length %d, %d tracer events.\n", n,
           *((short int *)msg));
   
    if(n < 0) {
      printf("%s: cannot receive data \n",argv[0]);
      continue;
    }
     
    // Read the trace we got from the network
    read_udp_trace(msg, dumpfunc);
  }

  return 0;
}


// test main: testing this program from a file (simulating a network package)
// ONLY USED FOR TESTING!!!

/*int main(int argc, char *argv[])
{
  FILE* fin;
  int nr_of_events = 0;
  trc_event_t buffer;
  char all_events[1000];
  char tmpbuf[2];

   if(argc != 2) {
    printf("Missing filename!\n");
    return -1;
  }

  // open the file and send event by event
  fin = fopen(argv[1], "r");
  if (fin == NULL) {
    printf("Cannot open file\n");
    return -1;
  }
 
  // check how many events
  while (!feof(fin)) {
    fread(&buffer, sizeof(trc_event_t), 1, fin);
    nr_of_events++;
  }

  //printf("Nr of events: %d\n", nr_of_events);

  rewind(fin); // start from the beginning again...
  //strncpy(tmpbuf, itoa(nr_of_events), 2);
  sprintf(tmpbuf, "%d", nr_of_events);
  //printf("tmpbuf: %s\n", tmpbuf);

  memset(all_events, 0, 1000);
  memcpy(all_events, tmpbuf, 2);
  printf("all_events(1): %s\n", all_events);
  // read all tracer events and store them in an array
  fread(all_events + 2, sizeof(trc_event_t), nr_of_events, fin);
  fclose(fin);

  printf("all_events(2): %s\n", all_events);
  // send it to the event reader
  read_trace(all_events, dumpfunc);

  return 1;  
}
*/