MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    port = new QSerialPort(this);
    //每500ms更新一次progress bar
    clock = new QTimer;
    clock->setInterval(500);
    connect(clock,SIGNAL(timeout()),this,SLOT(speedMeter()));
    //每20ms收取RS232 Data
    RS232Receive = new QTimer;
    RS232Receive->setInterval(20);
    RS232Receive->start();
    connect(RS232Receive,SIGNAL(timeout()),this,SLOT(receiveMsg()));
    //偵測電腦上可用之COM Port
    QList<QSerialPortInfo> portList;
    portList = QSerialPortInfo::availablePorts();
    for(int i = 0; i < QSerialPortInfo::standardBaudRates().size(); i++)
    {
        ui->port_baud_comboBox->insertItem(i,QString::number(QSerialPortInfo::standardBaudRates().at(i)));
    }
    for(int i = 0; i < portList.size(); i++)
    {
        ui->port_name_comboBox->insertItem(i,portList.at(i).portName()+" "+portList.at(i).description());
    }
}
예제 #2
0
static int flushAndDestroyQueue(char* pcQueueName, int iQueueId)
{
    int i,iReadLength;
    AppMsg sMsg;
    QueueAttr sAttr;

    memset(&sMsg,0,sizeof(AppMsg));
    if (getQueueAttr(iQueueId,&sAttr) == 0)
    {
        for (i = 0; i < sAttr.liCurMsgs; i++)
        {
            if (receiveMsg(iQueueId,(unsigned char*)&sMsg,sizeof(AppMsg),(int*)(&iReadLength)) > 0)
            {
                if (sMsg.iDataSize)
                    free(sMsg.pvData);
            }
        }
    }
    if (closeMsgQueue(iQueueId) < 0)
        DEBUG_PRINT("Error closing %s\n",pcQueueName);

    if (unlinkMsgQueue(pcQueueName) < 0)
        DEBUG_PRINT("Error unlinking %s\n",pcQueueName);

    return 0;
}
예제 #3
0
    void process()
    {
        CMessageBuffer msg;
        unsigned inputs = container.getInputs();
        unsigned slaves = container.queryJob().querySlaves();
        unsigned s;
        bool readSome=false, slaveReadSome;

        IntArray replyTags;
        for (s=0; s<slaves; s++)
            replyTags.append(0);
        while (inputs>1)
        {
            inputs--;
            for (s=0; s<slaves; s++)
            {
                rank_t sender;
                if (!receiveMsg(msg, RANK_ALL, replyTag, &sender)) return;

                replyTags.replace(msg.getReplyTag(), ((int)sender)-1);
                msg.read(slaveReadSome);
                if (slaveReadSome) readSome = true;
            }
            msg.clear().append(readSome);
            for (s=0; s<slaves; s++)
            {
                if (!queryJobChannel().queryJobComm().send(msg, ((rank_t) s+1), (mptag_t) replyTags.item(s), LONGTIMEOUT))
                    throw MakeActivityException(this, 0, "Failed to give result to slave");
            }
            if (readSome) // got some, have told slaves to ignore rest, so finish
                break;
        }
    }
예제 #4
0
void WMVBusClient::run() {
		threadActive = true;

		while(threadActive) {
				/*if (busStatus.device_count != 0) {
						if (((busStatus.frame_counter) >= busStatus.lower_limit) && ((busStatus.frame_counter) < busStatus.upper_limit)) {
						busStatus.flags |= (1<<BUS_STATUS_TIME_SLOT_ACTIVE);
						}
						else {
								busStatus.flags &= ~(1<<BUS_STATUS_TIME_SLOT_ACTIVE);
						}

						if (busStatus.frame_counter >= busStatus.device_count_mult) {
								busStatus.frame_counter = 0;
								busStatus.wraparounds++;
						}
						else
								busStatus.frame_counter++;
				}*/

				//Check to see if there are any characters waiting from the bus
        receiveMsg();

				//If there is a message in the queue, then we send it to the "Main Application" for it to be parsed
				if (messageInRXQueue())
					msgListener->recvBusMessage(getMessageInRXQueue());

				//we use the same interval as the hardware devices
				QThread::usleep(BUS_TIME_INTERRUPT_INTERVAL);
		}
}
예제 #5
0
 void process()
 {
     if (!container.queryLocal() && container.queryJob().querySlaves() > 1)
     {
         CMessageBuffer msg;
         unsigned nslaves = container.queryJob().querySlaves();
         unsigned s = 1;
         rowcount_t totalCount = 0, slaveCount;
         for (; s<=nslaves; s++)
         {
             if (!receiveMsg(msg, s, mpTag))
                 return;
             msg.read(slaveCount);
             if (RCUNSET == slaveCount)
             {
                 totalCount = RCUNSET;
                 break; // unknown
             }
             totalCount += slaveCount;
         }
         s=1;
         msg.clear().append(totalCount);
         for (; s<=nslaves; s++)
             container.queryJob().queryJobComm().send(msg, s, mpTag);
     }
 }
예제 #6
0
bool CommSocketClient::sendMsg(const Message *msg, bool waitAck)
{
    int n;
    Message ack;
    Message m;
    
    if(!msg)
        return false;
    memcpy(&m, msg, sizeof(Message));
    if(waitAck)
        m.msg = (CommBase::msgtype) ((int)CommBase::msg_ack | (int)m.msg); 
    n=write(clientSocket, m.data, MESSAGE_LENGTH);
    if(n==0)
    {
        close(clientSocket);
        throw EConnectionClosed();
    }
    if(n!=MESSAGE_LENGTH)
        return false;
    if(waitAck)
    {
        for(;;)
        {
            ack.sender=m.receiver;
            if(!receiveMsg(&ack))
                return false;
            if(!(ack.msg & CommBase::msg_ack))
                continue;
            break;
        }
    }
    return true;
}
예제 #7
0
void CommClass::processData() {
  if (serialPort->isOpen()) {
    receiveMsg();

    checkTXQueue();

    rxTimeout++;

    if (rxMessage.size() > 0) {
      if (rxTimeout > COMM_INTERFACE_RX_TIMEOUT_LIMIT) {
        rxMessage.clear();

        rxTimeout = 0;
      }
    }

    if (txMsgAcked == 0) {
      if ((txTimeout > COMM_INTERFACE_TX_TIMEOUT_LIMIT) && (resendFlag == 0)) {
        resendFlag = 1;
      }
      else
        txTimeout++;
    }
  }
}
예제 #8
0
bool CommQSocketClient::sendMsg(const Message *msg, bool waitAck)
{
    int n;
    Message ack;
    Message m;
    
    if(!msg)
        return false;
    memcpy(&m, msg, sizeof(CommBase::Message));
    if(waitAck)
        m.msg |= CommBase::msg_ack; 
    if(!clientSocket.connected())
        throw EConnectionClosed();
    n=write(m.data, MESSAGE_LENGTH);
    if(n!=MESSAGE_LENGTH)
        return false;
    if(waitAck)
    {
        for(;;)
        {
            ack.sender=m.receiver;
            if(!receiveMsg(&ack))
                return false;
            if(!(ack.msg & CommBase::msg_ack))
                continue;
            break;
        }
    }
    return true;
}
예제 #9
0
//Reconecta el puerto serie donde digamos
int ComThread::Reconectar(unsigned char index,BaudRateType ibaud){
	
	char temp[6];

    //snprintf(temp,sizeof(temp),"COM%d",index);
    //snprintf(temp,sizeof(temp),"/dev/ttyS%d",iPuerto-1);

	if (iRun==1) SerialMicro->close();
	iRun=0;
	if (SerialMicro){
		SerialMicro->setPortName(temp);
	}
	else{
		SerialMicro=new SerialPort(temp);
	}
	if (SerialMicro){
		SerialMicro->setBaudRate(ibaud);   
		SerialMicro->setFlowControl(FLOW_OFF);
		SerialMicro->setParity(PAR_NONE);    
		SerialMicro->setDataBits(DATA_8);   
		SerialMicro->setStopBits(STOP_1);
		if (SerialMicro->open(QIODevice::ReadWrite)){
		//A leer
			connect(SerialMicro, SIGNAL(readyRead()), this, SLOT(receiveMsg()));
			iRun=1;
		}
		else iRun=-1;
	}
	else iRun=-1;

	return iRun;
}
예제 #10
0
파일: commclass.cpp 프로젝트: lwid/openasc
void CommClass::run() {
  threadActive = true;

  while(threadActive) {
    receiveMsg();

    checkTXQueue();

    rxTimeout++;

    if (rxMessage.size() > 0) {
      if (rxTimeout > COMM_INTERFACE_RX_TIMEOUT_LIMIT) {
        rxMessage.clear();

        rxTimeout = 0;
      }
    }

    if (txMsgAcked == 0) {
      if (txTimeout > COMM_INTERFACE_TX_TIMEOUT_LIMIT) {
        resendFlag = 1;
        txTimeout = 0;
      }
      else
        txTimeout++;
    }

    QThread::usleep(1000);
  }
}
예제 #11
0
    void process()
    {
        ActPrintLog("ReDistributeActivityMaster::process");
        HashDistributeMasterBase::process();        
        IHThorHashDistributeArg *helper = (IHThorHashDistributeArg *)queryHelper(); 
        unsigned n = container.queryJob().querySlaves();
        MemoryAttr ma;
        offset_t *sizes = (offset_t *)ma.allocate(sizeof(offset_t)*n);
        unsigned i;
        try {
            for (i=0;i<n;i++) {
                if (abortSoon)
                    return;
                CMessageBuffer mb;
#ifdef _TRACE
                ActPrintLog("ReDistribute process, Receiving on tag %d",statstag);
#endif
                rank_t sender;
                if (!receiveMsg(mb, RANK_ALL, statstag, &sender)||abortSoon) 
                    return;
#ifdef _TRACE
                ActPrintLog("ReDistribute process, Received size from %d",sender);
#endif
                sender--;
                assertex((unsigned)sender<n);
                mb.read(sizes[sender]);
            }
            ActPrintLog("ReDistributeActivityMaster::process sizes got");
            for (i=0;i<n;i++) {
                CMessageBuffer mb;
                mb.append(n*sizeof(offset_t),sizes);
#ifdef _TRACE
                ActPrintLog("ReDistribute process, Replying to node %d tag %d",i+1,statstag);
#endif
                if (!container.queryJob().queryJobComm().send(mb, (rank_t)i+1, statstag))
                    return;
            }
            // check if any max skew broken
            double maxskew = helper->getTargetSkew();
            if (maxskew>helper->getSkew()) {
                offset_t tot = 0;
                for (i=0;i<n;i++) 
                    tot += sizes[i];
                offset_t avg = tot/n;
                for (i=0;i<n;i++) {
                    double r = ((double)sizes[i]-(double)avg)/(double)avg;
                    if ((r>=maxskew)||(-r>maxskew)) {
                        throw MakeActivityException(this, TE_DistributeFailedSkewExceeded, "DISTRIBUTE maximum skew exceeded (node %d has %"I64F"d, average is %"I64F"d)",i+1,sizes[i],avg);
                    }               
                }
            }

        }
        catch (IException *e) {
            ActPrintLog(e,"ReDistribute");
            throw;
        }
        ActPrintLog("ReDistributeActivityMaster::process exit");
    }
예제 #12
0
QespTest::QespTest(QWidget *parent)
    : QWidget(parent)

{
    //modify the port settings on your own
#ifdef Q_OS_UNIX
    port = new QextSerialPort(QLatin1String("/dev/ttyS0"), QextSerialPort::Polling);
#else
    port = new QextSerialPort(QLatin1String("COM1"), QextSerialPort::Polling);
#endif /*Q_OS_UNIX*/
    port->setBaudRate(BAUD19200);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_2);
    //set timeouts to 500 ms
    port->setTimeout(500);

    message = new QLineEdit(this);

    // transmit receive
    QPushButton *transmitButton = new QPushButton(tr("Transmit"));
    connect(transmitButton, SIGNAL(clicked()), SLOT(transmitMsg()));
    QPushButton *receiveButton = new QPushButton(tr("Receive"));
    connect(receiveButton, SIGNAL(clicked()), SLOT(receiveMsg()));
    QHBoxLayout *trLayout = new QHBoxLayout;
    trLayout->addWidget(transmitButton);
    trLayout->addWidget(receiveButton);

    //CR LF
    QPushButton *CRButton = new QPushButton(tr("CR"));
    connect(CRButton, SIGNAL(clicked()), SLOT(appendCR()));
    QPushButton *LFButton = new QPushButton(tr("LF"));
    connect(LFButton, SIGNAL(clicked()), SLOT(appendLF()));
    QHBoxLayout *crlfLayout = new QHBoxLayout;
    crlfLayout->addWidget(CRButton);
    crlfLayout->addWidget(LFButton);

    //open close
    QPushButton *openButton = new QPushButton(tr("Open"));
    connect(openButton, SIGNAL(clicked()), SLOT(openPort()));
    QPushButton *closeButton = new QPushButton(tr("Close"));
    connect(closeButton, SIGNAL(clicked()), SLOT(closePort()));
    QHBoxLayout *ocLayout = new QHBoxLayout;
    ocLayout->addWidget(openButton);
    ocLayout->addWidget(closeButton);

    received_msg = new QTextEdit();

    QVBoxLayout *myVBox = new QVBoxLayout;
    myVBox->addWidget(message);
    myVBox->addLayout(crlfLayout);
    myVBox->addLayout(trLayout);
    myVBox->addLayout(ocLayout);
    myVBox->addWidget(received_msg);
    setLayout(myVBox);

    qDebug("isOpen : %d", port->isOpen());
}
예제 #13
0
 void getGlobalCounts()
 {
     if (container.queryLocalOrGrouped())
         return;
     CMessageBuffer msg;
     if (!receiveMsg(msg, 0, mpTag))
         return;
     memcpy(totalCounts, msg.readDirect(numSets*sizeof(rowcount_t)), numSets*sizeof(rowcount_t));
     memcpy(priorCounts, msg.readDirect(numSets*sizeof(rowcount_t)), numSets*sizeof(rowcount_t));
 }
    const void *getResult(const void *firstRow)
    {
        IHThorAggregateArg *helper = (IHThorAggregateArg *)baseHelper.get();
        unsigned numPartialResults = container.queryJob().querySlaves();
        if (1 == numPartialResults)
            return firstRow;

        CThorExpandingRowArray partialResults(*this, this, true, stableSort_none, true, numPartialResults);
        if (hadElement)
            partialResults.setRow(0, firstRow);
        --numPartialResults;

        size32_t sz;
        while (numPartialResults--)
        {
            CMessageBuffer msg;
            rank_t sender;
            if (!receiveMsg(msg, RANK_ALL, mpTag, &sender))
                return NULL;
            if (abortSoon)
                return NULL;
            msg.read(sz);
            if (sz)
            {
                assertex(NULL == partialResults.query(sender-1));
                CThorStreamDeserializerSource mds(sz, msg.readDirect(sz));
                RtlDynamicRowBuilder rowBuilder(queryRowAllocator());
                size32_t sz = queryRowDeserializer()->deserialize(rowBuilder, mds);
                partialResults.setRow(sender-1, rowBuilder.finalizeRowClear(sz));
            }
        }
        RtlDynamicRowBuilder rowBuilder(queryRowAllocator(), false);
        bool first = true;
        numPartialResults = container.queryJob().querySlaves();
        unsigned p=0;
        for (;p<numPartialResults; p++)
        {
            const void *row = partialResults.query(p);
            if (row)
            {
                if (first)
                {
                    first = false;
                    sz = cloneRow(rowBuilder, row, queryRowMetaData());
                }
                else
                    sz = helper->mergeAggregate(rowBuilder, row);
            }
        }
        if (first)
            sz = helper->clearAggregate(rowBuilder);
        return rowBuilder.finalizeRowClear(sz);
    }
예제 #15
0
int sendLong( tid_t tid, void *data, size_t len, int timeout )
{
  volatile struct LongMsgHeader *long_msg;
  volatile struct Message msg;
  int num_sent = 0;

  if( data == NULL || tid == NULL_TID )
    return -1;

  long_msg = (volatile struct LongMsgHeader *)msg.data;

  long_msg->length = len;
  long_msg->fail = 0;
  long_msg->reply = 0;

  msg.protocol = MSG_PROTO_LONG;
  msg.length = len > sizeof msg.data - sizeof *long_msg ?
               sizeof msg.data :
               len + sizeof *long_msg;

  num_sent = msg.length - sizeof *long_msg;

  memcpy( (void *)(long_msg + 1), data, num_sent );

  if( sendMsg(tid, (struct Message *)&msg, timeout) < 0 )
    return -1;

  if( receiveMsg(tid, (struct Message *)&msg, timeout) < 0 || long_msg->fail )
    return -1;

  len = long_msg->length < len ? long_msg->length : len;

  if( (int)len <= num_sent )
    return (int)len;

  while( num_sent < (int)len )
  {
    msg.protocol = MSG_PROTO_LONG;

    if( num_sent + sizeof msg.data >= len ) // This is the last message to send
      msg.length = len - num_sent;
    else
      msg.length = sizeof msg.data;

    memcpy( (void *)msg.data, (void *)((unsigned)data + num_sent), msg.length );
    num_sent += msg.length;

    if( sendMsg(tid, (struct Message *)&msg, timeout) < 0 )
      return (int)num_sent - (int)sizeof msg.data;
  }

  return num_sent;
}
예제 #16
0
파일: main.c 프로젝트: rednaks/ecHoSystem
int main(int argc, char **argv) {


  if(argc < 3) {
    printHelp();
    return -1;
  }

  me.id = atoi(argv[1]);
  me.threshold = atoi(argv[2]);
  //me.useNumber = atoi(argv[3]);

  int sockfd = Connect("localhost", 9999);

    if(sockfd >= 0) {
    pthread_t th;
    pthread_create(&th, NULL, learningThread, &sockfd);
    while(1) {
      printf("Do you want to simulate a use ?\n");
      printf("press 'y' to do so\n press 'n' to exit learn mode\n");
      char input;
      scanf(" %c", &input);
      printf("Inp %c\n",input);
      if(input == 'y')  {
        if(isLearning){
          me.useNumber++;
        }else if(!isLearning){
          Message msg;
          msg.client_id = me.id;
          msg.cmd = USE_CMD;
          msg.arg = USE_REQ;
          sendMsg(sockfd, msg);

          receiveMsg(sockfd, &msg);
          if(msg.cmd == USE_CMD){
            if(msg.arg == USE_OK){
              printf("I'm allowed to run :)\n");
            }else if(msg.arg == USE_NO) {
              printf("I'm not allowed to run :(\n");
            }
          }
        }
      }

    }
  } else {
    printf("Erreur : %d\n", sockfd);
  }


  return 0;
}
예제 #17
0
파일: client.cpp 프로젝트: malimome/LoSt
void doHandshake(int numOfChallenges, int flag, int sock)
{
  char hn[256]={0};
  sendMsg("HELLO", 6, sock);
  receiveMsg(hn, 8, sock);
  if (strncmp(hn, "GOAHEAD", 7))
  {
    closeSocket(sock);
    exit(0);
  }
  sendObj(&numOfChallenges, sizeof(int), sock);
  if (flag)
    sendMsg("P", 2, sock);
  else
    sendMsg("R", 2, sock);
  receiveMsg(hn, 8, sock);
  if (strncmp(hn, "GOAHEAD", 7))
  {
    closeSocket(sock);
    exit(0);
  }
}
예제 #18
0
파일: server.c 프로젝트: rednaks/ecHoSystem
void sendInfoRequestAnWaitReponse(int sockfd, ClientInfo *aClient) {
  Message msg;
  msg.client_id = aClient->id;
  msg.cmd = INFO_CMD;
  msg.arg = INFO_USE_NUM;

  sendMsg(sockfd, msg);

  // We wait for the info from client:
  receiveMsg(sockfd, &msg);
  if(msg.cmd == INFO_CMD) {
    aClient->useAverage = msg.arg;
  }
}
예제 #19
0
    void getTallies() // NB: not called on first node.
    {
        CMessageBuffer msg;
        if (!receiveMsg(msg, queryJobChannel().queryMyRank()-1, mpTag))
            return;
        memcpy(tallies, msg.readDirect(numSets*sizeof(unsigned)), numSets*sizeof(unsigned));
#if THOR_TRACE_LEVEL >= 5
        StringBuffer s;
        unsigned idx=0;
        for (; idx<numSets; idx++)
            s.append("[").append(tallies[idx]).append("]");
        ActPrintLog("CHOOSESETS: Incoming count = %s", s.str());
#endif
    }
예제 #20
0
bool Client::readMsg(Codes code)
{
	std::cout << "Reading " << code << std::endl;
	switch (code)
	{
	case READY: {
		// Ready message from server contains chunk size
		uint32_t num;
		if(receiveIntMsg(code, num))
		{
			_chunkSize = num;
			return true;
		}
		break;
	}

	case SENDING:
		return receiveMsg(code, _chunkSize);

	case FINISHING:{
		// Finishing message from server contains number of bytes left to send
		uint32_t spareBytes;
		if (receiveIntMsg(code, spareBytes))
		{
			if (receiveMsg(SENDING, spareBytes))
				return true;
		}
		break;
	}

	default:
		// Just check if codes agree
		return receiveMsg(code, 0);
	}

	return false;
}
예제 #21
0
void SerialManagerView::setupGUI() {






    // transmit receive
    QPushButton *transmitButton = new QPushButton(tr("Transmit"));
    connect(transmitButton, SIGNAL(clicked()), SLOT(transmitMsg()));
    QPushButton *receiveButton = new QPushButton(tr("Receive"));
    connect(receiveButton, SIGNAL(clicked()), SLOT(receiveMsg()));
    QHBoxLayout *trLayout = new QHBoxLayout;
    trLayout->addWidget(transmitButton);
    trLayout->addWidget(receiveButton);

    //CR LF
    QPushButton *CRButton = new QPushButton(tr("CR"));
    connect(CRButton, SIGNAL(clicked()), SLOT(appendCR()));
    QPushButton *LFButton = new QPushButton(tr("LF"));
    connect(LFButton, SIGNAL(clicked()), SLOT(appendLF()));
    QHBoxLayout *crlfLayout = new QHBoxLayout;
    crlfLayout->addWidget(CRButton);
    crlfLayout->addWidget(LFButton);

    //open close
    QPushButton *openButton = new QPushButton(tr("Open"));
    connect(openButton, SIGNAL(clicked()), SLOT(openPort()));
    QPushButton *closeButton = new QPushButton(tr("Close"));
    connect(closeButton, SIGNAL(clicked()), SLOT(closePort()));
    QHBoxLayout *ocLayout = new QHBoxLayout;
    ocLayout->addWidget(openButton);
    ocLayout->addWidget(closeButton);

    received_msg = new QTextEdit();

    QVBoxLayout *myVBox = new QVBoxLayout;
    myVBox->addWidget(message);
    myVBox->addLayout(crlfLayout);
    myVBox->addLayout(trLayout);
    myVBox->addLayout(ocLayout);
    myVBox->addWidget(received_msg);
    setLayout(myVBox);

    qDebug("isOpen : %d", port->isOpen());

    /// configure textEdit
    message->installEventFilter(this);
}
예제 #22
0
 const void * getResult()
 {
     CMessageBuffer mb;
     if (receiveMsg(mb, 0, mpTag)) {
         size32_t sz;
         mb.read(sz);
         if (sz) {
             CThorStreamDeserializerSource ds(sz,mb.readDirect(sz));
             RtlDynamicRowBuilder rowBuilder(queryRowAllocator());
             size32_t sz = queryRowDeserializer()->deserialize(rowBuilder,ds);
             return rowBuilder.finalizeRowClear(sz);
         }
     }
     return NULL;
 }
예제 #23
0
파일: server.c 프로젝트: rednaks/ecHoSystem
void sendFirstInfoRequest(int sockfd, ClientInfo *aClient) {
  Message msg;
  msg.client_id = NO_ARGS;
  msg.cmd = INFO_CMD;
  msg.arg = INFO_THRESHOLD;
  
  sendMsg(sockfd, msg);

  receiveMsg(sockfd, &msg);

  if(msg.cmd == INFO_CMD) {
    aClient->id = msg.client_id;
    aClient->threshold = msg.arg;
    aClient->useAverage = 0;
  }
}
예제 #24
0
int main(int argc, char *argv[])
{
	WSADATA wsa;
	SOCKET s, new_socket;
	char *message;
	int done = 1;
	char server_reply[200];

	printf("\nInitialising Winsock...");
	if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
	{
		printf("Failed. Error Code : %d", WSAGetLastError());
		return 1;
	}

	printf("Initialized.\n");

	// Create Socket
	createSocket(&s);

	// Bind Socket
	bindSocket(s, 1456);

	//Listen to incoming connections
	listen(s, 3);

	//Accept and incoming connection
	puts("Waiting for incoming connections...");

	//Accept connection
	acceptConnection(s, &new_socket);

	while (done) {
		receiveMsg(new_socket, server_reply, 200);
		printf("received from client: %s\n", server_reply);
		if (!strcmp(server_reply, "bye")){
			done = 0;
		}
		sendMsg(new_socket, server_reply);
	}
	closesocket(s);
	WSACleanup();

	return 0;
}
예제 #25
0
    virtual void process()
    {
        CChooseSetsActivityMaster::process();

        IHThorChooseSetsArg *helper = (IHThorChooseSetsArg *)queryHelper();
        unsigned numSets = helper->getNumSets();
        unsigned nslaves = container.queryJob().querySlaves();

        MemoryBuffer countMb;
        rowcount_t *counts = (rowcount_t *)countMb.reserveTruncate((numSets*(nslaves+2)) * sizeof(rowcount_t));
        rowcount_t *totals = counts + nslaves*numSets;
        rowcount_t *tallies = totals + numSets;
        memset(counts, 0, countMb.length());

        unsigned s=nslaves;
        CMessageBuffer msg;
        while (s--)
        {
            msg.clear();
            rank_t sender;
            if (!receiveMsg(msg, RANK_ALL, mpTag, &sender))
                return;
            assertex(msg.length() == numSets*sizeof(rowcount_t));
            unsigned set = (unsigned)sender - 1;
            memcpy(&counts[set*numSets], msg.toByteArray(), numSets*sizeof(rowcount_t));
        }
        for (s=0; s<nslaves; s++)
        {
            unsigned i=0;
            for (; i<numSets; i++)
                totals[i] += counts[s * numSets + i];
        }
        msg.clear();
        msg.append(numSets*sizeof(rowcount_t), totals);
        unsigned endTotalsPos = msg.length();
        for (s=0; s<nslaves; s++)
        {
            msg.rewrite(endTotalsPos);
            msg.append(numSets*sizeof(rowcount_t), tallies);
            container.queryJob().queryJobComm().send(msg, s+1, mpTag);
            unsigned i=0;
            for (; i<numSets; i++)
                tallies[i] += counts[s * numSets + i];
        }
    }
예제 #26
0
    void process()
    {
        CWorkUnitWriteMasterBase::process();

        unsigned nslaves = container.queryJob().querySlaves();

        CMessageBuffer mb;
        unsigned s=0;
        for (; s<nslaves; s++)
        {
            loop
            {
                if (!container.queryJob().queryJobComm().send(mb, s+1, mpTag)) return;
                if (!receiveMsg(mb, s+1, mpTag)) return;
                if (0 == mb.length())
                    break;
                unsigned numGot;
                mb.read(numGot);
                unsigned l=mb.remaining();
                if (workunitWriteLimit && totalSize+resultData.length()+l > workunitWriteLimit)
                {
                    StringBuffer errMsg("Dataset too large to output to workunit (limit is set to ");
                    errMsg.append(workunitWriteLimit/0x100000).append(") megabytes, in result (");
                    if (resultName.length())
                        errMsg.append("name=").append(resultName);
                    else
                        errMsg.append("sequence=").append(resultSeq);
                    errMsg.append(")");
                    throw MakeThorException(TE_WorkUnitWriteLimitExceeded, "%s", errMsg.str());
                }
                resultData.append(l, mb.readDirect(l));
                mb.clear();
                numResults += numGot;

                if (-1 != flushThreshold && resultData.length() >= (unsigned)flushThreshold)
                    flushResults();
            }
        }
        flushResults(true);
    }
    virtual void process()
    {
        CMasterActivity::process();

        IHThorDistributionArg * helper = (IHThorDistributionArg *)queryHelper();
        IOutputMetaData *rcSz = helper->queryInternalRecordSize();

        unsigned nslaves = container.queryJob().querySlaves();

        IDistributionTable * * result = (IDistributionTable * *)createThorRow(rcSz->getMinRecordSize()); // not a real row
        helper->clearAggregate(result);


        while (nslaves--)
        {
            rank_t sender;
            CMessageBuffer msg;
            if (!receiveMsg(msg, RANK_ALL, mpTag, &sender))
                return;
#if THOR_TRACE_LEVEL >= 5
            ActPrintLog("Received distribution result from node %d", (unsigned)sender);
#endif
            if (msg.length())
                helper->merge(result, msg);
        }

        StringBuffer tmp;
        tmp.append("<XML>");
        helper->gatherResult(result, tmp);
        tmp.append("</XML>");

#if THOR_TRACE_LEVEL >= 5
        ActPrintLog("Distribution result: %s", tmp.str());
#endif

        helper->sendResult(tmp.length(), tmp.str());

        destroyThorRow(result);
    }
예제 #28
0
 rowcount_t aggregateToLimit()
 {
     rowcount_t total = 0;
     ICommunicator &comm = queryJobChannel().queryJobComm();
     unsigned slaves = container.queryJob().querySlaves();
     unsigned s;
     for (s=0; s<slaves; s++)
     {
         CMessageBuffer msg;
         rank_t sender;
         if (!receiveMsg(msg, RANK_ALL, mpTag, &sender))
             return 0;
         if (abortSoon)
             return 0;
         rowcount_t count;
         msg.read(count);
         total += count;
         if (total > limit)
             break;
     }
     return total;
 }
예제 #29
0
    void process()
    {
        unsigned slaves = container.queryJob().querySlaves();
        IHThorLimitArg *helper = (IHThorLimitArg *)queryHelper();

        rowcount_t rowLimit = (rowcount_t)helper->getRowLimit();
        rowcount_t total = 0;
        while (slaves--)
        {
            CMessageBuffer mb;
            if (!receiveMsg(mb, RANK_ALL, mpTag, NULL))
                return;
            if (abortSoon)
                return;
            rowcount_t count;
            mb.read(count);
            total += count;
            if (total > rowLimit)
                break;
        }
        switch (container.getKind())
        {
            case TAKcreaterowlimit: 
            case TAKskiplimit: 
            {
                unsigned slaves = container.queryJob().querySlaves();
                CMessageBuffer mb;
                mb.append(total);
                queryJobChannel().queryJobComm().send(mb, RANK_ALL_OTHER, mpTag);
                break;
            }
            case TAKlimit:
            {
                if (total > rowLimit)
                    helper->onLimitExceeded();
                break;
            }
        }
    }
예제 #30
0
    virtual void process()
    {
        CMasterActivity::process();

        bool results = false;
        unsigned nslaves = container.queryJob().querySlaves();
        while (nslaves--)
        {
            CMessageBuffer mb;
            if (abortSoon || !receiveMsg(mb, RANK_ALL, replyTag, NULL)) break;
            StringBuffer str;
            mb.getSender().getUrlStr(str);
            size32_t sz;
            mb.read(sz);
            if (sz)
            {
                if (results)
                    throw MakeThorException(TE_UnexpectedMultipleSlaveResults, "Received greater than one result from slaves");
                IHThorRemoteResultArg *helper = (IHThorRemoteResultArg *)queryHelper();
                Owned<IThorRowInterfaces> resultRowIf = createRowInterfaces(helper->queryOutputMeta());
                CThorStreamDeserializerSource mds(sz, mb.readDirect(sz));
                RtlDynamicRowBuilder rowBuilder(resultRowIf->queryRowAllocator());
                size32_t sz = resultRowIf->queryRowDeserializer()->deserialize(rowBuilder, mds);
                OwnedConstThorRow result = rowBuilder.finalizeRowClear(sz);
                helper->sendResult(result);
                results = true;
            }
        }
        if (!results && !abortSoon)
        {
            ActPrintLog("WARNING: no results");
            IHThorRemoteResultArg *helper = (IHThorRemoteResultArg *)queryHelper();
            //helper->sendResult(NULL);
            // Jake I think this always cores (so raise exception instead)
            throw MakeThorException(TE_UnexpectedMultipleSlaveResults, "Received no results from slaves");

        }
    }