00001 #ifndef __J2K__NameSpace_CPP__
00002 #define __J2K__NameSpace_CPP__
00003
00004 #include <j2k/nto/NameSpace.hpp>
00005
00006 NameSpace::NameSpace( const char* name0 = "myname", int nbClients = 1, int nbServers = 1 )
00007 : name( NULL ), nameLen( 0 ), attach_info( NULL ), rcvid( 0 )
00008 {
00009 initName( name0 );
00010 initSync( nbServers, nbClients );
00011 }
00012
00013 void NameSpace::initName( const char* name0 = NULL )
00014 {
00015 if ( name0 != NULL )
00016 {
00017 nameLen = strlen( name0 );
00018 name = new char[ nameLen + 1 ];
00019 memcpy( name, name0, nameLen );
00020 name[ nameLen ] = '\0';
00021 }
00022 }
00023
00024 NameSpace::~NameSpace()
00025 {
00026 if ( name != NULL )
00027 {
00028 delete[] name;
00029 }
00030 }
00031
00032 void NameSpace::Nattach( const char* name0 = NULL )
00033 {
00034 initName( name0 );
00035
00036
00037 attach_info = name_attach( NULL, name, 0 );
00038
00039 if ( attach_info == NULL )
00040 {
00041 printf( "Couldn't create an attach point named: [%s]. \n", name );
00042 switch( errno )
00043 {
00044 case EINVAL:
00045 printf( "Invalid arguments: \n" );
00046 printf( " - a NULL or empty path \n" );
00047 printf( " - a path starts with a leading slash / or contains .. characters \n" );
00048 break;
00049
00050 case ENOMEM:
00051 printf( " Not enough free memory to complete the operation. \n" );
00052 break;
00053
00054 case ENOTDIR:
00055 printf( "A component of the pathname wasn't a directory entry. \n" );
00056 break;
00057
00058 default:
00059 printf( "Unknown Error %s \n", strerror( errno ) );
00060 break;
00061 }
00062
00063 exit( 1 );
00064 }
00065
00066
00067 waitForServer();
00068 }
00069
00070 void NameSpace::Ndetach()
00071 {
00072 register int r = name_detach( attach_info, 0 );
00073
00074 if ( r == -1 )
00075 {
00076 printf( "Couldn't detach the possibly existing attach point. \n" );
00077
00078 switch ( errno )
00079 {
00080 case EINVAL:
00081 printf( "The mount ID (mntid) was never attached with name_attach(). \n" );
00082 break;
00083
00084 default:
00085 printf( "Unknown Error %s \n", strerror( errno ) );
00086 break;
00087 }
00088 exit( 1 );
00089 }
00090 }
00091
00092 int NameSpace::Nopen( const char* name0 = NULL )
00093 {
00094 initName( name0 );
00095
00096 register int fd = name_open( name, 0 );
00097
00098 if ( fd == -1 )
00099 {
00100 printf( "Couldn't open attach point: %s. \n", name );
00101
00102 switch( errno )
00103 {
00104 case EACCES:
00105 printf( "Search permission is denied on a component of the name." );
00106 break;
00107
00108 case EBADFSYS:
00109 printf( "While attempting to open the named file, \n" );
00110 printf( "either the file itself or a component of the path prefix was found to be corrupted. \n" );
00111 printf( "A system failure -- from which no automatic recovery is possible -- occurred \n" );
00112 printf( "while the file was being written to, or while the directory was being updated. \n" );
00113 printf( "You'll need to invoke appropriate systems-administration procedures \n" );
00114 printf( "to correct this situation before proceeding. \n" );
00115 break;
00116
00117 case EBUSY:
00118 printf( "The connection specified by name has already been opened \n" );
00119 printf( "and additional connections aren't permitted. \n" );
00120 break;
00121
00122 case EINTR:
00123 printf( "The name_open() operation was interrupted by a signal. \n" );
00124 break;
00125
00126 case EISDIR:
00127 printf( "The named path is a directory. \n" );
00128 break;
00129
00130 case ELOOP:
00131 printf( "Too many levels of symbolic links or prefixes. \n" );
00132 break;
00133
00134 case EMFILE:
00135 printf( "Too many file descriptors are currently in use by this process. \n" );
00136 break;
00137
00138 case ENAMETOOLONG:
00139 printf( "The length of the name string exceeds PATH_MAX, \n" );
00140 printf( "or a pathname component is longer than NAME_MAX. \n" );
00141 break;
00142
00143 case ENFILE:
00144 printf( "Too many files are currently open in the system. \n" );
00145 break;
00146
00147 case ENOENT:
00148 printf( "The connection specified by name doesn't exist. \n" );
00149 break;
00150
00151 case ENOTDIR:
00152 printf( "A component of the name prefix isn't a directory. \n" );
00153 break;
00154
00155 default:
00156 printf( "Unknown Error %s \n", strerror( errno ) );
00157 break;
00158 }
00159
00160 exit( 1 );
00161 }
00162
00163 return fd;
00164 }
00165
00166 void NameSpace::Nclose( int fd )
00167 {
00168 register int r = name_close( fd );
00169
00170 if ( r == -1 )
00171 {
00172 printf( "Couldn't close attach point file handle. \n" );
00173
00174 switch ( errno )
00175 {
00176 case EBADF:
00177 printf( "Invalid file descriptor filedes. \n" );
00178 break;
00179
00180 case EINTR:
00181 printf( "The name_close() call was interrupted by a signal. \n" );
00182 break;
00183
00184 case ENOSYS:
00185 printf( "The name_close() function isn't implemented for the filesystem specified by filedes. \n" );
00186 break;
00187
00188 default:
00189 printf( "Unknown Error %s \n", strerror( errno ) );
00190 break;
00191 }
00192 }
00193
00194
00195 waitForClient();
00196 }
00197
00198 #endif // End of NameSpace.cpp