COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
archive.cpp File Reference
#include "support/configCosmos.h"
#include "support/datalib.h"
#include "support/timelib.h"
#include "agent/agentclass.h"
#include "thirdparty/zlib/zlib.h"
#include <stdio.h>
Include dependency graph for archive.cpp:

Functions

int main (int argc, char *argv[])
 

Variables

Agentagent
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)
40 {
41  std::string node = "hiakasat";
42  std::string agentname = "soh";
43  std::string source = "incoming";
44 
45  switch (argc)
46  {
47  case 4:
48  source = argv[3];
49  case 3:
50  agentname = argv[2];
51  case 2:
52  node = argv[1];
53  break;
54  default:
55  printf("Usage: archive node [agent] [\"incoming\"|\"outgoing\"]\n");
56  exit (1);
57  }
58 
59  if (!(agent = new Agent(node)))
60  {
61  printf("Couldn't establish client for node %s\n", node.c_str());
62  exit (-1);
63  }
64 
65  char buffer[8192];
66  std::vector<filestruc> srcfiles;
67  data_list_files(agent->cinfo->node.name, source, agentname, srcfiles);
68 
69  for (filestruc srcfile: srcfiles)
70  {
71  if (srcfile.type != "directory")
72  {
73  FILE* fin;
74  if ((fin = data_open(srcfile.path, (char *)"r")) != NULL)
75  {
76  gzFile gzin = gzdopen(fileno(fin), "r");
77  uint32_t year, jday, isecond;
78  sscanf(srcfile.name.c_str(), "%*[A-Z,a-z,0-9]_%4u%3u%5u", &year, &jday, &isecond);
79  double utc = cal2mjd(year, 1, isecond/86400.) + jday;
80 
81  // Check for gzip
82  uint16_t namelen = srcfile.name.size();
83  if (namelen >= 3 && srcfile.name.substr(namelen-3, 3) == ".gz")
84  {
85  srcfile.name = srcfile.name.substr(0, namelen-3);
86  }
87 
88  std::string newpath = data_name_path(node, "data", agentname, utc, srcfile.name);
89  if (!newpath.empty() && !data_exists(newpath))
90  {
91  FILE* fout;
92  if ((fout = data_open(newpath, (char *)"w")) != NULL)
93  {
94  size_t tbytes = 0;
95  int nbytes;
96  do
97  {
98  nbytes = gzread(gzin, buffer, 8192);
99  if (nbytes > 0)
100  {
101  tbytes += nbytes;
102  fwrite(buffer, 1, nbytes, fout);
103  }
104  } while (nbytes > 0 && !gzeof(gzin));
105  fclose(fout);
106  gzclose_r(gzin);
107 
108  if (tbytes)
109  {
110  printf("Success: %s: %" PRIu64 "\n", newpath.c_str(), tbytes);
111  }
112  else
113  {
114  remove(newpath.c_str());
115  printf("Failure: %s\n", newpath.c_str());
116  }
117  }
118  }
119  }
120  }
121  }
122 }
int ZEXPORT gzeof(gzFile file)
Definition: gzlib.c:511
FILE * data_open(string path, const char *mode)
Open file from path.
Definition: datalib.cpp:1019
string data_name_path(string node, string location, string agent, double mjd, string name)
Create data file path.
Definition: datalib.cpp:954
vector< filestruc > data_list_files(string directory)
Get list of files in a directory, directly.
Definition: datalib.cpp:461
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
Definition: datalib.h:113
static char buffer[255]
Definition: propagator_simple.cpp:60
string agentname
Definition: agent_add_soh.cpp:53
char name[40+1]
Node Name.
Definition: jsondef.h:3556
Definition: agentclass.h:139
int ZEXPORT gzclose_r(gzFile file)
Definition: gzread.c:567
double cal2mjd(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t minute, int32_t second, int32_t nsecond)
Calendar representation to Modified Julian Day - full.
Definition: timelib.cpp:294
gzFile ZEXPORT gzdopen(int fd, const char *mode)
Definition: gzlib.c:284
static string source
Definition: ax25_recv.cpp:42
cosmosstruc * cinfo
Definition: agentclass.h:346
Agent * agent
Definition: archive.cpp:37
Definition: zlib.h:1670
static string node
Definition: agent_monitor.cpp:126
int ZEXPORT gzread(gzFile file, voidp buf, unsigned len)
Definition: gzread.c:288
bool data_exists(string &path)
Check existence of path.
Definition: datalib.cpp:1003

Variable Documentation

Agent* agent