All pastes #559619 Raw Edit

Unnamed

public text v1 · immutable
·published 2007-06-11 20:45 UTC
rendered paste body
/*
 *
 * sce.cpp Source
 *
 *
 * Note: that the dirent.h and any directory associated functions
 * are all originally written in C since C++ doesn't have 
 * directory manipulating functions.
 */

#include <SmApiNonBlocking.h>
#include "SmApiBlocking.h"
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <dirent.h>
#include <algorithm>
#include <time.h>
using namespace std;

//function prototype for wait (at the bottom)
void wait(int);

int main(int argc, int argv[]){

        SmApiBlocking bapi;
        ReturnCode* ret;
        int i=0,j=0, numfiles=0;
        char adel[2],name[50], ip[50], package[50];
        string files[50], temp[50], tempfile, line;
        DIR *pDIR;
        struct dirent *pDirEnt;
        
        //open the directory "." means same directory as code, will probably need to change
        if( (pDIR = opendir("/data/logs")) == NULL ){
                cout << "Directory not found.\n";
                exit(1);
        }
        
        pDirEnt = readdir( pDIR );


        while(1){ 
        
                //init w/ defaultparameters
                bapi.connect((char*)"10.0.0.132");
                bapi.init();
        
                //check if any files exist if not wait 2 minutes then should restart other wise keep going
                if( pDirEnt == NULL ){
                        wait(120);
                } else {
                        while ( pDirEnt != NULL ) {
                
                                tempfile = pDirEnt->d_name;                     
                                //skip over "." and ".." since readdir picks those up
                                if( tempfile[0] == '.' ){
                                        pDirEnt = readdir( pDIR );                      
                                }  
                                temp[i] = pDirEnt->d_name;
                                i++;
                                pDirEnt = readdir( pDIR );                      
                        }
                        closedir( pDIR ); //close the directory

                        /* 
                        Clean up the last ".." that readdir picks up
                        */
                        for(j=0;j<i;j++){
                                string str = temp[j];
                                if( str.compare( "..") != 0 ){
                                        files[numfiles] = temp[j];
                                        numfiles++;
                                }
                        } 
        
                        //sort the array, using built in C++ library
                        sort(files, files);
        
                        for(int j=0;j<numfiles;j++){
                                string str = "/data/logs/";
                                string path = str + files[j];
                                cout << "path: " << path << "\n";
                
                                //cout << "looking at file: " << files[j] << "\n";
                                ifstream myfile(path.c_str()); //file to read from
                
                                if(myfile.is_open()){
                                        while( !myfile.eof() ){
                                                getline(myfile, line); //grab the line here
                                                
                                                sscanf(line.c_str(), "%s %s %s %s", adel, name, ip, package);
                
                                        //      cout << "adel: " << adel << " name: " << name << " ip: " << ip
                                        //              << " package: " << package << "\n";
                
                                                if(strcmp(adel, "a") || strcmp(adel, "A")){
                                                        cout << "Adding subscriber \n";
                                                        bapi.addSubscriber(name, NULL, NULL,
                                                                0, NULL, NULL, 0, NULL, NULL, 0, "subscribers");
                                                        MappingType map_type = IP_RANGE;
                                                        char* prop_name = "packageID";
                                                        char* ip_address = ip;
                                                        char* rname = name;
                                                        char* rpackage = package;
//                                                      bapi.login(rname, &ip_address, &map_type, 1,
//                                                              &prop_name, &rpackage, 1, "subscribers", true, -1);
                                                }
                                                else if(strcmp(adel, "d") || strcmp(adel, "D")){
                                                        cout << "deleting subscriber\n";
                                                        char* rname = name;
                                                        bapi.removeSubscriber(rname);
                                                } else {
                                                        //unrecognized variable, should never execute
                                                        cout;
                                                } 
                                        } //while
                                        myfile.close();
                                }//if
                        } //for loop
                
                        //disconnect clean up variables
                        bapi.disconnect();
                        //purge files, still testing stuff uncomment later
                        cout << "Removing files: \n";
                        for(int j=0;j<numfiles;j++){
                                string file = "/data/logs/" + files[j];
                                remove(file.c_str());
                        }
                        i=0; 
                        wait(120);
                }//else statement
        } //while, uncomment eventually
        return 0;
}

void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}