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