MessageSplitter.cpp
Go to the documentation of this file.
1 #include <rms/provider/MessageSplitter.h>
2 
3 #include <iostream>
4 #include <cmath>
5 
6 namespace gov {
7 
8 namespace fnal {
9 
10 namespace cd {
11 
12 namespace rms {
13 
14 namespace provider {
15 
16 /**
17  * Split a long string message into several message fragments
18  * that can be serialized.
19  *
20  * @param messageText The message to split
21  * @param maxFragmentSize The maximum size of the message fragments
22  * that will be generated.
23  * @param uuid The UUID of the message
24  *
25  * @return A vector of message fragments
26  */
27 std::vector <MessageFragment *>
29  int maxFragmentSize,
30  const char *uuid) {
31  std::vector <MessageFragment *> messageFragments;
32  std::string stringFragment;
33  MessageFragment *fragment;
34  int index;
35 
36  maxFragmentSize -= MessageFragment::getMaxHeaderSize();
37 
38  if (maxFragmentSize < 1) {
39  return messageFragments;
40  }
41 
42  int bytesRemaining = messageText.length();
43 
44  int numFragments =
45  (int) ceil((float)messageText.length() / maxFragmentSize);
46 
47  for (index = 0; bytesRemaining > 0; index++) {
48  if (bytesRemaining > maxFragmentSize) {
49  stringFragment =
50  messageText.substr(index * maxFragmentSize, maxFragmentSize);
51  }
52  else {
53  stringFragment =
54  messageText.substr(index * maxFragmentSize, bytesRemaining);
55  }
56 
57  fragment = new MessageFragment(uuid, index, numFragments);
58  fragment->setPayload(stringFragment);
59  messageFragments.push_back(fragment);
60  bytesRemaining -= maxFragmentSize;
61  }
62 
63  return messageFragments;
64 }
65 
66 } // end of namespace provider
67 
68 } // end of namespace rms
69 
70 } // end of namespace cd
71 
72 } // end of namespace fnal
73 
74 } // end of namespace gov
static std::vector< MessageFragment * > splitMessage(std::string messageText, int maxFragmentSize, const char *uuid)
Definition: fnal.py:1
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
c cd(1)
fvar< T > ceil(const fvar< T > &x)
Definition: ceil.hpp:11
void setPayload(std::string payloadString)