File: scamper_cyclemon.c

package info (click to toggle)
scamper 20070523n-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,208 kB
  • ctags: 2,484
  • sloc: ansic: 26,262; makefile: 124
file content (142 lines) | stat: -rw-r--r-- 3,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * scamper_cyclemon: monitor active use of cycle structures so we know when
 *                   to write a cycle-stop record.
 *
 * $Id: scamper_cyclemon.c,v 1.11 2007/05/07 10:03:57 mjl Exp $
 *
 *                 Matthew Luckie
 *
 * Copyright (C) 2006 The University of Waikato
 *
 * 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, version 2.
 *
 * 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
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>

#include <netinet/in.h>

#include <stdlib.h>
#include <string.h>

#if defined(__APPLE__)
#include <stdint.h>
#endif

#include "scamper_list.h"
#include "scamper_outfiles.h"
#include "scamper_task.h"
#include "scamper_addresslist.h"
#include "scamper_cyclemon.h"
#include "utils.h"

struct scamper_cyclemon
{
  struct scamper_cycle      *cycle;
  scamper_cyclemon_finish_t  finish;
  scamper_source_t          *source;
  scamper_outfile_t         *outfile;
  int                        refcnt;
};

scamper_cycle_t *scamper_cyclemon_cycle(const scamper_cyclemon_t *cyclemon)
{
  if(cyclemon != NULL)
    {
      return cyclemon->cycle;
    }
  return NULL;
}

void scamper_cyclemon_source_detach(scamper_cyclemon_t *cyclemon)
{
  cyclemon->source = NULL;
  return;
}

/*
 * scamper_cyclemon_free
 *
 */
void scamper_cyclemon_free(scamper_cyclemon_t *cyclemon)
{
  if(cyclemon == NULL)
    {
      return;
    }

  if(cyclemon->cycle != NULL)
    {
      scamper_cycle_free(cyclemon->cycle);
    }

  if(cyclemon->outfile != NULL)
    {
      scamper_outfile_free(cyclemon->outfile);
    }

  free(cyclemon);
  return;
}

void scamper_cyclemon_unuse(scamper_cyclemon_t *cyclemon)
{
  if(cyclemon == NULL)
    {
      return;
    }

  cyclemon->refcnt--;

  /*
   * if there are still others with a pointer to the cycle monitor, then
   * don't finish the cycle off
   */
  if(cyclemon->refcnt > 0)
    {
      return;
    }

  cyclemon->finish(cyclemon->cycle, cyclemon->source, cyclemon->outfile);

  scamper_cyclemon_free(cyclemon);
  return;
}

scamper_cyclemon_t *scamper_cyclemon_use(scamper_cyclemon_t *cyclemon)
{
  if(cyclemon != NULL) cyclemon->refcnt++;
  return cyclemon;
}

scamper_cyclemon_t *scamper_cyclemon_alloc(scamper_cycle_t *cycle,
					   scamper_cyclemon_finish_t finish,
					   scamper_source_t *source,
					   scamper_outfile_t *outfile)
{
  scamper_cyclemon_t *cyclemon;

  if((cyclemon = malloc_zero(sizeof(scamper_cyclemon_t))) != NULL)
    {
      cyclemon->cycle   = scamper_cycle_use(cycle);
      cyclemon->outfile = scamper_outfile_use(outfile);
      cyclemon->finish  = finish;
      cyclemon->source  = source;
      cyclemon->refcnt  = 1;
    }

  return cyclemon;
}