Skip to content

Commit 7f722c4

Browse files
committed
Add source code for project.
1 parent 357ba44 commit 7f722c4

17 files changed

+2800
-0
lines changed

DiskBuffer.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* File: DiskBuffer.h
3+
* Class: ICS 451
4+
* Project #: 3
5+
* Team Members: Bryce Groff, Brandon Grant, Emiliano Miranda
6+
* Author: Bryce Groff
7+
* Created Date: 04-23-09
8+
* Desc: Creates a buffer created on disk that can be written to.
9+
*/
10+
#include <iostream>
11+
#include <fstream>
12+
#include <inttypes.h>
13+
#include <sys/mman.h>
14+
15+
#include "DiskBuffer.h"
16+
17+
/*!
18+
\param fileName The filename that will be used to store the information.
19+
\param fileSize The total size of the file to be stored.
20+
\param nextSeq The starting sequence number.
21+
*/
22+
DiskBuffer::DiskBuffer(string &fileName, uint64_t fileSize, uint64_t nextSeq)
23+
: mFileName(fileName), mFileSize(fileSize), mNextSeq(nextSeq)
24+
{
25+
// Open the file for writing
26+
try {
27+
mFile.open(mFileName.c_str(), fstream::out);
28+
}
29+
catch (ios_base::failure &e) {
30+
cerr<<"DiskBuffer [Constructor]: "<<e.what()<<endl;
31+
}
32+
}
33+
34+
DiskBuffer::~DiskBuffer()
35+
{
36+
mFile.close();
37+
}
38+
39+
/*!
40+
\brief Adds data to the DiskBuffer. If the data is in order, the
41+
data will be added and the out of order cache will be iterated over
42+
to find any data that may come next. If the data is out of order it will be
43+
put in the OutOfSeqCache
44+
\sa Data
45+
\param &packet The packet that you wish to add to the buffer.
46+
\return The amount of data saved to disk.
47+
*/
48+
uint64_t DiskBuffer::Add(Data &packet)
49+
{
50+
uint64_t fSeqNum = packet.mSeqNum;
51+
uint32_t fSize = packet.mPacketSize;
52+
53+
// If the sequence number is the number that we
54+
// are expecting to receive then process the data.
55+
if (mNextSeq == fSeqNum) {
56+
// TODO: This can probably be finner grain.
57+
//mWriteLock.Lock();
58+
59+
try {
60+
mFile.write(packet.mData, (streamsize)fSize);
61+
}
62+
catch (ios_base::failure &e) {
63+
cerr<<"DiskBuffer [Add]: "<<e.what()<<endl;
64+
}
65+
66+
// Increment the current sequence number by the size of the packet.
67+
// This should be the next sequence number. Then loop over the out
68+
// of order cache and add as much data as we have.
69+
mNextSeq += fSize;
70+
Data *fNext = mOutOfSeqCache.GetData(mNextSeq);
71+
while (fNext != NULL) {
72+
// Write the data.
73+
mFile.flush();
74+
mFile.write(fNext->mData, (streamsize)fNext->mPacketSize);
75+
fSize += fNext->mPacketSize;
76+
77+
// Increment the pointer
78+
mNextSeq += fNext->mPacketSize;
79+
80+
// Free the memory.
81+
delete [] fNext->mData;
82+
delete fNext;
83+
84+
fNext = mOutOfSeqCache.GetData(mNextSeq);
85+
}
86+
87+
//mWriteLock.Unlock();
88+
}
89+
90+
// If the sequence number is greater than the number we are expecting
91+
// then add the data to the out of order cache.
92+
else if (mNextSeq < fSeqNum) {
93+
// Lock so we are not adding data while trying to remove above.
94+
//mWriteLock.Lock();
95+
mOutOfSeqCache.Add(packet);
96+
//mWriteLock.Unlock();
97+
//fSize = 0;
98+
}
99+
100+
// return how many bytes we saved to disk.
101+
return fSize;
102+
}
103+
104+
/*!
105+
\brief Returns the next sequence number that DiskBuffer is expecting.
106+
\return mNextSeq
107+
*/
108+
uint64_t DiskBuffer::GetNextSeq()
109+
{
110+
return mNextSeq;
111+
}
112+
113+
/*!
114+
\brief Returns the window size to throttle back the sender.
115+
\return the window size
116+
*/
117+
uint32_t DiskBuffer::GetWindowSize()
118+
{
119+
//TODO: Make this do something useful
120+
return 0xFFFFFFFF;
121+
}
122+
123+
/*!
124+
\brief Flushes all the data that is in memory but not yet on the disk.
125+
\return Nothing
126+
*/
127+
void DiskBuffer::Flush()
128+
{
129+
try {
130+
mFile.flush();
131+
}
132+
catch (ios_base::failure &e) {
133+
cerr<<"DiskBuffer [Flush]: "<<e.what()<<endl;
134+
}
135+
}

DiskBuffer.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* File: DiskBuffer.h
3+
* Class: ICS 451
4+
* Project #: 3
5+
* Team Members: Bryce Groff, Brandon Grant, Emiliano Miranda
6+
* Author: Bryce Groff
7+
* Created Date: 04-23-09
8+
* Desc: Creates a buffer created on disk that can be written to.
9+
*/
10+
#ifndef _DISKBUFFER_H_
11+
#define _DISKBUFFER_H_
12+
#include <iostream>
13+
#include <fstream>
14+
#include <inttypes.h>
15+
16+
#include "Transmission.h"
17+
#include "OutOfSeqCache.h"
18+
#include "Mutex.h"
19+
20+
using namespace std;
21+
22+
/*! \class DiskBuffer
23+
\brief Represents a place to store large amounts of information.
24+
25+
DiskBuffer opens a file on the disk and performs writes to the file.
26+
Allows one to Add data, Get the sequence number of the last processed packet,
27+
and has the ability to flush what is in memory to disk.
28+
*/
29+
class DiskBuffer {
30+
public:
31+
DiskBuffer(string &fileName, uint64_t fileSize, uint64_t nextSeq);
32+
virtual ~DiskBuffer();
33+
34+
uint64_t Add(Data &);
35+
uint64_t GetNextSeq();
36+
uint32_t GetWindowSize();
37+
void Flush();
38+
39+
private:
40+
//Mutex mWriteLock;
41+
OutOfSeqCache mOutOfSeqCache;
42+
43+
fstream mFile;
44+
std::string mFileName;
45+
uint64_t mFileSize;
46+
uint64_t mNextSeq;
47+
uint32_t mWindowSize;
48+
};
49+
#endif

Mutex.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* File: Mutex.cpp
3+
* Class: ICS 451
4+
* Project #: 3
5+
* Team Members: Bryce Groff, Brandon Grant, Emiliano Miranda
6+
* Author: Bryce Groff
7+
* Created Date: 04-19-09
8+
* Desc: Represents a Lock.
9+
*/
10+
11+
#include "Mutex.h"
12+
13+
Mutex::Mutex()
14+
{
15+
pthread_mutex_init(&mLock, NULL);
16+
pthread_cond_init(&mCond, NULL);
17+
}
18+
19+
Mutex::Mutex(int type)
20+
{
21+
pthread_mutexattr_init(&mAttr);
22+
pthread_mutexattr_settype(&mAttr, type);
23+
pthread_mutex_init(&mLock, &mAttr);
24+
pthread_cond_init(&mCond, NULL);
25+
}
26+
27+
Mutex::~Mutex()
28+
{
29+
pthread_mutex_destroy(&mLock);
30+
pthread_mutexattr_destroy(&mAttr);
31+
pthread_cond_destroy(&mCond);
32+
}
33+
34+
int Mutex::Lock()
35+
{
36+
return pthread_mutex_lock(&mLock);
37+
}
38+
39+
int Mutex::TryLock()
40+
{
41+
return pthread_mutex_trylock(&mLock);
42+
}
43+
44+
int Mutex::Unlock()
45+
{
46+
return pthread_mutex_unlock(&mLock);
47+
}
48+
49+
int Mutex::TimedWait(timespec &waitTime)
50+
{
51+
return pthread_cond_timedwait(&mCond, &mLock, &waitTime);
52+
}
53+
54+
int Mutex::Wait()
55+
{
56+
return pthread_cond_wait(&mCond, &mLock);
57+
}
58+
59+
int Mutex::Signal()
60+
{
61+
return pthread_cond_signal(&mCond);
62+
}

Mutex.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* File: Mutex.h
3+
* Class: ICS 451
4+
* Project #: 3
5+
* Team Members: Bryce Groff, Brandon Grant, Emiliano Miranda
6+
* Author: Bryce Groff
7+
* Created Date: 04-19-09
8+
* Desc: Represents a Lock.
9+
*/
10+
#ifndef _MUTEX_H_
11+
#define _MUTEX_H_
12+
13+
#include <pthread.h>
14+
#include <inttypes.h>
15+
#include <time.h>
16+
17+
/*! \class Mutex
18+
\brief Wrapper around pthread locks.
19+
20+
Mutex gives a wrapper around the pthread mutual exclusion mechanism.
21+
*/
22+
class Mutex {
23+
public:
24+
Mutex();
25+
Mutex(int type);
26+
~Mutex();
27+
28+
int Lock(); //!< Locks this lock.
29+
int TryLock(); //!< Tries to lock this lock. If 0 is returned, then the lock is required; otherwise, an error number is returned.
30+
int Unlock(); //!< Unlocks this lock.
31+
int TimedWait(timespec &); //!< Waits for timespec amount of time then timesout
32+
int Wait(); //!< Waits until signaled
33+
int Signal(); //!< Signals the condition variable to continue.
34+
35+
private:
36+
pthread_mutex_t mLock;
37+
pthread_cond_t mCond;
38+
pthread_mutexattr_t mAttr;
39+
};
40+
#endif

OutOfSeqCache.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* File: OutOfSeqCache.h
3+
* Class: ICS 451
4+
* Project #: 3
5+
* Team Members: Bryce Groff, Brandon Grant, Emiliano Miranda
6+
* Author: Bryce Groff
7+
* Created Date: 04-23-09
8+
* Desc: Creates cache to hold out of order data.
9+
*/
10+
#include <map>
11+
#include <utility>
12+
13+
#include "OutOfSeqCache.h"
14+
#include "Transmission.h"
15+
16+
/*!
17+
\brief Constructor does nothing.
18+
*/
19+
OutOfSeqCache::OutOfSeqCache()
20+
{
21+
}
22+
23+
OutOfSeqCache::~OutOfSeqCache()
24+
{
25+
}
26+
27+
/*!
28+
\brief Adds data to the OutOfSeqCache. Remeber that you must free the data that
29+
gets passed in to the OutOfSeqCache.
30+
\sa Data
31+
\param &packet The packet that you wish to add to the buffer. The data of the packet
32+
must be on the heap.
33+
\return Nothing
34+
*/
35+
void OutOfSeqCache::Add(Data packet)
36+
{
37+
DataMap::iterator fIter = mMap.begin();
38+
fIter = mMap.find(packet.mSeqNum);
39+
40+
// Make sure we didn't already add this packet.
41+
if (fIter == mMap.end())
42+
{
43+
// Make sure that we copy the data so that when the execution continues
44+
// running we do not loose the data.
45+
Data *fData = new Data();
46+
fData->mSeqNum = packet.mSeqNum;
47+
fData->mPacketSize = packet.mPacketSize;
48+
fData->mData = new char[packet.mPacketSize];
49+
memcpy(fData->mData, packet.mData, packet.mPacketSize);
50+
51+
// Add the packet to the map, use the seqNum as a lookup.
52+
mMap.insert(pair<uint64_t, Data*>(packet.mSeqNum, fData));
53+
}
54+
}
55+
56+
/*!
57+
\brief Iterates through the list of packet and either returns the
58+
packet if we can find it, or return NULL if we cannot.
59+
\param &seqNum The sequence number to search for.
60+
\return The packet or NULL if the data was not found.
61+
*/
62+
Data *OutOfSeqCache::GetData(uint64_t seqNum)
63+
{
64+
Data *fVal = NULL;
65+
66+
// find will return an iterator to the matching element if it is found
67+
// or to the end of the map if the key is not found
68+
DataMap::iterator fIter = mMap.begin();
69+
fIter = mMap.find(seqNum);
70+
if( fIter != mMap.end() ) {
71+
fVal = fIter->second;
72+
mMap.erase(fIter);
73+
}
74+
75+
// If we fall through return NULL.
76+
return fVal;
77+
}

0 commit comments

Comments
 (0)