CPN
Computational Process Networks
PacketHeader.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
30 #ifndef PACKET_H
31 #define PACKET_H
32 #pragma once
33 
34 #include "common_priv.h"
35 #include <stdint.h>
36 #include <cstring>
37 
38 namespace CPN {
39 
40  // All the packet headers are PODs so they can be unioned.
41 
44  enum {
45  PACKET_SYNCWORD = 0xF1F0C0DE,
48  };
49 
53  enum PacketType_t {
64 
67  };
68 
70  union {
71  struct {
72  uint32_t syncWord;
73  uint32_t dataLength;
74  uint32_t dataType;
75 
76  union {
77  uint32_t requested; // read/write block packets
78  uint32_t maxThresh; // Only for grow packet
79  uint32_t count; // Enqueue/Dequeue packets
80  };
81  union {
82  struct { // For ID packet
83  uint64_t srckey;
84  uint64_t dstkey;
85  };
86  struct {
87  uint64_t readclock;
88  uint64_t writeclock;
89  };
90  };
91 
92  uint32_t queueSize; // only used for grow packet
93  };
94  uint8_t pad[PACKET_HEADERLENGTH];
95  };
96  };
97 
98  inline bool ValidPacket(const PacketHeader *ph) CPN_LOCAL {
99  return ph->syncWord == PACKET_SYNCWORD;
100  }
101 
102  inline void InitPacket(PacketHeader *ph, uint32_t datalen, PacketType_t type) CPN_LOCAL {
103  memset(ph, 0, sizeof(PacketHeader));
104  ph->syncWord = PACKET_SYNCWORD;
105  ph->dataLength = datalen;
106  ph->dataType = type;
107  }
108 
113  public:
114  Packet() { memset(&header, 0, sizeof(header)); }
115  Packet(uint32_t datalen, PacketType_t type) { Init(datalen, type); }
116  Packet(PacketType_t type) { Init(0, type); }
117  Packet(const PacketHeader &ph) : header(ph) {}
118 
119  Packet &Init(uint32_t datalen, PacketType_t type) {
120  InitPacket(&header, datalen, type);
121  return *this;
122  }
123 
124  uint32_t DataLength() const { return header.dataLength; }
125  PacketType_t Type() const { return static_cast<PacketType_t>(header.dataType); }
126  uint32_t QueueSize() const { return header.queueSize; }
127  uint64_t SourceKey() const { return header.srckey; }
128  uint64_t DestinationKey() const { return header.dstkey; }
129  uint32_t Requested() const { return header.requested; }
130  uint32_t MaxThreshold() const { return header.maxThresh; }
131  uint32_t Count() const { return header.count; }
132  uint64_t ReadClock() const { return header.readclock; }
133  uint64_t WriteClock() const { return header.writeclock; }
134  bool Valid() const { return ValidPacket(&header); }
135 
136  Packet &DataLength(uint32_t dl) { header.dataLength = dl; return *this; }
137  Packet &Type(PacketType_t t) { header.dataType = t; return *this; }
138  Packet &QueueSize(uint32_t qs) { header.queueSize = qs; return *this; }
139  Packet &SourceKey(uint64_t k) { header.srckey = k; return *this; }
140  Packet &DestinationKey(uint64_t k) { header.dstkey = k; return *this; }
141  Packet &Requested(uint32_t r) { header.requested = r; return *this; }
142  Packet &MaxThreshold(uint32_t mt) { header.maxThresh = mt; return *this; }
143  Packet &Count(uint32_t cnt) { header.count = cnt; return *this; }
144  Packet &ReadClock(uint64_t c) { header.readclock = c; return *this; }
145  Packet &WriteClock(uint64_t c) { header.writeclock = c; return *this; }
146 
147  public:
149  };
150 
155  public:
156  virtual ~PacketHandler();
157  void FirePacket(const Packet &packet);
158 
159  virtual void EnqueuePacket(const Packet &packet) = 0;
160  virtual void DequeuePacket(const Packet &packet) = 0;
161  virtual void ReadBlockPacket(const Packet &packet) = 0;
162  virtual void WriteBlockPacket(const Packet &packet) = 0;
163  virtual void EndOfWritePacket(const Packet &packet) = 0;
164  virtual void EndOfReadPacket(const Packet &packet) = 0;
165  virtual void GrowPacket(const Packet &packet) = 0;
166  virtual void D4RTagPacket(const Packet &packet) = 0;
167  virtual void FlushPacket(const Packet &packet) = 0;
168  virtual void ResetPacket(const Packet &packet) = 0;
169  virtual void IDReaderPacket(const Packet &packet) = 0;
170  virtual void IDWriterPacket(const Packet &packet) = 0;
171  };
172 }
173 
174 #endif
175 
Packet & Type(PacketType_t t)
Definition: PacketHeader.h:137
Packet(PacketType_t type)
Definition: PacketHeader.h:116
uint32_t QueueSize() const
Definition: PacketHeader.h:126
PacketHeader header
Definition: PacketHeader.h:148
uint64_t writeclock
Definition: PacketHeader.h:88
uint32_t Count() const
Definition: PacketHeader.h:131
Packet & WriteClock(uint64_t c)
Definition: PacketHeader.h:145
uint64_t WriteClock() const
Definition: PacketHeader.h:133
uint32_t DataLength() const
Definition: PacketHeader.h:124
uint64_t SourceKey() const
Definition: PacketHeader.h:127
uint32_t MaxThreshold() const
Definition: PacketHeader.h:130
Packet & Init(uint32_t datalen, PacketType_t type)
Definition: PacketHeader.h:119
uint64_t DestinationKey() const
Definition: PacketHeader.h:128
uint64_t ReadClock() const
Definition: PacketHeader.h:132
#define CPN_LOCAL
Definition: common.h:37
Packet & SourceKey(uint64_t k)
Definition: PacketHeader.h:139
Packet(const PacketHeader &ph)
Definition: PacketHeader.h:117
uint32_t requested
Definition: PacketHeader.h:77
Packet & MaxThreshold(uint32_t mt)
Definition: PacketHeader.h:142
Packet & QueueSize(uint32_t qs)
Definition: PacketHeader.h:138
PacketType_t Type() const
Definition: PacketHeader.h:125
Packet & Requested(uint32_t r)
Definition: PacketHeader.h:141
Packet & Count(uint32_t cnt)
Definition: PacketHeader.h:143
Packet & DestinationKey(uint64_t k)
Definition: PacketHeader.h:140
PacketType_t
Definition: PacketHeader.h:53
Packet & DataLength(uint32_t dl)
Definition: PacketHeader.h:136
uint32_t maxThresh
Definition: PacketHeader.h:78
uint32_t dataLength
Definition: PacketHeader.h:73
Packet & ReadClock(uint64_t c)
Definition: PacketHeader.h:144
uint64_t readclock
Definition: PacketHeader.h:87
uint32_t Requested() const
Definition: PacketHeader.h:129
Packet(uint32_t datalen, PacketType_t type)
Definition: PacketHeader.h:115
uint32_t queueSize
Definition: PacketHeader.h:92
void InitPacket(PacketHeader *ph, uint32_t datalen, PacketType_t type) CPN_LOCAL
Definition: PacketHeader.h:102
bool Valid() const
Definition: PacketHeader.h:134
bool ValidPacket(const PacketHeader *ph) CPN_LOCAL
Definition: PacketHeader.h:98