API  Version 2.0.1
Low-Latency HFT API
 All Classes Functions Variables
baseCommands.h
1 #ifndef BASECOMMANDS_H
2 #define BASECOMMANDS_H
3 #include <vector>
4 //#include "defines.h"
5 //#include "response.h"
6 #include <iostream>
7 #include "cmdDefines.h"
8 #include <map>
9 #include <serializeCommands.h>
10 #include <stdio.h>
11 #include <typeinfo>
12 #include <sstream>
13 namespace API2{
14 
15  using namespace Serialization;
19  class BaseType
20  {
21  CREATE_FIELD(std::string, String);
22  public:
23 
24 
25  virtual ~BaseType(){}
26 
32  virtual void serializeFun(char *buf, int &bytes)=0;
33 
39  virtual void deSerializeFun(const char *buf, int &offset)=0;
40 
44  virtual void dump()=0;
45 
49  virtual std::string getKeyValueString()=0;
50 
51 
55  virtual void printType() = 0;
56 
61  BaseType(const std::string &stringType)
62  {
63  setString(stringType);
64  }
65 
70  {
71  }
72  };
73 
74 
78  template <class T>
79  class DerivedType :public BaseType
80  {
81  CREATE_FIELD( T, Value);
82  CREATE_FIELD( UNSIGNED_INTEGER, Count );
83 
84 
85  public:
86 
91  DerivedType(const std::string &type):
92  BaseType(type),
93  _Value(T()),
94  _Count(0)
95  {
96  }
97 
102 
106  void printType()
107  {
108  std::cout<<"\n typename "<<typeid(_Value).name()<<std::endl;
109  }
110 
116  void serializeFun(char *buf, int &bytes)
117  {
118  serialize(_Value, buf, bytes);
119  }
120 
126  void deSerializeFun(const char *buf, int &offset)
127  {
128  printf("calling deSerialize in main \n");
129  deSerialize(_Value, buf, offset);
130  }
131 #if 1
132 
139  void serialize(
140  const MapULong &mapULong,
141  char *buf,
142  int &bytes)
143  {
144  UNSIGNED_LONG tmpLong = 0;
145  for(MapULongIter i= mapULong.begin(); i!= mapULong.end(); i++)
146  {
147  Serialization::serialize(i->first,buf,bytes);
148  Serialization::serialize(i->second,buf,bytes);
149  //SERIALIZE_64(tmpLong, i->first, buf, bytes);
150  //SERIALIZE_64(tmpLong, (i->second), buf, bytes);
151  }
152  }
153 
160  void deSerialize(MapULong &val, const char *buf, int &offset)
161  {
162  UNSIGNED_LONG tmp1,tmp2= 0;
163  for(int i=0; i<getCount();i++)
164  {
165  UNSIGNED_LONG key;
166  UNSIGNED_LONG value;
167  Serialization::deSerialize(key,buf,offset);
168  Serialization::deSerialize(value,buf,offset);
169  //DESERIALIZE_64(tmp1, tmp2, t.setTmpKey(tmp2), buf, offset);
170  //DESERIALIZE_64(tmp1, tmp2, t.setTmpVal(tmp2), buf, offset);
171  val[key] = value;
172  }
173  }
180  template<class M>
181  void serialize(const M &value,
182  char *buf,int &bytes)
183  {
184  Serialization::serialize(value,buf,bytes);
185  }
186  template<class M>
187 
194  void deSerialize(M &val, const char *buf, int &offset)
195  {
196  Serialization::deSerialize(val,buf,offset);
197  }
198 #endif
199 
203  void dump()
204  {
205  std::cout<<getKeyValueStringImpl(getValue());
206  }
210  std::string getKeyValueString()
211  {
212  return getKeyValueStringImpl(getValue());
213  }
214 
219  template<class M>
220  std::string getKeyValueStringImpl(const M &val)
221  {
222  std::ostringstream out;
223  out << std::endl << getString() <<"--->"<< val;
224  return out.str();
225  }
226  std::string getKeyValueStringImpl(const API2::AccountDetail &val)
227  {
228  std::ostringstream out;
229  out << std::endl << getString() <<"--->"<< val.dump();
230  return out.str();
231  }
232 
237  std::string getKeyValueStringImpl(const char &val)
238  {
239  std::ostringstream out;
240  out << std::endl << getString() <<"--->"<< (short)val;
241  return out.str();
242  }
243 
248  std::string getKeyValueStringImpl(const UNSIGNED_CHARACTER &val)
249  {
250  std::ostringstream out;
251  out << std::endl << getString() <<"--->"<< (short)val;
252  return out.str();
253  }
254 
259  std::string getKeyValueStringImpl(const MapULong &val)
260  {
261  std::ostringstream out;
262  out <<"========" << std::endl << getString() << "========";
263  for(MapULongIter iter = _Value.begin(); iter != _Value.end(); iter ++)
264  {
265  out << std::endl << iter->first <<"--->"<< iter->second;
266  }
267  return out.str();
268  }
269 
270  };
271 
272 
273  typedef std::vector<BaseType *> BaseTypeVector;
274 
279  {
280 
284  CREATE_FIELD_DERIVED( UNSIGNED_LONG, StrategyVersion);
285 
289  CREATE_FIELD_DERIVED( UNSIGNED_CHARACTER, TransactionType);
290 
294  CREATE_FIELD_DERIVED( UNSIGNED_INTEGER, ClientId);
295 
299  CREATE_FIELD_DERIVED( UNSIGNED_INTEGER, StrategyId);
300 
304  CREATE_FIELD_DERIVED( UNSIGNED_LONG, AdminTokenId);
305 
309  CREATE_FIELD_DERIVED( UNSIGNED_LONG, SequenceNumber);
310 
314  BaseTypeVector _Members;
315 
316  public:
321  {
322  initialize();
323  }
324 
328  virtual void initialize()
329  {
330  _Members.clear();
331  SET_DERIVED_TYPE( StrategyVersion, 0);
332  SET_DERIVED_TYPE( TransactionType, 0);
333  SET_DERIVED_TYPE( ClientId, 0);
334  SET_DERIVED_TYPE( StrategyId, 0);
335  SET_DERIVED_TYPE( AdminTokenId, 0);
336  SET_DERIVED_TYPE( SequenceNumber, 0);
337  }
338 
347  int serialize(char *buf, bool isResponse, UNSIGNED_CHARACTER cat, UNSIGNED_CHARACTER comCat)
348  {
349  int bytes = 0;
350 
351  bytes = sizeof(UNSIGNED_SHORT); // Leave 2 bytes for packet size
352  if (isResponse)
353  {
354  Serialization::serialize(cat,buf,bytes);
355  }
356  else
357  {
358  Serialization::serialize(comCat,buf,bytes);
359  }
360 
361  for( int i=0; i<(int)_Members.size(); i++)
362  {
363  //printf("Serializing :%s \n",_Members[i]->getString().c_str());
364  _Members[i]->serializeFun(buf,bytes);
365  }
366 
367  int dummyBytes = 0;
371  Serialization::serialize((UNSIGNED_SHORT)(bytes-sizeof(UNSIGNED_SHORT)),buf,dummyBytes);
372 
373  return bytes;
374  }
375 
385  int serializeApi(char *buf,
386  bool isResponse,
387  UNSIGNED_CHARACTER cat,
388  UNSIGNED_CHARACTER comCat,
389  int apiIndex)
390  {
391  int bytes = 0;
392 
393  bytes = sizeof(UNSIGNED_SHORT); // Leave 2 bytes for packet size
394  if (isResponse)
395  {
396  Serialization::serialize(cat,buf,bytes);
397  }
398  else
399  {
400  Serialization::serialize(comCat,buf,bytes);
401  }
402 
403  Serialization::serialize(apiIndex,buf,bytes);
404  for( int i=0; i<(int)_Members.size(); i++)
405  {
406  //printf("Serializing :%s \n",_Members[i]->getString().c_str());
407  _Members[i]->serializeFun(buf,bytes);
408  }
409 
410  int dummyBytes = 0;
414  Serialization::serialize((UNSIGNED_SHORT)(bytes-sizeof(UNSIGNED_SHORT)),buf,dummyBytes);
415 
416  return bytes;
417  }
418 
423  void deSerialize(const char* buf)
424  {
425  initialize();
426  int offset =0;
427 
428  for( int i=0; i<(int)_Members.size(); i++)
429  {
430  //printf("Deserializing :%s \n",_Members[i]->getString().c_str());
431  _Members[i]->deSerializeFun(buf,offset);
432  }
433  }
434 
439  void addType(BaseType *type)
440  {
441  _Members.push_back(type);
442  }
443 
447  void dump()
448  {
449  std::cout << std::endl<< "SG_DUMP. Total Params:" <<_Members.size()<<std::endl;
450  std::cout << std::endl<< "===============================" <<std::endl;
451 
452  for( int i=0; i<(int)_Members.size(); i++)
453  {
454  _Members[i]->dump();
455  }
456 
457  }
458 
459  std::string getString()
460  {
461  std::ostringstream out;
462  out << std::endl<< "SG_DUMP. Total Params:" <<_Members.size()<<std::endl;
463  out << std::endl<< "===============================" <<std::endl;
464 
465  for( int i=0; i<(int)_Members.size(); i++)
466  {
467  out<<_Members[i]->getKeyValueString();
468  }
469  return out.str();
470  }
471 
472  void writeToFile(FILE *fp)
473  {
474  char strategyCommandBuffer[MAX_BUF_SIZE];
475  int strategyBytes = serialize((char *)strategyCommandBuffer,false,0,0);
476  fwrite((void*)strategyCommandBuffer,strategyBytes, 1, fp);
477  fflush(fp);
478  }
479  int getSerializeSize()
480  {
481  char strategyCommandBuffer[MAX_BUF_SIZE];
482  int strategyBytes = serialize(strategyCommandBuffer,false,0,0);
483  return strategyBytes;
484  }
485  void deSerializeMemorySize(const char *buff)
486  {
487  UNSIGNED_SHORT tmp1;
488  UNSIGNED_CHARACTER tmp2;
489  int offset = 0;
490  Serialization::deSerialize(tmp1, buff, offset);
491  Serialization::deSerialize(tmp2,buff,offset);
492  deSerialize(buff + offset);
493  }
494  };
495 
496 
497 
498 
499 
500 }
501 #endif // BASECOMMANDS_H
void addType(BaseType *type)
addType
Definition: baseCommands.h:439
std::string getKeyValueStringImpl(const MapULong &val)
getKeyValueStringImpl
Definition: baseCommands.h:259
void deSerialize(MapULong &val, const char *buf, int &offset)
deSerialize
Definition: baseCommands.h:160
int serialize(char *buf, bool isResponse, UNSIGNED_CHARACTER cat, UNSIGNED_CHARACTER comCat)
serialize
Definition: baseCommands.h:347
void printType()
printType
Definition: baseCommands.h:106
void deSerialize(const char *buf)
deSerialize
Definition: baseCommands.h:423
std::string getKeyValueString()
getKeyValueString
Definition: baseCommands.h:210
void deSerializeFun(const char *buf, int &offset)
deSerializeFun
Definition: baseCommands.h:126
int serializeApi(char *buf, bool isResponse, UNSIGNED_CHARACTER cat, UNSIGNED_CHARACTER comCat, int apiIndex)
serializeApi
Definition: baseCommands.h:385
Definition: sharedCommands.h:23
std::string getKeyValueStringImpl(const M &val)
getKeyValueStringImpl
Definition: baseCommands.h:220
AbstractUserParams()
AbstractUserParams.
Definition: baseCommands.h:320
std::string getKeyValueStringImpl(const char &val)
getKeyValueStringImpl
Definition: baseCommands.h:237
virtual void initialize()
initialize
Definition: baseCommands.h:328
DerivedType()
DerivedType.
Definition: baseCommands.h:101
The AbstractUserParams class.
Definition: baseCommands.h:278
void serialize(const M &value, char *buf, int &bytes)
serialize
Definition: baseCommands.h:181
BaseType()
BaseType.
Definition: baseCommands.h:69
BaseType(const std::string &stringType)
BaseType.
Definition: baseCommands.h:61
void dump()
dump
Definition: baseCommands.h:447
void dump()
dump
Definition: baseCommands.h:203
std::string getKeyValueStringImpl(const UNSIGNED_CHARACTER &val)
getKeyValueStringImpl
Definition: baseCommands.h:248
The BaseType class.
Definition: baseCommands.h:19
The DerivedType class.
Definition: baseCommands.h:79
DerivedType(const std::string &type)
DerivedType.
Definition: baseCommands.h:91
void serializeFun(char *buf, int &bytes)
serializeFun
Definition: baseCommands.h:116
void deSerialize(M &val, const char *buf, int &offset)
deSerialize
Definition: baseCommands.h:194
void serialize(const MapULong &mapULong, char *buf, int &bytes)
serialize
Definition: baseCommands.h:139