Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / parsim / cparsimprotocolbase.cc @ 81ad8b66

History | View | Annotate | Download (3.04 KB)

1
//=========================================================================
2
//  CPARSIMPROTOCOLBASE.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga, 2003
8
//          Dept. of Electrical and Computer Systems Engineering,
9
//          Monash University, Melbourne, Australia
10
//
11
//=========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2003-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20

    
21

    
22
#include "cmessage.h"
23
#include "cmodule.h"
24
#include "cgate.h"
25
#include "cenvir.h"
26
#include "cparsimpartition.h"
27
#include "cparsimcomm.h"
28
#include "ccommbuffer.h"
29
#include "messagetags.h"
30
#include "cparsimprotocolbase.h"
31

    
32
USING_NAMESPACE
33

    
34

    
35

    
36
cParsimProtocolBase::cParsimProtocolBase() : cParsimSynchronizer()
37
{
38
}
39

    
40
cParsimProtocolBase::~cParsimProtocolBase()
41
{
42
}
43

    
44
void cParsimProtocolBase::processOutgoingMessage(cMessage *msg, int destProcId, int destModuleId, int destGateId, void *)
45
{
46
    cCommBuffer *buffer = comm->createCommBuffer();
47

    
48
    buffer->pack(destModuleId);
49
    buffer->pack(destGateId);
50
    buffer->packObject(msg);
51
    comm->send(buffer, TAG_CMESSAGE, destProcId);
52

    
53
    comm->recycleCommBuffer(buffer);
54
}
55

    
56
void cParsimProtocolBase::processReceivedBuffer(cCommBuffer *buffer, int tag, int sourceProcId)
57
{
58
    int destModuleId;
59
    int destGateId;
60
    cMessage *msg;
61

    
62
    switch (tag)
63
    {
64
        case TAG_CMESSAGE:
65
            buffer->unpack(destModuleId);
66
            buffer->unpack(destGateId);
67
            msg = (cMessage *)buffer->unpackObject();
68
            processReceivedMessage(msg, destModuleId, destGateId, sourceProcId);
69
            break;
70

    
71
        default:
72
            partition->processReceivedBuffer(buffer, tag, sourceProcId);
73
            break;
74
    }
75
    buffer->assertBufferEmpty();
76
}
77

    
78
void cParsimProtocolBase::processReceivedMessage(cMessage *msg, int destModuleId, int destGateId, int sourceProcId)
79
{
80
    partition->processReceivedMessage(msg, destModuleId, destGateId, sourceProcId);
81
}
82

    
83
void cParsimProtocolBase::receiveNonblocking()
84
{
85
    int tag, sourceProcId;
86
    cCommBuffer *buffer = comm->createCommBuffer();
87
    while (comm->receiveNonblocking(PARSIM_ANY_TAG, buffer, tag, sourceProcId))
88
        processReceivedBuffer(buffer, tag, sourceProcId);
89
    comm->recycleCommBuffer(buffer);
90
}
91

    
92
bool cParsimProtocolBase::receiveBlocking()
93
{
94
    cCommBuffer *buffer = comm->createCommBuffer();
95

    
96
    int tag, sourceProcId;
97
    if (!comm->receiveBlocking(PARSIM_ANY_TAG, buffer, tag, sourceProcId))
98
    {
99
        comm->recycleCommBuffer(buffer);
100
        return false;
101
    }
102

    
103
    processReceivedBuffer(buffer, tag, sourceProcId);
104
    while (comm->receiveNonblocking(PARSIM_ANY_TAG, buffer, tag, sourceProcId))
105
        processReceivedBuffer(buffer, tag, sourceProcId);
106

    
107
    comm->recycleCommBuffer(buffer);
108
    return true;
109
}
110