/** * createNode method<p> * create node inside transaction<p> * Like the non transactional schemaAdapter in create new entity,<p> * but unlike the non transactional schemaAdapter, the change doesn't the effect in the server until they will be commited */ int transactionCreateNode(Transaction transaction,StringHashMap attributes,Node *node){ TransactionInternal*internal=(TransactionInternal*)transaction; int result; if(internal->transaction!=NULL){ result=transactionCreateNode(internal->transaction,attributes,node); }else{ int schemaId=internal->sessionMetaData->schemaId; int coreManagerId=internal->sessionMetaData->coreManagerId; int sessionId=internal->sessionMetaData->id; int lockTimeout=schemaAdapterGetLockTimeout(internal->scheamAdapter); Command command = commandBuilderBuildCreateNode(TRUE, schemaId, coreManagerId, sessionId, lockTimeout, attributes); int entityId=idProviderGetTempId(internal->idProvider); int true=TRUE; commandPush(command,ENTITY_ID, INTEGER,&entityId); commandPush(command,TRANSACTION, INTEGER, &true); commandPush(command,COPY_OF_ENTITY_ID, INTEGER, &entityId); commandPush(command,LOCK, INTEGER, true); result=commandExecuterConsume(internal->commandExecutor,command); registryGetNode(internal->registry,entityId,node); hashmapPut(internal->transactionEntities,entityId,node); arrayListAddAll(internal->commandList,command); } return result; }
void tcpConnectionListenerRunThread(TcpConnection tcpConnection){ TcpConnectionInternal* internal=(TcpConnectionInternal*)tcpConnection; //Declaring proc ess variables. int size ; void* buffer; void *commandBuffer; int commandSize; int currentSize; int tempSize=0; int result; while(internal->active==TRUE){ size=0; while(size<SIZE_OF_INT){ tempSize=read(internal->sockfd,&commandSize+size, SIZE_OF_INT-size); if(tempSize<=0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } size +=tempSize; } commandSize=htonl(commandSize); buffer=memoryAlloc(commandSize-SIZE_OF_INT); currentSize=0; while(currentSize<commandSize-SIZE_OF_INT){ currentSize+=read(internal->sockfd,buffer+currentSize, MIN(MAX_TCP_MESSAGE_SIZE,commandSize-currentSize-SIZE_OF_INT)); if(currentSize<=0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); memoryFree(buffer); return; } } Command command=commandDeSerialize(buffer,commandSize-SIZE_OF_INT); commandPush(command,IP,STRING,internal->remoteIp,internal->remoteIpLenght); commandPush(command,PORT,INTEGER,&internal->remotePort); memoryFree(buffer); // handle message distributionManagerConsumeTcp(internal->distributionManager,command); // Create the result message and send it to client commandSize=commandSerialize(command,&commandBuffer); result=writeToSocket(internal->sockfd,commandBuffer,commandSize); memoryFree(commandBuffer); if(result<0){ internal->active=FALSE; distributionManagerHandleTcpConnectionListenerFailure(internal->distributionManager,internal,internal->coreManagerId); return; } // Delete all none model or none atomic commands if(commandIsDeleteOnExit(command)==TRUE){ commandFree(command); } } internal->readyForCleanup+=LISTENER_READY_FOR_CLEANUP; }
int transactionRemoveAttributeFromNode(Transaction transaction,Node node,StringHashMap attributes){ TransactionInternal*internal=(TransactionInternal*)transaction; int result; if(internal->transaction!=NULL){ result=transactionRemoveAttributeFromNode(internal->transaction,node, attributes); }else{ validateSchemaEntity(internal,node); int schemaId=internal->sessionMetaData->schemaId; int coreManagerId=internal->sessionMetaData->coreManagerId; int sessionId=internal->sessionMetaData->id; int lockTimeout= schemaAdapterGetLockTimeout(internal->scheamAdapter); int true=TRUE; Command command = commandBuilderCreateRemoveAttributes(node->id, schemaId, coreManagerId, sessionId,lockTimeout,attributes); commandPush(command,OLD_ATTRIBUTES,ATTRIBUTES_MAP, node->attributes); commandPush(command,TRANSACTION, INTEGER, &true); result=commandExecuterConsume(internal->commandExecutor,command); arrayListAddLast(internal->commandList,command); } return result; }
/* * handleMessage * 1. Create new TcpConnection or UdpConnection (depends on the request) * 2. Register the new connection in the DistributionManager */ void handleMessage(int client_sockfd,ConnectionListenerInternal* connectionListenerInternal,Command command,char* ip) { DistributionManager distributionManager=connectionListenerInternal->distributionManager; distributionManagerConsumeTcp(distributionManager,command); int status=commandGetInt(command,COMMAND_STATUS); if(status==VALID) { int type=commandGetInt(command,COMMAND_TYPE); if(IS_TCP_CONNECTION(type)) { // Create Tcp connection int coreManagerId=commandGetInt(command,CORE_MANAGER_ID); TcpConnection connection=tcpConnectionCreate(distributionManager,client_sockfd,coreManagerId,ip,commandGetInt(command,PORT)); distributionManagerRegisterTcp(distributionManager,connection,coreManagerId); tcpConnectionStartListener(connection); } if(IS_BROADCAST_TCP_CONNECTION(type)) { // Create Tcp connection int coreManagerId=commandGetInt(command,CORE_MANAGER_ID); TcpConnection connection=tcpConnectionCreate(distributionManager,client_sockfd,coreManagerId,ip,commandGetInt(command,PORT)); distributionManagerRegisterBroadcastTcp(distributionManager,connection,coreManagerId); tcpConnectionStartWriter(connection); } if(IS_UDP_CONNECTION(type)) { // Create Udp connection char* interface=propertiesGet(connectionListenerInternal->properties,"interface"); int coreManagerId=commandGetInt(command,CORE_MANAGER_ID); int isRegistred=distributionManagerIsRegistred(distributionManager,coreManagerId); if(isRegistred==FALSE) { UdpConnection connection=udpConnectionCreate(distributionManager,coreManagerId,ip,commandGetInt(command,PORT),interface); distributionManagerRegisterUdp(distributionManager,connection,coreManagerId); udpConnectionSatrt(connection); } } if(IS_MULTICAST_CONNECTION(type)) { int coreManagerId=commandGetInt(command,CORE_MANAGER_ID); distributionManagerRegisterMulticast(distributionManager,NULL,coreManagerId); } int coreManagerId=distributionManagerGetCoreManager(distributionManager); int status=VALID; commandPush(command,CORE_MANAGER_ID,INTEGER,&coreManagerId); commandPush(command,COMMAND_STATUS,INTEGER,&status); } }
void commandContainerPushCommand(CommandContainer commandContainer,Command command){ CommandContainerInternal* internal=(CommandContainerInternal*)commandContainer; lockLock(internal->lock); time_t currentTime; // Stores seconds elapsed since 01-01-1970 time(¤tTime); int type=commandGetInt(command,COMMAND_TYPE); if(type==ATOMIC_MODEL_COMMAND){ commandPush(command,SERVER_HANDLING_TIME,INTEGER,¤tTime); linkedListAddLast(internal->atomicCommands,command); }else{ int version=commandGetInt(command,VERSION); int* intBuffer=memoryAlloc(SIZE_OF_INT); memcpy(intBuffer,&version,SIZE_OF_INT); commandPush(command,SERVER_HANDLING_TIME,INTEGER,¤tTime); // Update version Map; hashmapPut(internal->versionMap,version,command); // Update version list linkedListAddLast(internal->versions,intBuffer); } lockUnlock(internal->lock); }
/** * addArcToNode method * Like the non transactional schemaAdapter in addArcToNode entity, * but unlike the non transactional schemaAdapter, this schemaAdapter commits its changes directly on the entities * It also accumulate the command in order to rollback the changes in the client or commit the changes in the server */ int transactionAddArcToNode(Transaction transaction,Node parent, Node child,char arcName[],int arcNameLenght){ TransactionInternal*internal=(TransactionInternal*)transaction; int result; if(internal->transaction!=NULL){ result=transactionAddArcToNode(internal->transaction,parent, child, arcName,arcNameLenght); }else{ validateSchemaEntity(internal,parent); validateSchemaEntity(internal,child); int schemaId=internal->sessionMetaData->schemaId; int coreManagerId=internal->sessionMetaData->coreManagerId; int sessionId=internal->sessionMetaData->id; int lockTimeout= schemaAdapterGetLockTimeout(internal->scheamAdapter); Command command = commandBuilderCreateAddChildToParentNode(parent->id, child->id, arcName,arcNameLenght, schemaId, coreManagerId, sessionId, lockTimeout); commandPush(TRANSACTION, INTEGER, TRUE); result=commandExecuterConsume(internal->commandExecutor,command); arrayListAddLast(internal->commandList,command); } return result; }
int transactionRemoveArcFromNode(Transaction transaction,Node parent, char arcName[],int arcNameLenght){ TransactionInternal*internal=(TransactionInternal*)transaction; int result; if(internal->transaction!=NULL){ result=transactionRemoveArcFromNode(internal->transaction,parent, arcName,arcNameLenght); }else{ validateSchemaEntity(internal,parent); Arc arc; stringHashmapGet(parent->children,arcName,arcNameLenght,(Object*)&arc); if(arc!=NULL){ int schemaId=internal->sessionMetaData->schemaId; int coreManagerId=internal->sessionMetaData->coreManagerId; int sessionId=internal->sessionMetaData->id; int lockTimeout= schemaAdapterGetLockTimeout(internal->scheamAdapter); int true = TRUE; Command command = commandBuilderCreateAddChildToParentNode(parent->id,arc->child->id,arcName,arcNameLenght, schemaId, coreManagerId, sessionId,lockTimeout); commandPush(TRANSACTION, INTEGER, &true); result=commandExecuterConsume(internal->commandExecutor,command); arrayListAddLast(internal->commandList,command); }else{ return -1;
ArrayList registrySerializeToCommands(Registry registry){ RegistryInternal* internal=(RegistryInternal*)registry; ArrayList commands=arrayListCreate(); Array nodeArray; hashmapToArray(internal->nodes,&nodeArray); int length=arrayGetLength(nodeArray); int i; int coreManagerId=SERVER_COREMANAGER_ID; int serverSessionId=SERVER_SESSION_ID; for(i=0;i<length;i++){ Node node=arrayGetObject(nodeArray,i); int schemaId=node->schemaId; int locked=node->locked; int version=-1; int commandType=CREATE_NEW_ENTITY; int entityId=node->id; Command command=commandCreate(); commandPush(command,COMMAND_TYPE,INTEGER,&commandType,SIZE_OF_INT); commandPush(command,LOCK,INTEGER,&locked,SIZE_OF_INT); commandPush(command,SCHEMA_ID,INTEGER,&schemaId,SIZE_OF_INT); commandPush(command,ENTITY_ID,INTEGER,&entityId,SIZE_OF_INT); commandPush(command,VERSION,INTEGER,&version,SIZE_OF_INT); commandPush(command,ATTRIBUTES,ATTRIBUTES_MAP,node->attributes,CONTAINER_SIZE); commandPush(command,CORE_MANAGER_ID,INTEGER,&coreManagerId,0); commandPush(command,SESSION_ID,INTEGER,&serverSessionId,0); arrayListAddLast(commands,command); } for(i=0;i<length;i++){ Node node=arrayGetObject(nodeArray,i); NodeInternal* nodeInternal=(NodeInternal*)node; Array children; stringHashmapToArray(nodeInternal->children,&children); int childrenAmount=arrayGetLength(children); int version=-1; int schemaId=node->schemaId; int j; int commandType=ADD_CHILD_ENTITY_TO_PARENT_ENTITY; for(j=0;j<childrenAmount;j++){ Arc childArc=arrayGetObject(children,j); Command command=commandCreate(); commandPush(command,COMMAND_TYPE,INTEGER,&commandType,SIZE_OF_INT); commandPush(command,ENTITY_ID,INTEGER,&node->id,SIZE_OF_INT); commandPush(command,ENTITY_CHILD_ID,INTEGER,&childArc->child->id,SIZE_OF_INT); commandPush(command,SCHEMA_ID,INTEGER,&schemaId,SIZE_OF_INT); commandPush(command,VERSION,INTEGER,&version,SIZE_OF_INT); commandPush(command,CORE_MANAGER_ID,INTEGER,&coreManagerId,SIZE_OF_INT); commandPush(command,SESSION_ID,INTEGER,&serverSessionId,SIZE_OF_INT); commandPush(command,ARC_NAME,BYTE_ARRAY,childArc->arcName,childArc->arcLength); arrayListAddLast(commands,command); } arrayFree(children); } arrayFree(nodeArray); return commands; }
int validationManagerValidateOnCoreExecuteInternalRecurcive(ValidationManager validationManager,ArrayList commands,int administrator,int dept){ ValidationManagerStruct* internal=(ValidationManagerStruct*)validationManager; if(commands!=NULL&&dept<arrayListSize(commands)){ Command command=arrayListGetValueAt(commands,dept); int coreManagerId=commandGetInt(command,CORE_MANAGER_ID); int sessionId=commandGetInt(command,SESSION_ID); int transaction=commandGetInt(command,TRANSACTION); int schimaId=commandGetInt(command,SCHEMA_ID); int type=commandGetInt(command,COMMAND_TYPE); int version=commandGetInt(command,VERSION); int entityId=commandGetInt(command,ENTITY_ID); VALIDATE_TRANSACTION(transaction); VALIDATE_CORE_MANAGER_ID(coreManagerId); VALIDATE_SESSION_ID(sessionId); if(IS_CREATE_NEW_ENTITY(type)){ int lock=commandGetInt(command,LOCK); StringHashMap newAttributesMap=commandGetAttributes(command,ATTRIBUTES); VALIDATE(registryValidateNodeCreate(internal->registry,entityId)); Node node; nodeCreate(internal->registry,entityId,schimaId,lock,&node); stringHashmapPutAll(node->attributes,newAttributesMap); stringHashmapFree(newAttributesMap); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if( VALID!= result){ registryDeleteNode(internal->registry,node); return result; } char* buf; int length=nodeSerialize(node,&buf); commandPush(command,SERIALIZED_NODE,BYTE_ARRAY,buf,length); memoryFree(buf); } if(IS_ADD_CHILD_ENTITY_TO_PARENT_ENTITY(type)){ char* arcName; int childEntityId=commandGetInt(command,ENTITY_CHILD_ID); int arcNameLenght=commandGetString(command,ARC_NAME,&arcName); Node parent; registryGetNode(internal->registry,entityId,&parent); Node child; registryGetNode(internal->registry,childEntityId,&child); VALIDATE(registryValidateAddChild(parent,child,arcName,arcNameLenght,sessionId)); registryAddChild(internal->registry,parent,child,arcName,arcNameLenght); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if( VALID!= result){ registryRemoveChild(internal->registry,parent,arcName,arcNameLenght); return result; } char* buf; int length=nodeSerialize(parent,&buf); commandPush(command,SERIALIZED_NODE,BYTE_ARRAY,buf,length); memoryFree(buf); } if(IS_REMOVE_CHILD_ENTITY_FROM_PARENT_ENTITY(type)){ char* arcName; int childEntityId=commandGetInt(command,ENTITY_CHILD_ID); int arcNameLenght=commandGetString(command,ARC_NAME,&arcName); Node parent; registryGetNode(internal->registry,entityId,&parent); Node child; registryGetNode(internal->registry,childEntityId,&child); VALIDATE(registryValidateRemoveChild(parent,arcName,arcNameLenght,sessionId)); registryRemoveChild(internal->registry,parent,arcName,arcNameLenght); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if( VALID!= result){ registryAddChild(internal->registry,parent,child,arcName,arcNameLenght); return result; } char* buf; int length=nodeSerialize(parent,&buf); commandPush(command,SERIALIZED_NODE,BYTE_ARRAY,buf,length); memoryFree(buf); } if(IS_LOCK_ENTITY(type)){ int lock=commandGetInt(command,LOCK); int forceLock=commandGetInt(command,FORCE_LOCK); Node node; registryGetNode(internal->registry,entityId,&node); VALIDATE(registryValidateUpdateLock(node,lock,sessionId,forceLock,administrator)); registryUpdateLock(internal->registry,node,lock,sessionId); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if( VALID!= result){ int revertedLock=lock==TRUE?FALSE:TRUE; registryUpdateLock(internal->registry,node,revertedLock,sessionId); return result; } } if(IS_DELETE_ENTITY(type)){ int lock=commandGetInt(command,LOCK); Node node; VALIDATE(registryGetNode(internal->registry,entityId,&node)); VALIDATE(registryValidateDeleteNode(node,sessionId)); registryDeleteNode(internal->registry,node); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if( VALID!= result){ nodeCreate(internal->registry,entityId,schimaId,lock,&node); return result; } } if(IS_ADD_ATTRIBUTE(type)){ StringHashMap newAttributesMap=commandGetAttributes(command,ATTRIBUTES); Node node; registryGetNode(internal->registry,entityId,&node); VALIDATE(registryValidateUpdate(node,sessionId)); Array array; stringHashmapToArray(newAttributesMap,&array); int lenght=arrayGetLength(array); int i; StringHashMap toRemove = stringHashmapCreate(SMALL_CONTAINER_SIZE); for(i=0;i<lenght;i++){ Attribute newAttribute=(Attribute)arrayGetObject(array,i); Attribute oldAttribute; stringHashmapGet(node->attributes,newAttribute->name,newAttribute->nameLength,(Object) &oldAttribute); if(oldAttribute!=NULL){ stringHashmapPut(toRemove,newAttribute->name,newAttribute->nameLength,oldAttribute); stringHashmapRemove(node->attributes,newAttribute->name,newAttribute->nameLength); } stringHashmapPut(node->attributes,newAttribute->name,newAttribute->nameLength,(Object)newAttribute); } arrayFree(array); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if(result!=VALID){ stringHashmapToArray(newAttributesMap,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute newAttribute=(Attribute)arrayGetObject(array,i); stringHashmapRemove(node->attributes,newAttribute->name,newAttribute->nameLength); attributeFree(newAttribute,TRUE); } stringHashmapPutAll(node->attributes,toRemove); arrayFree(array); stringHashmapFree(toRemove); }else{ stringHashmapToArray(toRemove,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute oldAttribute=(Attribute)arrayGetObject(array,i); stringHashmapRemove(toRemove,oldAttribute->name,oldAttribute->nameLength); attributeFree(oldAttribute,TRUE); } arrayFree(array); stringHashmapFree(toRemove); } stringHashmapFree(newAttributesMap); char* buf; int length=nodeSerialize(node,&buf); commandPush(command,SERIALIZED_NODE,BYTE_ARRAY,buf,length); memoryFree(buf); } if(IS_REMOVE_ATTRIBUTE(type)){ StringHashMap toRemoveAttributeMap=commandGetAttributes(command,ATTRIBUTES); Node node; registryGetNode(internal->registry,entityId,&node); VALIDATE(registryValidateUpdate(node,sessionId)); Array array; stringHashmapToArray(toRemoveAttributeMap,&array); int lenght=arrayGetLength(array); int i; StringHashMap oldAttributes = stringHashmapCreate(SMALL_CONTAINER_SIZE); for(i=0;i<lenght;i++){ Attribute toRemoveAttribute=(Attribute)arrayGetObject(array,i); Attribute oldAttribute; stringHashmapGet(node->attributes,toRemoveAttribute->name,toRemoveAttribute->nameLength,(Object) &oldAttribute); if(oldAttribute!=NULL){ stringHashmapPut(oldAttributes,toRemoveAttribute->name,toRemoveAttribute->nameLength,(Object)oldAttribute); stringHashmapRemove(node->attributes,toRemoveAttribute->name,toRemoveAttribute->nameLength); } } arrayFree(array); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if(result!=VALID){ stringHashmapToArray(oldAttributes,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute oldAttribute=(Attribute)arrayGetObject(array,i); stringHashmapPut(node->attributes,oldAttribute->name,oldAttribute->nameLength,(Object)oldAttribute); } arrayFree(array); stringHashmapToArray(toRemoveAttributeMap,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute toRemoveAttribute=(Attribute)arrayGetObject(array,i); stringHashmapPut(toRemoveAttributeMap,toRemoveAttribute->name,toRemoveAttribute->nameLength,(Object)toRemoveAttribute); attributeFree(toRemoveAttribute,TRUE); } arrayFree(array); stringHashmapFree(toRemoveAttributeMap); }else{ stringHashmapToArray(oldAttributes,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute oldAttribute=(Attribute)arrayGetObject(array,i); stringHashmapRemove(oldAttributes,oldAttribute->name,oldAttribute->nameLength); attributeFree(oldAttribute,TRUE); } arrayFree(array); stringHashmapToArray(toRemoveAttributeMap,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute toAddAttribute=(Attribute)arrayGetObject(array,i); stringHashmapPut(toRemoveAttributeMap,toAddAttribute->name,toAddAttribute->nameLength,(Object)toAddAttribute); attributeFree(toAddAttribute,TRUE); } arrayFree(array); stringHashmapFree(toRemoveAttributeMap); } char* buf; int length=nodeSerialize(node,&buf); commandPush(command,SERIALIZED_NODE,BYTE_ARRAY,buf,length); memoryFree(buf); } if(IS_UPDATE_ENTITY(type)){ StringHashMap newAttributesMap=commandGetAttributes(command,ATTRIBUTES); Node node; registryGetNode(internal->registry,entityId,&node); StringHashMap oldAttributesMap=stringHashmapCreate(SMALL_CONTAINER_SIZE); stringHashmapPutAll(oldAttributesMap,node->attributes); Array array; stringHashmapToArray(node->attributes,&array); int lenght=arrayGetLength(array); int i; for(i=0;i<lenght;i++){ Attribute toRemoveAttribute=(Attribute)arrayGetObject(array,i); stringHashmapRemove(node->attributes,toRemoveAttribute->name,toRemoveAttribute->nameLength); } stringHashmapPutAll(node->attributes,newAttributesMap); int result=validationManagerValidateOnCoreExecuteInternalRecurcive(validationManager,commands,administrator,dept+1); if(result!=VALID){ stringHashmapToArray(node->attributes,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute toRemoveAttribute=(Attribute)arrayGetObject(array,i); stringHashmapRemove(node->attributes,toRemoveAttribute->name,toRemoveAttribute->nameLength); attributeFree(toRemoveAttribute,TRUE); } stringHashmapPutAll(node->attributes,oldAttributesMap); stringHashmapFree(newAttributesMap); stringHashmapFree(oldAttributesMap); }else{ stringHashmapToArray(oldAttributesMap,&array); lenght=arrayGetLength(array); for(i=0;i<lenght;i++){ Attribute oldAttribute=(Attribute)arrayGetObject(array,i); stringHashmapRemove(oldAttributesMap,oldAttribute->name,oldAttribute->nameLength); attributeFree(oldAttribute,TRUE); } stringHashmapFree(oldAttributesMap); stringHashmapFree(newAttributesMap); } char* buf; int length=nodeSerialize(node,&buf); commandPush(command,SERIALIZED_NODE,BYTE_ARRAY,buf,length); memoryFree(buf); } } return VALID; }