void *
MMTT_SharedMem::getMemory()
{
	if (!checkInfo())
	{
		return NULL;
	}

    if( myMemory == 0 )
    {
		if ((myAmOwner && createSharedMem()) || (!myAmOwner && openSharedMem()))
		{
#ifdef WIN32 
		    myMemory = MapViewOfFile(myMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
#else
		    assert(false);
		    myMemory = NULL;
#endif 
		    if (!myMemory)
				myErrorState = MMTT_SHM_ERR_UNABLE_TO_MAP;
		}
    }
    if (myMemory)
    {
		myErrorState = MMTT_SHM_ERR_NONE;
    }
    return myMemory;
}
bool
MMTT_SharedMem::open(const char *name,  unsigned int size, bool supportInfo)
{
	mySize = size;
	myMemory = 0;
	myMapping = 0;
    myName = 0;
	mySharedMemInfo = NULL;
    memset(myNamePostFix, 0, MMTT_SHM_MAX_POST_FIX_SIZE);
    myShortName = _strdup(name);

	mySupportInfo = supportInfo;

    size_t len = strlen(myShortName);

    createName();
    char *m = new char[strlen(myName) + 5 + 1];
    strcpy(m, myName);
    strcat(m, "Mutex");
    myMutex = new MMTT_Mutex(m);
    delete m;

    if (size > 0)
		myAmOwner = true;
    else
		myAmOwner = false;

    if (supportInfo)
    {
		if (!createInfo())
		{
			return false;
		}
    }
    else
    {
		mySharedMemInfo = NULL;
    }

    if (size > 0)
    {
		if (!createSharedMem())
		{
		    myErrorState = MMTT_SHM_ERR_ALREADY_EXIST;
		    return false;
		}
    }
    else
    {
		if (!openSharedMem())
		{
		    myErrorState = MMTT_SHM_ERR_DOESNT_EXIST;
		    return false;
		}
    }
    myErrorState = MMTT_SHM_ERR_NONE;
	return true;
}
Exemplo n.º 3
0
/**
 * Funkcia syncProcess vytvara procesy a synchronizuje ich.
 * @param param Struktura s vlastnostami pre procesy.
 * @return Chybovy navrat funkcie.
 */
int syncProcess(TParams *param)
{
  int error = EOK;           // Premmena pre chybu pri zdielanej pamati.
  param->mainpid = getpid(); // Proces id hlavneho programu.
  setSignals(getpid());      // Zachytenie signalov.
  signal(SIGCHLD, SIG_IGN);  // Ignorovanie sigchld a uvolnovanie procesov.

  // Inicializacia semaforov
  TMutx semaphores = {.mut0 = NULL, .mut1 = NULL, .mut2 = NULL ,.mut3 = NULL,
  .mut4 = NULL, .mut5 = NULL, .mut6 = NULL, .mut7 = NULL,};
  if(initSemaphores(&semaphores) != EOK)
  {
   destroySemaphores(&semaphores);
   return ESEMAP;
  }
  g_mutxs = &semaphores;

  // Vytvorenie zdielanej pamate.
  TSmem *memory = createSharedMem(&error,param,"/tmp");
  if(error) return ESHARED;
  g_mems = memory;
  g_shmid = param->sharedid;

  // Vytvorenie procesa holica.
  if((param->barberid = fork()) == -1)
  {
    destroySemaphores(&semaphores);
    unlinkSharedMem(memory,param->sharedid);
    return EFORK;
  }
  // Volanie holica.
  else if(param->barberid == 0)
  {
    syncBarber(memory, &semaphores, param);
    destroySemaphores(&semaphores);
    unlinkSharedMem(memory,param->sharedid);
    fclose(param->fw);
    exit(EXIT_SUCCESS);
  }
  // Vytvorenie procesov zakaznikov.
  else if(param->barberid > 0)
  {
    if(createCustomers(memory,param,&semaphores) == EFORK)
    {
      printECode(EFORK);
      kill(param->barberid, SIGUSR1);
      for(int i = 0; i < memory->created; i++)
        kill(memory->pids[0],SIGTERM);
      errorCleanAll(&semaphores,memory,param);
    }

    // Pockanie na proces barbra a uvolnenie zdielanej pamati.
    waitpid(param->barberid, NULL, 0);
    unlinkSharedMem(memory,param->sharedid);
    destroySemaphores(&semaphores);
  }
  return EOK;
}
Exemplo n.º 4
0
 void connectToServer(const char *address, const char *port,
         EventHandler *pProcessor) {
     this->client = pProcessor;
     setParent(pProcessor);
     createSharedMem();
     // Put destination  as context
     pProcessor->setContext((Context*) (void*) ServerDest);
     pProcessor->enable();
 }
void
MMTT_SharedMem::resize(unsigned int s)
{

    // This can't be called by someone that didn't create it in the first place
    // Also you can't resize it if you arn't using the info feature
    // Finally, don't set the size to 0, just delete this object if you want to clean it
    if (mySize > 0 && mySharedMemInfo && myAmOwner)
    {
		mySharedMemInfo->lock();
		MMTT_SharedMemInfo *info = (MMTT_SharedMemInfo*)mySharedMemInfo->getMemory();
		if (info && info->supported)
		{
		    detachInternal();
		    mySize = s;
		    // Keep trying until we find a name that works
		    do 
		    {
				randomizePostFix();
				createName();
		    } while(!createSharedMem());
		    memcpy(info->namePostFix, myNamePostFix, MMTT_SHM_MAX_POST_FIX_SIZE);
		}
		else // Otherwise, just try and detach and resize, if it fails give up
		{
		    detachInternal();
		    mySize = s;
		    if (!createSharedMem())
		    {
				myErrorState = MMTT_SHM_ERR_ALREADY_EXIST;
		    }

		}
		mySharedMemInfo->unlock();
    }
}
Exemplo n.º 6
0
int main(int argc, char **argv) {
    ACQ_MessagePacketType *packet;
    int currentPacket = 0;
    int numChannels = 356;
    int blockSize = 70;  /* > 28160/356 is too much for regular operation! */
    int sampleNumber = 0;
    int ID = 0;
    int sampleFreq = 1200;
    struct timeval timerInterval = {0,50000}; /*  50ms */
    struct timeval timerValue    = {0,50000};  /* 200ms */
    struct itimerval timerOpts;

    timerOpts.it_value = timerValue;
    timerOpts.it_interval = timerInterval;

    if (argc>=2) {
        sampleFreq = atoi(argv[1]);
        if (sampleFreq>0) {
            timerOpts.it_interval.tv_usec = timerOpts.it_value.tv_usec = 1000000*blockSize/sampleFreq;
        }
    }

    printf("Trying to set up shared memory...\n");
    packet = createSharedMem();
    if (packet == NULL) {
        fprintf(stderr, "Could not set up shared memory - exiting\n");
        exit(1);
    }
    initSharedMem(packet);

    packet[0].messageId    = 0;
    packet[0].sampleNumber = 0;
    packet[0].numSamples   = 0;
    packet[0].numChannels  = numChannels;
    strcpy((char *)(packet[0].data), "test.ds");
    packet[0].message_type = ACQ_MSGQ_SETUP_COLLECTION;

    currentPacket = 1;

    printf("Wrote setup packet at 0\n");


    usleep(200000);  /* 0.2 sec */
    setitimer(ITIMER_REAL, &timerOpts, NULL);
    signal(SIGALRM, alarmHandler);
    /* register CTRL-C handler */
    signal(SIGINT,  abortHandler);


    while (keepRunning) {
        ++ID;

        if (packet[currentPacket].message_type != ACQ_MSGQ_INVALID) {
            printf("Packet #%i not free...\n", currentPacket);
            break;
        }

        packet[currentPacket].messageId = ID;
        packet[currentPacket].sampleNumber = sampleNumber;
        packet[currentPacket].numSamples = blockSize;
        packet[currentPacket].numChannels = numChannels;
        memset(packet[currentPacket].data, 0, numChannels * blockSize * sizeof(int));
        //packet[currentPacket].data[0] = i+1;
        //packet[currentPacket].data[10*numChannels] = 10;
        packet[currentPacket].numChannels = numChannels;
        packet[currentPacket].message_type = ACQ_MSGQ_DATA;

        printf("Wrote data packet %i at %i\n", ID, currentPacket);

        sampleNumber += blockSize;

        if (++currentPacket == ACQ_MSGQ_SIZE) currentPacket = 0;

        sleep(1);
    }

    packet[currentPacket].message_type = ACQ_MSGQ_CLOSE_CONNECTION;
    usleep(100000);

    closeSharedMem(packet);
    return 0;
}
Exemplo n.º 7
0
int main(int argc, char **argv) {
	ACQ_MessagePacketType *packet;
	int currentPacket, intSlot;
	int lastId;
	int numChannels, numSamples, sampleNumber;
	host_t host;
	pthread_t tcpserverThread, convertThread;
	
	/* Parse command line arguments */
	if (argc == 1) {
		/* Put defaults in case no arguments given */
		strcpy(host.name, "-");
		host.port = 1972;
	} else if (argc == 3) {
		strncpy(host.name, argv[1], sizeof(host.name));
		host.name[sizeof(host.name)-1]=0;
		
		host.port = atoi(argv[2]);
		if (host.port <= 0) {
			fputs("Port number must be positive\n\n",stderr);
			fputs(usage, stderr);
			return 1;
		}
	} else {
		fputs(usage, stderr);
		return 1;
	}
	
	/* Create socket pair */
	if (socketpair(AF_UNIX, SOCK_STREAM, 0, mySockets)) {
		perror("Could not create socket pair for internal communication: ");
		return 1;
	}
	
	/*  Internal ringbuffer uses same mechanism as Acq: free slots are
		marked with ACQ_MSGQ_INVALID. To avoid polling in the converter thread,
		the main thread sends the index of the latest slot over the socket pair.
		If the converter thread does not manage to clear the ringbuffer quickly
		enough, the program will report an error. */
	for (intSlot=0;intSlot<INT_RB_SIZE;intSlot++) {
		intPackets[intSlot].message_type = ACQ_MSGQ_INVALID;
	}
	
	/* Spawn conversion thread */
	if (pthread_create(&convertThread, NULL, dataToFieldTripThread, NULL)) {
		fprintf(stderr, "Could not spawn conversion thread\n");
		return 1;
	}
	
	/* Spawn tcpserver or connect to remote buffer */
	if (strcmp(host.name, "-") == 0) {
		int rc;
		
		rc = pthread_create(&tcpserverThread, NULL, tcpserver, &host);
		if (rc) {
			fprintf(stderr, "Could not spawn tcpserver thread (%d)\n", rc);
			return 1;
		}
		ftSocket = 0; /* dma */
	} else {
		ftSocket = open_connection(host.name, host.port);
		
		if (ftSocket < 0) {
			fprintf(stderr, "Could not connect to remote buffer\n");
			return 1;
		}
	}		
	
	printf("Trying to set up shared memory...\n");
	packet = createSharedMem();
	if (packet == NULL) {
		fprintf(stderr, "Could not set up shared memory - exiting\n");
		exit(1);
	}
	initSharedMem(packet);
	
	/* register CTRL-C handler */
	signal(SIGINT, abortHandler);
	
	/* Both ringbuffers start at 0 */
	currentPacket = intSlot = 0;
	
	printf("Entering main loop. Press CTRL-C to stop operation.\n\n");
	while (keepRunning) {
		int size;
		char *dataset;
		int code = waitPacket(&packet[currentPacket].message_type);
		
		switch(code) {
			case ACQ_MSGQ_SETUP_COLLECTION:
				lastId = packet[currentPacket].messageId;
				dataset = (char *) packet[currentPacket].data;
				printf("Setup | ID=%i | %.255s\n", lastId, dataset);
				
				size = 5*sizeof(int) + strlen(dataset) + 1;
				if (size >= sizeof(ACQ_MessagePacketType)) {
					fprintf(stderr, "Dataset name not zero-terminated -- ignoring\n");
					continue;
				}
				
				if (intPackets[intSlot].message_type != ACQ_MSGQ_INVALID) {
					fprintf(stderr, "Internal converter thread does not keep up with the load.\n");
				} else {
					memcpy(&intPackets[intSlot], &packet[currentPacket], size);
					write(mySockets[0], &intSlot, sizeof(int));
					if (++intSlot == INT_RB_SIZE) intSlot=0;
				}
				
				packet[currentPacket].message_type = ACQ_MSGQ_INVALID;
				if (++currentPacket == ACQ_MSGQ_SIZE) currentPacket=0;
				break;
			case ACQ_MSGQ_DATA:
				lastId = packet[currentPacket].messageId;
				numChannels  = packet[currentPacket].numChannels;
				numSamples   = packet[currentPacket].numSamples;
				sampleNumber = packet[currentPacket].sampleNumber;
				printf("Data | %3i channels x %3i samples | nr = %6i | ID=%4i | slot=%3i\n", numChannels, numSamples, sampleNumber, lastId, currentPacket);
								
				size = 5*sizeof(int) + numSamples*numChannels*sizeof(int);
				
				if (size > sizeof(ACQ_OverAllocType)) {
					fprintf(stderr, "Acq wrote far too much data -- cannot handle this\n");
				} else {
					if (intPackets[intSlot].message_type != ACQ_MSGQ_INVALID) {
						fprintf(stderr, "Internal converter thread does not keep up with the load.\n");
					} else {
						memcpy(&intPackets[intSlot], &packet[currentPacket], size);
						write(mySockets[0], &intSlot, sizeof(int));
						if (++intSlot == INT_RB_SIZE) intSlot=0;
					}
				}				
				
				if (numSamples * numChannels > 28160) {
					fprintf(stderr, "Warning: Acq wrote too much data into this block!\n");
            
					/* clear this packet */
					packet[currentPacket].message_type = ACQ_MSGQ_INVALID;
					/* next packet last in ringbuffer? */ 
					if (++currentPacket == ACQ_MSGQ_SIZE) {
						currentPacket = 0;
					} else {
						/* make sure the next packet is marked as free */
						packet[currentPacket].message_type = ACQ_MSGQ_INVALID;
					}
				} else {
					packet[currentPacket].message_type = ACQ_MSGQ_INVALID;
					if (++currentPacket == ACQ_MSGQ_SIZE) currentPacket=0;
				}
				break;
			case ACQ_MSGQ_CLOSE_CONNECTION:
				printf("Stop | ID=%i \n", lastId);
				
				/* Does this mean anything for the buffer as well ? */
				
				packet[currentPacket].message_type = ACQ_MSGQ_INVALID;
				currentPacket = 0;
				break;
			case ACQ_MSGQ_INVALID:
				printf("Waiting (on %i)\n",currentPacket);
				break;
			default:
				fprintf(stderr, "Warning: Unexpected ID in packet %i\n", currentPacket);
				break;
		}
	}
	printf("Closing sockets / stopping tcpserver...\n");
	close(mySockets[0]); /* the other one will be closed in the thread */
	if (ftSocket > 0) {
		close_connection(ftSocket);
	} else {
		pthread_cancel(tcpserverThread);
		pthread_detach(tcpserverThread);
	}
	printf("Closing shared memory...\n");
	closeSharedMem(packet);
	printf("Joining conversion thread...\n");
	pthread_join(convertThread, NULL);
	printf("Done.\n");
	return 0;
}
Exemplo n.º 8
0
 void bindServer(const char *port, EventHandler *pProcessor) {
     this->server = pProcessor;
     setParent(pProcessor);
     pProcessor->setContext((Context*) (void*) ClientDest);
     createSharedMem();
 }