PROGRAM 1
// Client.cpp #include #include #include #include #include #ifdef HAVE_STD_IOSTREAM using namespace std; #endif // Contact the naming service int contactNamingService (const char * argv, CORBA :: ORB_ptr orb, CORBA :: Object_var & obj, CosNaming :: NamingContext_var & nc) { try { obj = orb -> resolve_initial_references ("NameService"); } catch (const CORBA :: ORB :: InvalidName &) { cerr << argv [0] << ": can't resolve `NameService'" << endl; return 1; } if (CORBA :: is_nil (obj.in ())) { cerr << argv [0] << ": `NameService' is a nil object reference" << endl; return 1; } nc = CosNaming :: NamingContext :: _narrow (obj.in ()); if (CORBA :: is_nil (nc.in ())) { cerr << argv [0] << ": `NameService' is not a NamingContext object reference" << endl; return EXIT_FAILURE; } return 0; } // Resolve object name int resolveObjectName (const char * user, CORBA :: Object_var & obj, CosNaming :: NamingContext_var nc) { try { CosNaming :: Name aName; aName.length (1); aName [0].id = CORBA :: string_dup (user); aName [0].kind = CORBA :: string_dup (""); obj = nc -> resolve (aName); assert (! CORBA :: is_nil (obj)); } catch (CORBA :: SystemException & ex) { // OBPrintException (ex); cerr << "can't resolve\n"; return 1; } return 0; } // Get Haystack object int getHaystackObject (const char * argv, CORBA :: ORB_ptr orb, CORBA :: Object_var & obj) { obj = orb -> string_to_object ("relfile:/Haystack.ref"); if (CORBA :: is_nil (obj)) { cerr << argv [0] << ": cannot read IOR from Haystack.ref" << endl; return EXIT_FAILURE; } return 0; } // Print menu void printMenu () { cout << "Menu\n\n"; cout << "Enter 'd' to display a file on a specific haystack.\n"; cout << "Enter 'f' to find a file on all haystacks.\n"; cout << "Enter 'h' for list all haystacks.\n"; cout << "Enter 'l' for list all files on all haystacks.\n"; cout << "Enter 'x' to exit the program.\n\n"; cout << "> "; } // Pause screen void pause () { char anykey [] = ""; fflush (stdin); cout << endl << endl << "Press any key to continue. "; gets (anykey); } // Display file int displayFile (CORBA :: Object_var obj, CosNaming :: NamingContext_var nc) { char haystack [25]; char search [25]; cout << endl << "enter a haystack: "; cin >> haystack; cout << endl << "enter a filename: "; cin >> search; if (resolveObjectName (haystack, obj, nc) == 1) { return 1; } Haystack_var Haystack = Haystack :: _narrow (obj); assert (! CORBA :: is_nil (Haystack)); OctetStream * file = Haystack -> Get (search); cout << endl << haystack << ", " << search << endl << endl; if (file -> length () > 0) { int count = 0; for (int i = 0; i < file -> length (); i++) { if ((count % 20) == 0 && count != 0) { count = 0; pause (); cout << endl << endl; } if ((* file) [i] == '\n') { count++; } cout << (* file) [i]; } cout << endl; } else { cout << "file not found" << endl; } cout << endl; return 0; } // Find files int findFiles (CORBA :: Object_var obj, CosNaming :: NamingContext_var nc) { char search [25]; cout << endl << "enter a filename: "; cin >> search; CosNaming :: BindingList_var bl; CosNaming :: BindingIterator_var bi; nc -> list (999999, bl.out (), bi.out ()); int count = 0; for (int i = 0; i < bl -> length (); i++) { if (resolveObjectName (bl [i].binding_name [0].id, obj, nc) == 1) { return 1; } Haystack_var Haystack = Haystack :: _narrow (obj); assert (! CORBA :: is_nil (Haystack)); InfoList * theList = Haystack -> Find (search); cout << endl << bl [i].binding_name [0].id << endl; for (int j = 0; j < theList -> length (); j++) { if ((count % 20) == 0 && count != 0) { pause (); } count++; cout << endl << (* theList) [j].name << ", " << (* theList) [j].size; } if (theList -> length () == 0) { cout << endl << "file not found"; } cout << endl; } cout << endl; return 0; } // Display Haystacks void displayHaystacks (CosNaming :: NamingContext_var nc) { CosNaming :: BindingList_var bl; CosNaming :: BindingIterator_var bi; nc -> list (999999, bl.out (), bi.out ()); for (int i = 0; i < bl -> length (); i++) { if ((i % 20) == 0 && i != 0) { pause (); } cout << endl << bl [i].binding_name [0].id; } cout << endl << endl; } // List files int listFiles (CORBA :: Object_var obj, CosNaming :: NamingContext_var nc) { CosNaming :: BindingList_var bl; CosNaming :: BindingIterator_var bi; nc -> list (999999, bl.out (), bi.out ()); int count = 0; for (int i = 0; i < bl -> length (); i++) { if (resolveObjectName (bl [i].binding_name [0].id, obj, nc) == 1) { return 1; } Haystack_var Haystack = Haystack :: _narrow (obj); assert (!CORBA :: is_nil (Haystack)); InfoList * theList = Haystack -> List (); cout << endl << bl [i].binding_name [0].id << endl; for (int j = 0; j < theList -> length (); j++) { if ((count % 20) == 0 && count != 0) { pause (); } count++; cout << endl << (* theList) [j].name << ", " << (* theList) [j].size; } cout << endl; } cout << endl; return 0; } int run (CORBA :: ORB_ptr orb, int argc, char * argv []) { CORBA :: Object_var obj; CosNaming :: NamingContext_var nc; if (argc != 2) { cerr << "usage: " << argv [0] << "username\n"; exit (1); } char * user = argv [1]; // Contact Naming Service int i = contactNamingService (argv [0], orb, obj, nc); if (i == 1) { return 1; } else if (i == EXIT_FAILURE) { return EXIT_FAILURE; } #if 1 // Resolve object name if (resolveObjectName (user, obj, nc) == 1) { return 1; } #else // Get Haystack object if (getHaystackObject (argv, orb, obj) == EXIT_FAILURE) { return EXIT_FAILURE } #endif // Main loop #if 0 // Do nothing #else // Display file, get files, list Haystack, list files, or exit char c; do { printMenu (); cin >> c; if (c == 'd') { if (displayFile (obj, nc) == 1) { return 1; } } else if (c == 'f') { if (findFiles (obj, nc) == 1) { return 1; } } else if (c == 'h') { displayHaystacks (nc); } else if (c == 'l') { if (listFiles (obj, nc) == 1) { return 1; } } } while (cin.good () && c != 'x'); #endif return EXIT_SUCCESS; } int main (int argc, char * argv [], char * []) { int status = EXIT_SUCCESS; CORBA :: ORB_var orb; try { orb = CORBA :: ORB_init (argc, argv); status = run (orb, argc, argv); } catch (const CORBA :: Exception & ex) { cerr << ex << endl; status = EXIT_FAILURE; } if (! CORBA :: is_nil (orb)) { try { orb -> destroy (); } catch (const CORBA :: Exception & ex) { cerr << ex << endl; status = EXIT_FAILURE; } } return status; } // Server.cpp #include #include #include #include #include #ifdef HAVE_FSTREAM # include #else # include #endif #ifdef HAVE_STD_IOSTREAM using namespace std; #endif // Bind names with the Naming Service void registerObject (CORBA :: Object_ptr object, const char * name, CosNaming :: NamingContext_var nc) { CosNaming :: Name aName; aName.length (1); aName [0].id = CORBA :: string_dup (name); aName [0].kind = CORBA :: string_dup (""); nc -> rebind (aName, object); } // Contact Naming Service int contactNamingService (const char * argv, CORBA :: ORB_ptr orb, CORBA :: Object_var & obj, CosNaming :: NamingContext_var & nc) { try { obj = orb -> resolve_initial_references ("NameService"); } catch (const CORBA :: ORB :: InvalidName &) { cerr << argv [0] << ": can't resolve `NameService'" << endl; return 1; } if (CORBA :: is_nil (obj.in ())) { cerr << argv [0] << ": `NameService' is a nil object reference" << endl; return 1; } nc = CosNaming :: NamingContext :: _narrow (obj.in ()); if (CORBA :: is_nil (nc.in ())) { cerr << argv [0] << ": `NameService' is not a NamingContext object reference" << endl; return EXIT_FAILURE; } return 0; } int run (CORBA :: ORB_ptr orb, int argc, char * argv []) { CORBA :: Object_var obj; CosNaming :: NamingContext_var nc; // Get command line parameters (did ORB_init clean up?) if (argc != 3) { cerr << "usage: " << argv [0] << "directory user\n"; exit (1); } char * directory = argv [1]; char * user = argv [2]; // Resolve Root POA CORBA :: Object_var poaObj = orb -> resolve_initial_references ("RootPOA"); PortableServer :: POA_var rootPOA = PortableServer :: POA :: _narrow (poaObj); // Get a reference to the POA manager PortableServer :: POAManager_var manager = rootPOA -> the_POAManager (); // Create implementation object Haystack_impl * HaystackImpl = new Haystack_impl (directory); PortableServer :: ServantBase_var servant = HaystackImpl; Haystack_var Haystack = HaystackImpl -> _this (); // Contact Naming Service int i = contactNamingService (argv [0], orb, obj, nc); if (i == 1) { return 1; } else if (i == EXIT_FAILURE) { return EXIT_FAILURE; } #if 1 // Register the object registerObject (Haystack, user, nc); #else // Save reference CORBA :: String_var s = orb -> object_to_string (Haystack); const char * refFile = "Haystack.ref"; ofstream out (refFile); if (out.fail ()) { cerr << argv [0] << ": can't open `" << refFile << "': " << strerror (errno) << endl; return EXIT_FAILURE; } out << s << endl; out.close (); #endif // Run implementation manager -> activate (); orb -> run (); return EXIT_SUCCESS; } int main (int argc, char * argv [], char * []) { int status = EXIT_SUCCESS; CORBA :: ORB_var orb; try { orb = CORBA :: ORB_init (argc, argv); status = run (orb, argc, argv); } catch (const CORBA :: Exception & ex) { cerr << ex << endl; status = EXIT_FAILURE; } if (! CORBA :: is_nil (orb)) { try { orb -> destroy (); } catch (const CORBA :: Exception & ex) { cerr << ex << endl; status = EXIT_FAILURE; } } return status; } // Haystack.idl struct Info { string name; unsigned long size; }; typedef sequence InfoList; typedef sequence OctetStream; interface Haystack { exception NotFound { }; InfoList List (); InfoList Find (in string spec); OctetStream Get (in string filename) raises (NotFound); }; // Haystack_impl.h #ifndef HAYSTACK_IMPL_H #define HAYSTACK_IMPL_H #include class MyDir; class Haystack_impl : public POA_Haystack, public PortableServer :: RefCountServantBase { public: Haystack_impl (char * directory); virtual InfoList * List () throw (CORBA :: SystemException); virtual InfoList * Find (const char * spec) throw (CORBA :: SystemException); virtual OctetStream * Get (const char * filename) throw (Haystack :: NotFound, CORBA :: SystemException); private: char mDirectory [256]; MyDir * mDir; }; #endif // Haystack_impl.cpp #include #include "Haystack_impl.h" #include "MyDir.h" // Constructor Haystack_impl :: Haystack_impl (char * directory) : mDir (NULL) { strcpy (mDirectory, directory); } // List directory files InfoList * Haystack_impl :: List () throw (CORBA :: SystemException) { if (mDir) { delete mDir; } mDir = new MyDir (mDirectory); InfoList * theList = new InfoList; MyStat * stat; while (stat = mDir -> ReadDir ()) { Info info; info.name = CORBA :: string_dup (stat -> mFilename); info.size = stat -> mStat.st_size; int pos = theList -> length (); theList -> length (pos + 1); (* theList) [pos] = info; } return theList; } // Find specific files InfoList * Haystack_impl :: Find (const char * spec) throw (CORBA :: SystemException) { if (mDir) { delete mDir; } mDir = new MyDir (mDirectory); InfoList * theList = new InfoList; MyStat * stat; while (stat = mDir -> ReadDir ()) { Info info; info.name = CORBA :: string_dup (stat -> mFilename); info.size = stat -> mStat.st_size; if (strstr (info.name, spec) != NULL) { int pos = theList -> length (); theList -> length (pos + 1); (* theList) [pos] = info; } } return theList; } // Get file OctetStream * Haystack_impl :: Get (const char * filename) throw (Haystack :: NotFound, CORBA :: SystemException) { if (mDir) { delete mDir; } mDir = new MyDir (mDirectory); MyStat * stat; OctetStream * contents = new OctetStream; while (stat = mDir -> ReadDir ()) { char * name = CORBA :: string_dup (stat -> mFilename); int size = stat -> mStat.st_size; if (strcmp (name, filename) == 0) { FILE * infile = fopen (filename, "r"); int pos = 0; while (! feof (infile)) { pos = contents -> length (); contents -> length (pos + 1); (* contents) [pos] = fgetc (infile); } pos = contents -> length (); contents -> length (pos + 1); (* contents) [pos] = '\0'; fclose (infile); break; } } return contents; } // MyDir.h #include "MyStat.h" #include "counted_ptr.h" #include class MyDir { public: MyDir (char * pathname); virtual ~MyDir (); virtual MyStat * ReadDir (); virtual void Rewind (); protected: vector > mStats; int mLastStat; }; // MyDir.cpp #include "MyDir.h" #include #include #include "counted_ptr.h" #include // Compare files class Compare_MyStat_filenames { public: int operator () (const counted_ptr & s1, const counted_ptr & s2) { return (strcmp (s1 -> mFilename, s2 -> mFilename)); } }; // Constructor MyDir :: MyDir (char * pathnameOfDirectory) : mLastStat (0) { char cwd [256]; struct stat dirstat; struct stat filestat; DIR * dir; struct dirent * direntry; int i; // Check for validity of directories if (stat (pathnameOfDirectory, & dirstat)) { fprintf (stderr, "can't stat () directory %s\n", pathnameOfDirectory); return; } if (! S_ISDIR (dirstat.st_mode)) { fprintf (stderr, "%s is not a directory\n", pathnameOfDirectory); return; } // Open the directory dir = opendir (pathnameOfDirectory); if (dir == NULL) { fprintf (stderr, "opendir () failed!\n"); return; } // Chdir into the dir to make stat-ing easier (save old dir, too) if (getcwd (cwd, 256) == NULL) { fprintf (stderr, "can't get current working dir!\n"); return; } if (chdir (pathnameOfDirectory) != 0) { fprintf (stderr, "can't chdir to directory %s\n", pathnameOfDirectory); return; } // Fill array with stat () result for all the files in the directory while ((direntry = readdir (dir)) != NULL) { // Ignore "." and ".." if ((strcmp (direntry -> d_name, ".") != 0) && (strcmp (direntry -> d_name, "..") != 0) && (strcmp (direntry -> d_name, "current.jpg") != 0) && (strstr (direntry -> d_name, "TEMP") == NULL)) { // Read the status info and save MyStats in vector if (stat (direntry -> d_name, & filestat) != 0) { fprintf (stderr, "stat on %s failed!\n", direntry -> d_name); } MyStat * newStat = new MyStat (direntry -> d_name, filestat); if (! newStat) { fprintf (stderr, "MyDir::MyDir: can't make a MyStat\n"); return; } mStats.push_back (counted_ptr (newStat)); } } } // Destructor MyDir :: ~MyDir () { } // Read directory MyStat * MyDir :: ReadDir () { if (mLastStat >= mStats.size ()) { return NULL; } else { return mStats [mLastStat++].get (); } } // Rewind void MyDir :: Rewind () { mLastStat = 0; } // MyStat.h #ifndef MYSTAT_H #define MYSTAT_H #include #include #include #include "counted_ptr.h" static const int MAX_LENGTH = 80; struct MyStat { MyStat (char * name, struct stat thestat) : mStat (thestat) { if (strlen (name) <= MAX_LENGTH - 1) { strcpy (mFilename, name); } }; static int compare_filenames (const void * first, const void * second); char mFilename [MAX_LENGTH]; struct stat mStat; }; // MyStat.cpp #include "MyStat.h" // Compare files int MyStat :: compare_filenames (const void * first, const void * second) { MyStat * a = (MyStat *) first; MyStat * b = (MyStat *) second; char * A = a -> mFilename; char * B = b -> mFilename; return (strcmp (A, B)); } // counted_ptr.h // // counted_ptr - simple reference counted pointer. // // The is a non-intrusive implementation that allocates an additional // int and pointer for every counted object. // from: http://ootips.org/yonat/4dev/smart-pointers.html #ifndef COUNTED_PTR_H #define COUNTED_PTR_H // For ANSI-challenged compilers, you may want to #define // NO_MEMBER_TEMPLATES or explicit #define NO_MEMBER_TEMPLATES template class counted_ptr { public: typedef X element_type; explicit counted_ptr (X * p = 0) // Allocate a new counter : itsCounter (0) { if (p) { itsCounter = new counter (p); } } ~counted_ptr () { release (); } counted_ptr (const counted_ptr & r) throw () { acquire (r.itsCounter); } counted_ptr & operator = (const counted_ptr & r) { if (this != & r) { release (); acquire (r.itsCounter); } return * this; } #ifndef NO_MEMBER_TEMPLATES template friend class counted_ptr ; template counted_ptr (const counted_ptr & r) throw () { acquire (r.itsCounter); } template counted_ptr & operator = (const counted_ptr & r) { if (this != & r) { release (); acquire (r.itsCounter); } return * this; } #endif // NO_MEMBER_TEMPLATES X & operator * () const throw () { return * itsCounter -> ptr; } X * operator -> () const throw () { return itsCounter -> ptr; } X * get () const throw () { return itsCounter ? itsCounter -> ptr : 0; } bool unique () const throw () { return (itsCounter ? itsCounter -> count == 1 : true); } private: struct counter { counter (X * p = 0, unsigned c = 1) : ptr (p), count (c) { } X * ptr; unsigned count; } * itsCounter; void acquire (counter * c) throw () { // Increment the count itsCounter = c; if (c) { ++c -> count; } } void release () { // Decrement the count, delete if it is 0 if (itsCounter) { if (--itsCounter -> count == 0) { delete itsCounter -> ptr; delete itsCounter; } itsCounter = 0; } } }; #endif // COUNTED_PTR_H // P2p.out Menu Enter 'd' to display a file on a specific haystack. Enter 'f' to find a file on all haystacks. Enter 'h' for list all haystacks. Enter 'l' for list all files on all haystacks. Enter 'x' to exit the program. > d enter a haystack: user1 enter a filename: Haystack.idl user1, Haystack.idl // Haystack.idl struct Info { string name; unsigned long size; }; typedef sequence InfoList; typedef sequence OctetStream; interface Haystack { exception NotFound { }; InfoList List (); InfoList Find (in string spec); OctetStream Get (in string filename) raises (NotFound); Press any key to continue. }; ÿ Menu Enter 'd' to display a file on a specific haystack. Enter 'f' to find a file on all haystacks. Enter 'h' for list all haystacks. Enter 'l' for list all files on all haystacks. Enter 'x' to exit the program. > f enter a filename: Hay user1 Haystack.h, 9117 Haystack.cpp, 17281 Haystack_skel.h, 2860 Haystack.idl, 364 Haystack_impl.h, 606 Haystack_impl.cpp, 2386 Haystack_skel.cpp, 4786 user2 file not found user3 file not found user4 file not found stat on archive.Nov00 failed! user5 file not found Menu Enter 'd' to display a file on a specific haystack. Enter 'f' to find a file on all haystacks. Enter 'h' for list all haystacks. Enter 'l' for list all files on all haystacks. Enter 'x' to exit the program. > h user1 user2 user3 user4 user5 Menu Enter 'd' to display a file on a specific haystack. Enter 'f' to find a file on all haystacks. Enter 'h' for list all haystacks. Enter 'l' for list all files on all haystacks. Enter 'x' to exit the program. > l user1 Haystack.h, 9117 Haystack.cpp, 17281 script.c, 534 Haystack_skel.h, 2860 MyDir.h, 399 Haystack.idl, 364 Haystack_impl.h, 606 Makefile, 2092 Haystack_impl.cpp, 2386 MyDir.cpp, 2692 MyStat.h, 519 MyStat.cpp, 309 counted_ptr.h, 2796 Haystack_skel.cpp, 4786 server, 2737072 client, 1396248 test, 512 Client.cpp, 8055 Server.cpp, 3657 user2 lost+found, 8192 Press any key to continue. var, 512 local, 512 usr, 1024 etc, 3072 bin, 7680 dev, 3584 devices, 512 kernel, 512 lib, 10240 mnt, 512 opt, 512 proc, 62656 sbin, 512 tmp, 1389 platform, 512 net, 1 export, 512 home, 1 xfn, 1 .cpr_config, 1032 Press any key to continue. vol, 512 .dt, 512 .dtprofile, 5111 TT_DB, 512 students, 512 .Xauthority, 100 homer, 512 staff1, 512 staff2, 2048 .TTauthority, 76 apps, 1024 .cshrc, 139 .hotjava, 512 cdrom, 512 .netscape, 512 nsmail, 512 .rhosts, 34 floppy, 512 user3 user4 lost+found, 512 admin, 512 Press any key to continue. ho, 1536 morgan, 4096 nico, 3584 simon, 3072 tebo, 8192 winz, 2048 diskman, 512 rluibran, 1024 mcs, 512 troby, 1536 metis, 1536 yu, 3072 dwarnke, 1536 dallen, 1536 dtopham, 1536 flather, 512 gowri, 2560 malek, 3072 billard, 1536 rfj, 512 Press any key to continue. reiter, 4608 btrumbo, 512 callahan, 1024 cchompoo, 512 mroberts, 512 lippman, 52224 leann, 3072 cs428-46, 3584 daley, 2048 cclee, 2560 chuckf, 512 ckitting, 512 clee, 1024 lang, 512 dan, 1536 egenser, 512 emackinn, 2048 esuess, 1024 evelasqu, 1536 farzan, 1536 Press any key to continue. fleavitt, 512 fujimura, 512 graphics, 1024 hann, 1024 jcarter, 512 jglass, 1536 kbrown, 1536 keller, 512 kmehl, 512 export, 512 lfreerks, 512 assim, 512 manderso, 512 marut, 3584 mega, 512 merris, 512 mnugent, 1536 ssimon, 512 ouyang, 9216 pchamber, 512 Press any key to continue. pprasert, 512 quotas, 672032 roy, 512 ruhler, 2560 sbenson, 512 scoot, 1024 services, 512 shirschf, 512 simutis, 4608 sm, 512 sps, 6656 sshewbri, 512 steve, 2048 ranzelc, 1024 tang, 512 eguzman, 1024 victor, 512 vssunder, 512 wfernand, 512 wolitzer, 3072 Press any key to continue. wwwbio, 512 wwwchem, 512 wwwphys, 512 wwwstat, 512 mcs_b4_reorg, 3130 sseeley, 512 TT_DB, 512 jloos, 512 jbansiya, 512 johnson, 512 jcourter, 1536 grewe, 1024 stat on archive.Nov00 failed! user5 lost+found, 8704 cln_accts, 593 vlsiguy, 512 lab, 1024 diskman, 1024 guests, 1536 sp97, 512 TT_DB, 512 Press any key to continue. medtech, 512 delete.log.11-19, 21276 quotas, 672032 mkln, 103 fa00, 1536 su00, 512 archive.Nov00, 512 clr_cache, 209 clr_dotfm, 250 Menu Enter 'd' to display a file on a specific haystack. Enter 'f' to find a file on all haystacks. Enter 'h' for list all haystacks. Enter 'l' for list all files on all haystacks. Enter 'x' to exit the program. > x
BACK TO CS6580 PAGE.