Example #1
0
bool ConnectionNoMoreUsed (Connection* connection, ConnectionOwner owner) {
    bool result = true;
    int i = 0;

    TakeLock(connection->closedByLock);

    connection->closedBy[owner] = true;

    Log(AIO4C_LOG_LEVEL_DEBUG, "connection %s closed by: [reader:%u,worker:%u,writer:%u,acceptor:%u,client:%u]", connection->string,
            connection->closedBy[AIO4C_CONNECTION_OWNER_READER], connection->closedBy[AIO4C_CONNECTION_OWNER_WORKER],
            connection->closedBy[AIO4C_CONNECTION_OWNER_WRITER], connection->closedBy[AIO4C_CONNECTION_OWNER_ACCEPTOR],
            connection->closedBy[AIO4C_CONNECTION_OWNER_CLIENT]);

    for (i = 0; i < AIO4C_CONNECTION_OWNER_MAX && result; i++) {
        result = (result && connection->closedBy[i]);
    }

    if (result) {
        memset(connection->closedBy, 0, AIO4C_CONNECTION_OWNER_MAX * sizeof(bool));
    }

    ReleaseLock(connection->closedByLock);

    return result;
}
Example #2
0
void ConnectionManagedBy(Connection* connection, ConnectionOwner owner) {
    bool managedByAll = true;
    int i = 0;

    TakeLock(connection->managedByLock);

    connection->managedBy[owner] = true;

    Log(AIO4C_LOG_LEVEL_DEBUG, "connection %s managed by: [reader:%u,worker:%u,writer:%u]", connection->string,
        connection->managedBy[AIO4C_CONNECTION_OWNER_READER], connection->managedBy[AIO4C_CONNECTION_OWNER_WORKER],
        connection->managedBy[AIO4C_CONNECTION_OWNER_WRITER]);

    for (i = 0; i < AIO4C_CONNECTION_OWNER_MAX; i++) {
        managedByAll = (managedByAll && connection->managedBy[i]);
    }

    ReleaseLock(connection->managedByLock);

    if (managedByAll) {
        Log(AIO4C_LOG_LEVEL_DEBUG, "connection %s is managed by all threads", connection->string);
        ConnectionState(connection, AIO4C_CONNECTION_STATE_CONNECTED);
    }

}
int OGRSelafinDataSource::OpenTable(const char * pszFilename) {
    //CPLDebug("Selafin","OpenTable(%s,%i)",pszFilename,bUpdate);
    // Open the file
    VSILFILE * fp;
    if( bUpdate ) {
        // We have to implement this locking feature for write access because the same file may hold several layers, and some programs (like QGIS) open each layer in a single datasource,
        // so the same file might be opened several times for write access
        if (TakeLock(pszFilename)==0) {
            CPLError(CE_Failure,CPLE_OpenFailed,"Failed to open %s for write access, lock file found %s.",pszFilename,pszLockName);
            return FALSE;
        }
        fp = VSIFOpenL( pszFilename, "rb+" );
    } else fp = VSIFOpenL( pszFilename, "rb" );
    if( fp == NULL ) {
        CPLError( CE_Warning, CPLE_OpenFailed, "Failed to open %s, %s.", pszFilename, VSIStrerror( errno ) );
        return FALSE;
    }
    if( !bUpdate && strstr(pszFilename, "/vsigzip/") == NULL && strstr(pszFilename, "/vsizip/") == NULL ) fp = (VSILFILE*) VSICreateBufferedReaderHandle((VSIVirtualHandle*)fp);

    // Quickly check if the file is in Selafin format, before actually starting to read to make it faster
    char szBuf[9];
    VSIFReadL(szBuf,1,4,fp);
    if (szBuf[0]!=0 || szBuf[1]!=0 || szBuf[2]!=0 || szBuf[3]!=0x50) {
        VSIFCloseL(fp);
        return FALSE;
    }
    VSIFSeekL(fp,84,SEEK_SET);
    VSIFReadL(szBuf,1,8,fp);
    if (szBuf[0]!=0 || szBuf[1]!=0 || szBuf[2]!=0 || szBuf[3]!=0x50 || szBuf[4]!=0 || szBuf[5]!=0 || szBuf[6]!=0 || szBuf[7]!=8) {
        VSIFCloseL(fp);
        return FALSE;
    }
    /* VSIFSeekL(fp,76,SEEK_SET);
    VSIFReadL(szBuf,1,8,fp);
    if (STRNCASECMP(szBuf,"Seraphin",8)!=0 && STRNCASECMP(szBuf,"Serafin",7)!=0) {
        VSIFCloseL(fp);
        return FALSE;
    } */

    // Get layer base name
    CPLString osBaseLayerName = CPLGetBasename(pszFilename);
    CPLString osExt = CPLGetExtension(pszFilename);
    if( STARTS_WITH(pszFilename, "/vsigzip/") && EQUAL(osExt, "gz") ) {
        size_t nPos=std::string::npos;
        if (strlen(pszFilename)>3) nPos=osExt.find_last_of('.',strlen(pszFilename)-4);
        if (nPos!=std::string::npos) {
            osExt=osExt.substr(nPos+1,strlen(pszFilename)-4-nPos);
            osBaseLayerName=osBaseLayerName.substr(0,nPos);
        } else {
            osExt="";
        }
    }

    // Read header of file to get common information for all layers
    poHeader=Selafin::read_header(fp,pszFilename);
    if (poHeader==NULL) {
        VSIFCloseL(fp);
        CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open %s, wrong format.\n", pszFilename);
        return FALSE;
    }
    if (poHeader->nEpsg!=0) {
        poSpatialRef=new OGRSpatialReference();
        if (poSpatialRef->importFromEPSG(poHeader->nEpsg)!=OGRERR_NONE) {
            CPLError( CE_Warning, CPLE_AppDefined, "EPSG %d not found. Could not set datasource SRS.\n", poHeader->nEpsg);
            delete poSpatialRef;
            poSpatialRef=NULL;
        }
    }

    // Create two layers for each selected time step: one for points, the other for elements
    int nNewLayers;
    poRange.setMaxValue(poHeader->nSteps);
    nNewLayers=static_cast<int>(poRange.getSize());
    if (EQUAL(pszFilename, "/vsistdin/")) osBaseLayerName = "layer";
    CPLString osLayerName;
    papoLayers = (OGRSelafinLayer **) CPLRealloc(papoLayers, sizeof(void*) * (nLayers+nNewLayers));
    for (size_t j=0;j<2;++j) {
        SelafinTypeDef eType=(j==0)?POINTS:ELEMENTS;
        for (int i=0;i<poHeader->nSteps;++i) {
            if (poRange.contains(eType,i)) {
                char szTemp[30];
                double dfTime;
                if (VSIFSeekL(fp,poHeader->getPosition(i)+4,SEEK_SET)!=0 || Selafin::read_float(fp,dfTime)==0) {
                    VSIFCloseL(fp);
                    CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open %s, wrong format.\n", pszFilename);
                    return FALSE;
                }
                if (poHeader->panStartDate==NULL) snprintf(szTemp,29,"%d",i); else {
                    struct tm sDate;
                    memset(&sDate, 0, sizeof(sDate));
                    sDate.tm_year=poHeader->panStartDate[0]-1900;
                    sDate.tm_mon=poHeader->panStartDate[1]-1;
                    sDate.tm_mday=poHeader->panStartDate[2];
                    sDate.tm_hour=poHeader->panStartDate[3];
                    sDate.tm_min=poHeader->panStartDate[4];
                    sDate.tm_sec=poHeader->panStartDate[5]+(int)dfTime;
                    mktime(&sDate);
                    strftime(szTemp,29,"%Y_%m_%d_%H_%M_%S",&sDate);
                }
                if (eType==POINTS) osLayerName=osBaseLayerName+"_p"+szTemp; else osLayerName=osBaseLayerName+"_e"+szTemp;
                papoLayers[nLayers++] = new OGRSelafinLayer( osLayerName, bUpdate, poSpatialRef, poHeader,i,eType);
                //poHeader->nRefCount++;
            }
        }
    }

    // Free allocated variables and exit
    return TRUE;
}
Example #4
0
void _ConnectionState(char* file, int line, Connection* connection, ConnectionState state) {
    if (connection->state == state) {
        Log(AIO4C_LOG_LEVEL_DEBUG, "%s:%d: connection %s already in state %s", file, line,
                connection->string, ConnectionStateString[state]);
        return;
    }

    TakeLock(connection->stateLock);

    Log(AIO4C_LOG_LEVEL_DEBUG, "%s:%d: connection %s [%s] -> [%s]", file, line, connection->string,
           ConnectionStateString[connection->state], ConnectionStateString[state]);

    connection->state = state;

    switch (state) {
        case AIO4C_CONNECTION_STATE_NONE:
            break;
        case AIO4C_CONNECTION_STATE_INITIALIZED:
            _ConnectionEventHandle(connection, AIO4C_INIT_EVENT);
            break;
        case AIO4C_CONNECTION_STATE_CONNECTING:
            connection->canRead = false;
            connection->canWrite = false;
            _ConnectionEventHandle(connection, AIO4C_CONNECTING_EVENT);
            break;
        case AIO4C_CONNECTION_STATE_CONNECTED: {
            char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
            char* newName = NULL;
            aio4c_addr_t* addr = NULL;
            socklen_t addrSize = 0;
            memset(hbuf, 0, NI_MAXHOST * sizeof(char));
            memset(sbuf, 0, NI_MAXSERV * sizeof(char));
            if (connection->string != NULL) {
                addrSize = AddressGetAddrSize(connection->address);
                addr = aio4c_malloc(addrSize);
                if (addr != NULL) {
                    memcpy(addr, AddressGetAddr(connection->address), addrSize);
                    if (getsockname(connection->socket, addr, &addrSize) == 0) {
                        if (getnameinfo(addr, addrSize, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
                            if (connection->stringAllocated) {
                                newName = aio4c_realloc(connection->string, (strlen(AddressGetString(connection->address)) + strlen(hbuf) + strlen(sbuf) + 8) * sizeof(char));
                            } else {
                                newName = aio4c_malloc((strlen(AddressGetString(connection->address)) + strlen(hbuf) + strlen(sbuf) + 8) * sizeof(char));
                            }
                            if (newName != NULL) {
                                snprintf(newName, strlen(AddressGetString(connection->address)) + strlen(hbuf) + strlen(sbuf) + 8, "[%s]:%s -> %s", hbuf, sbuf, AddressGetString(connection->address));
                                connection->string = newName;
                                connection->stringAllocated = true;
                            }
                        }
                    }
                }
                aio4c_free(addr);
            }
            connection->canRead = true;
            connection->canWrite = true;
            _ConnectionEventHandle(connection, AIO4C_CONNECTED_EVENT);
        }; break;
        case AIO4C_CONNECTION_STATE_PENDING_CLOSE:
            connection->canRead = false;
            connection->canWrite = true;
            _ConnectionEventHandle(connection, AIO4C_PENDING_CLOSE_EVENT);
            break;
        case AIO4C_CONNECTION_STATE_CLOSED:
            connection->canRead = false;
            connection->canWrite = false;
            _ConnectionEventHandle(connection, AIO4C_CLOSE_EVENT);
            break;
        case AIO4C_CONNECTION_STATE_MAX:
            break;
    }

    ReleaseLock(connection->stateLock);
}