File size: 5,320 Bytes
68ce2fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-    */
/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */

/*

  Author(s):  Min Yang Jung
  Created on: 2009-09-01

  (C) Copyright 2009-2014 Johns Hopkins University (JHU), All Rights Reserved.

--- begin cisst license - do not edit ---

This software is provided "as is" under an open source license, with
no warranty.  The complete license can be found in license.txt and
http://www.cisst.org/cisst/license.txt.

--- end cisst license ---
*/


/*!
  \file
  \brief Defines the base class for command proxy classes
*/

#ifndef _mtsProxySerializer_h
#define _mtsProxySerializer_h

#include <cisstCommon/cmnSerializer.h>
#include <cisstCommon/cmnDeSerializer.h>
#include <cisstMultiTask/mtsGenericObject.h>

/*!
  \ingroup cisstMultiTask

  This class provides the feature of serialization and deserialization for
  command proxy and function proxy classes.
*/
class mtsProxySerializer {
private:
    /*! Internal buffer for serialization and deserialization. */
    std::stringstream SerializationBuffer;
    std::stringstream DeSerializationBuffer;

    /*! Serializer and Deserializer. */
    cmnSerializer * Serializer;
    cmnDeSerializer * DeSerializer;

public:
    mtsProxySerializer() {
        Serializer = new cmnSerializer(SerializationBuffer);
        DeSerializer = new cmnDeSerializer(DeSerializationBuffer);
    }

    ~mtsProxySerializer() {
        delete Serializer;
        delete DeSerializer;
    }

    bool ServicesSerialized(const cmnClassServicesBase *servicesPointer) const {
        return Serializer->ServicesSerialized(servicesPointer);
    }

    void Reset(void) {
        Serializer->Reset();
        DeSerializer->Reset();
    }

    bool Serialize(const mtsGenericObject & originalObject, std::string & serializedObject) {
        try {
            SerializationBuffer.str("");
            Serializer->Serialize(originalObject);
            serializedObject = SerializationBuffer.str();
        } catch (const std::runtime_error &e) {
            CMN_LOG_RUN_ERROR << "Serialization failed: " << originalObject.ToString() << std::endl;
            CMN_LOG_RUN_ERROR << e.what() << std::endl;
            serializedObject = "";
            return false;
        }
        return true;
    }

    bool SerializeStart(const mtsGenericObject & originalObject) {
        try {
            SerializationBuffer.str("");
            Serializer->Serialize(originalObject);
        } catch (const std::runtime_error &e) {
            CMN_LOG_RUN_ERROR << "Serialization failed: " << originalObject.ToString() << std::endl;
            CMN_LOG_RUN_ERROR << e.what() << std::endl;
            return false;
        }
        return true;
    }

    bool SerializeNext(const mtsGenericObject & originalObject) {
        try {
            Serializer->Serialize(originalObject);
        } catch (const std::runtime_error &e) {
            CMN_LOG_RUN_ERROR << "Serialization failed: " << originalObject.ToString() << std::endl;
            CMN_LOG_RUN_ERROR << e.what() << std::endl;
            return false;
        }
        return true;
    }

    bool SerializeEnd(const mtsGenericObject & originalObject, std::string & serializedObject) {
        try {
            Serializer->Serialize(originalObject);
            serializedObject = SerializationBuffer.str();
        } catch (const std::runtime_error &e) {
            CMN_LOG_RUN_ERROR << "Serialization failed: " << originalObject.ToString() << std::endl;
            CMN_LOG_RUN_ERROR << e.what() << std::endl;
            serializedObject = "";
            return false;
        }
        return true;
    }

    bool DeSerialize(const std::string & serializedObject, mtsGenericObject & originalObject) {
        try {
            DeSerializationBuffer.str("");
            DeSerializationBuffer << serializedObject;
            DeSerializer->DeSerialize(originalObject);
        }  catch (const std::runtime_error &e) {
            originalObject.SetValid(false);
            CMN_LOG_RUN_ERROR << "DeSerialization failed: " << e.what() << std::endl;
            return false;
        }
        return true;
    }

    // DeSerialize the next object in the string that was passed to DeSerialize
    bool DeSerializeNext(mtsGenericObject & originalObject) {
        try {
            DeSerializer->DeSerialize(originalObject);
        }  catch (const std::runtime_error &e) {
            originalObject.SetValid(false);
            CMN_LOG_RUN_ERROR << "DeSerializeNext failed: " << e.what() << std::endl;
            return false;
        }
        return true;
    }

    // MJ: This method internally allocates memory. Caller should deallocate it.
    mtsGenericObject * DeSerialize(const std::string & serializedObject) {
        cmnGenericObject * deserializedObject = 0;
        try {
            DeSerializationBuffer.str("");
            DeSerializationBuffer << serializedObject;
            deserializedObject = DeSerializer->DeSerialize();
        }  catch (const std::runtime_error &e) {
            CMN_LOG_RUN_ERROR << "DeSerialization failed: " << e.what() << std::endl;
            return 0;
        }

        return dynamic_cast<mtsGenericObject*>(deserializedObject);
    }
};

#endif // _mtsProxySerializer_h