Exemplo n.º 1
0
std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
    const CommunicateFlags& flags,
    IOBufQueue data) {
  std::pair<IOBufQueue, IOBufQueue> out;

  auto readCallback = [&] (int pfd, int cfd) -> bool {
    if (cfd == 1 && flags.readStdout_) {
      return handleRead(pfd, out.first);
    } else if (cfd == 2 && flags.readStderr_) {
      return handleRead(pfd, out.second);
    } else {
      // Don't close the file descriptor, the child might not like SIGPIPE,
      // just read and throw the data away.
      return discardRead(pfd);
    }
  };

  auto writeCallback = [&] (int pfd, int cfd) -> bool {
    if (cfd == 0 && flags.writeStdin_) {
      return handleWrite(pfd, data);
    } else {
      // If we don't want to write to this fd, just close it.
      return false;
    }
  };

  communicate(std::move(readCallback), std::move(writeCallback));

  return out;
}
Exemplo n.º 2
0
void run (int numBlocks) {
  char buf [8];
  for (int blockno = 0; blockno < numBlocks; blockno++) {
    syncRead   (buf, sizeof (buf), blockno);
    handleRead (buf, sizeof (buf), blockno);
  }
}
Exemplo n.º 3
0
    void Connection::onHandler(IOLoop* loop, int events)
    {
        //to prevent ref decr to 0
        ConnectionPtr_t conn = shared_from_this();
        
        if(events & TNET_READ)
        {
            handleRead();    
        }    
        
        if(events & TNET_WRITE)
        {
            if(m_status == Connecting)
            {
                handleConnect();   
            }
            else
            {
                handleWrite();    
            }
        }

        if(events & TNET_ERROR)
        {
            handleError();    
        }
    }
Exemplo n.º 4
0
bool TCPServer::handleIO(
	PSocketContext pSocketContext,
	PIOContext pIOContext,
	BOOL status) 
{
	if(!status) {  
		if(!handleError(pSocketContext)) {
			return false;
		}

		return true;
	}

	if( (0 == pIOContext->overlapped.InternalHigh) && 
		(pIOContext->opType & (OPT_READ + OPT_WRITE))) {  
		handleClose(pSocketContext);
		return true;
	}

	switch(pIOContext->opType) {
		case OPT_ACCEPT: handleAccept(pSocketContext, pIOContext); break;
		case OPT_READ: handleRead(pSocketContext, pIOContext); break;
		case OPT_WRITE: handleWrite(pSocketContext, pIOContext); break;
		default: LOG("操作类型参数异常"); break;
	}

	return true;
}
Exemplo n.º 5
0
void Channel::close() {
    if (fd_>=0) {
        CHEN_LOG(DEBUG,"close channel %ld fd %d", (long)id_, fd_);
        poller_->removeChannel(this);
        ::close(fd_);
        fd_ = -1;
        handleRead();       //清理连接资源
    }
}
static void _as_activateAs(AS* h, int sd, uint32_t events) {
	struct partner_s *partner;
	int lsd = sd;
	partner = g_hash_table_lookup(h->hashmap, &lsd);

	if (partner == NULL && !IS_NEW_CONNECTION(sd)) {
		h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__,
				"Null pointer in lookup of %d (partner).", sd);
		_exit(EXIT_FAILURE);
	}

	h->slogf(SHADOW_LOG_LEVEL_MESSAGE, __FUNCTION__,
			"In the activate %d.", sd);

	if(events & EPOLLIN) {
		int res = 0;
		h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__,
				"EPOLLIN is set %d", sd);
		if (IS_NEW_CONNECTION(sd))
			handleNewConnection(h, sd);
		else
			res = handleRead(h, sd, partner);

		if (res > 0) {
			/* close the connection, some error (like EPIPE)
			 * occurred in the read. */
			closeConnections(h, sd, partner);
		} else if (res < 0) {
			/* Fatal error occurred. */
			_exit(EXIT_FAILURE);
		}
	}

	if(events & EPOLLOUT) {
		struct partner_s *me = g_hash_table_lookup(h->hashmap, &partner->sd);

		if (me == NULL) {
			if (h->isDone == 1){
				/* If it reaches this point, it means that the other 
			   		end point closed its socket descriptior (connection closed).
			   		Thus this socked descriptor must be closed too. */
				closeConnections(h, sd, partner);
			}else{
				h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__,
					"Null pointer in lookup of %d (me) (connection closed).", sd);
				_exit(EXIT_FAILURE);
			}
		}else{

			h->slogf(SHADOW_LOG_LEVEL_DEBUG, __FUNCTION__,
				"EPOLLOUT is set %d", sd);
			handleWrite(h, sd, partner->sd, me);
		}
	}

}
Exemplo n.º 7
0
void CSerialPortReader::openSerial()
{
    if (!mSerial.open(QIODevice::ReadOnly)) {
        LOG_WARN << "fail open port" << mConfig.mSerialPort << mSerial.error() << mSerial.errorString();
    }
    else
    {
        connect(&mSerial, SIGNAL(readyRead()), this, SLOT(handleRead()));
        connect(&mSerial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
                this, &CSerialPortReader::handleError);
    }
}
Exemplo n.º 8
0
    SimpleHttpResult* SimpleHttpClient::request (int method,
            const string& location,
            const char* body,
            size_t bodyLength,
            const map<string, string>& headerFields) {
      
      _result = new SimpleHttpResult;
      _errorMessage = "";

      // set body to all connections
      setRequest(method, location, body, bodyLength, headerFields);

      double endTime = now() + _requestTimeout;
      double remainingTime = _requestTimeout;

      while (isWorking() && remainingTime > 0.0) {
        switch (_state) {
          case (IN_CONNECT):
            handleConnect();
            break;

          case (IN_WRITE):
            handleWrite(remainingTime);
            break;

          case (IN_READ_HEADER):
          case (IN_READ_BODY):
          case (IN_READ_CHUNKED_HEADER):
          case (IN_READ_CHUNKED_BODY):
            handleRead(remainingTime);
            break;
          default:
            break;
        }

        remainingTime = endTime - now();
      }
      
      if (isWorking() && _errorMessage=="" ) {
        if (_warn) {
          LOGGER_WARNING << "Request timeout reached.";
        }
        _errorMessage = "Request timeout reached.";
      }
      
      // set result type in getResult()
      SimpleHttpResult* result = getResult();
      
      _result = 0;
      
      return result;
    }
Exemplo n.º 9
0
std::pair<IOBufQueue, IOBufQueue> Subprocess::communicateIOBuf(
    IOBufQueue input) {
  // If the user supplied a non-empty input buffer, make sure
  // that stdin is a pipe so we can write the data.
  if (!input.empty()) {
    // findByChildFd() will throw std::invalid_argument if no pipe for
    // STDIN_FILENO exists
    findByChildFd(STDIN_FILENO);
  }

  std::pair<IOBufQueue, IOBufQueue> out;

  auto readCallback = [&] (int pfd, int cfd) -> bool {
    if (cfd == STDOUT_FILENO) {
      return handleRead(pfd, out.first);
    } else if (cfd == STDERR_FILENO) {
      return handleRead(pfd, out.second);
    } else {
      // Don't close the file descriptor, the child might not like SIGPIPE,
      // just read and throw the data away.
      return discardRead(pfd);
    }
  };

  auto writeCallback = [&] (int pfd, int cfd) -> bool {
    if (cfd == STDIN_FILENO) {
      return handleWrite(pfd, input);
    } else {
      // If we don't want to write to this fd, just close it.
      return true;
    }
  };

  communicate(std::move(readCallback), std::move(writeCallback));

  return out;
}
Exemplo n.º 10
0
void NetMessageSocket::io(ev::io&, int revents)
{
	debug("I/O event received: 0x%04x", revents);
	socketTimer_.stop();

	if (revents & ev::READ)
		if (!handleRead())
			return;

	if (revents & ev::WRITE)
		if (!handleWrite())
			return;

	socketTimer_.start(30, 0);
}
Exemplo n.º 11
0
void GtSessionPrivate::init(QAbstractSocket *s, GtServer *m, GtServerThread *t)
{
    Q_Q(GtSession);

    m_socket = s;
    m_server = m;
    m_thread = t;
    m_peerAddress = s->peerAddress();
    m_peerPort = s->peerPort();
    m_peerName = m_peerAddress.toString() + ":" + QString::number(m_peerPort);

    s->connect(s, SIGNAL(readyRead()), q, SLOT(handleRead()));
    s->connect(s, SIGNAL(error(QAbstractSocket::SocketError)),
               q, SLOT(handleError(QAbstractSocket::SocketError)));
}
Exemplo n.º 12
0
bool parseI2CMessage(int size, const byte *msg, CONTEXT *context)
    {
    switch (msg[0] ) 
        {
        case I2C_CMD_CONFIG:
            return handleConfig(size, msg, context);
            break;
        case I2C_CMD_READ:
            return handleRead(size, msg, context);
            break;
        case I2C_CMD_WRITE:
            return handleWrite(size, msg, context);
            break;
        }
    return false;
    }
Exemplo n.º 13
0
void AbstractService::loop() {
	static char buf[65535];
	struct pollfd fds[1];
	fds[0].fd = m_sock;
	fds[0].events = POLLIN;

	while (1) {
		int ret = poll(fds, 1, -1);
		if (ret == 1) {
			int len = read(m_sock, &buf, 65535);
			if (len > 0) {
				std::string d(buf, len);
				handleRead(d, len);
			}
		}
	}
}
Exemplo n.º 14
0
void* readAndHandleBlock (void* args_voidstar) {
  // TODO
  // Synchronously:
  //   (1) call disk_scheduleRead to request the block
  //   (2) wait read for it to complete
  //   (3) call handleRead
  struct readAndHandleBlockArgs* obj = args_voidstar;
  disk_scheduleRead(obj->buf,obj->nbytes,obj->blockno); //call disk_scheduleRead to request the block 
  blockUntilComplete();
  handleRead(obj->buf,obj->nbytes,obj->blockno); 
  free(obj->buf);
  free(obj);  //how do we get the information after the reading 
  return NULL;
  // read first, because it takes time to read so we block it first. And then 
  // when it finishes reading, we unblock it and put it into a queue. We call interrupt service 
  // routine to handle the data
  
  }
Exemplo n.º 15
0
void IOServiceLibEvent::doRead(ConnectorChannelContext* ctx)
{
	std::list<mxcore::SharedPtr<mxcore::ByteBuffer> > readMsgList;

	try
	{
		int r = ctx->readMessage(readMsgList);
		if (r > 0)
		{
			handleRead(ctx, readMsgList);
		}
		else
		{
			handleReadError(ctx, readMsgList.front(),
					mxasio::IOApplication::ERR_EOF);
		}
	} catch (mxcore::IOException& e)
	{
		handleReadError(ctx, readMsgList.front(), e.getErrorCode());
	}
}
Exemplo n.º 16
0
SessionManager::SessionManager(QWidget *parent) :
    QDialog(parent)
{
    needBind = false;
    needSess = false;
    firstTimeShowMainWindow = true;
    xmlTree = new QTreeWidget;
    xmlHandler = new SaxHandler(xmlTree);
    tcpSocket = new QTcpSocket();

    connect(tcpSocket, SIGNAL(readyRead()),
            this, SLOT(handleRead()));
    connect(tcpSocket, SIGNAL(disconnected()),
            this, SLOT(connectionClosedByServer()));
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(socketError()));
    connect(this, SIGNAL(send(QString)),
            this, SLOT(sendRequest(QString)));

    sessionId = random()%50;
}
Exemplo n.º 17
0
void MailServer::messageReceived(int sd, boost::shared_ptr<Message> msg)
{
	DEBUG("A message has been dispatched!");

	// Don't accept messages from users that aren't authenticated yet.
	if( msg->getType() != Message::MessageTypeLogin &&
		msg->getType() != Message::MessageTypeQuit  &&
		loginManager_.isLoggedIn() == false)
	{
		sendErr(sd);
		return;
	}

	switch( msg->getType() )
	{
		case Message::MessageTypeSend:
			handleSend(static_cast<SendMessage&>(*msg));
			break;
		case Message::MessageTypeList:
			handleList(static_cast<ListMessage&>(*msg));
			break;
		case Message::MessageTypeRead:
			handleRead(static_cast<ReadMessage&>(*msg));
			break;
		case Message::MessageTypeDel:
			handleDel(static_cast<DelMessage&>(*msg));
			break;
		case Message::MessageTypeQuit:
			handleQuit(static_cast<QuitMessage&>(*msg));
			break;
		case Message::MessageTypeLogin:
			handleLogin(static_cast<LoginMessage&>(*msg));
			break;
		case Message::MessageTypeInvalid:
		default:
			DEBUG("Ignoring: An invalid/unknown message type was received. Code: [" << (*msg).getType() << "].");
			break;
	}
}
Exemplo n.º 18
0
void AsyncUDPSocket::handlerReady(uint16_t events) noexcept {
  if (events & EventHandler::READ) {
    DCHECK(readCallback_);
    handleRead();
  }
}
Exemplo n.º 19
0
void run_work(struct work_param* param) {
	if (pipe(param->pipes) != 0) {
		printf("Failed to create pipe! %s\n", strerror(errno));
		return;
	}
	unsigned char wb;
	unsigned char* mbuf = xmalloc(1024);
	while (1) {
		pthread_rwlock_rdlock(&param->conns->data_mutex);
		size_t cc = param->conns->count;
		struct pollfd fds[cc + 1];
		struct conn* conns[cc];
		int fdi = 0;
		for (int i = 0; i < param->conns->size; i++) {
			if (param->conns->data[i] != NULL) {
				conns[fdi] = (param->conns->data[i]);
				struct conn* conn = conns[fdi];
				fds[fdi].fd = conns[fdi]->fd;
				fds[fdi].events = POLLIN | (conn->writeBuffer_size > 0 ? POLLOUT : 0);
				fds[fdi++].revents = 0;
				if (fdi == cc) break;
			} else conns[fdi] = NULL;
		}
		pthread_rwlock_unlock(&param->conns->data_mutex);
		fds[cc].fd = param->pipes[0];
		fds[cc].events = POLLIN;
		fds[cc].revents = 0;
		int cp = poll(fds, cc + 1, -1);
		if (param->mysql->mysql && param->mysql->complete && param->zone != param->mysql->czone) {
			if (param->zone != NULL) {
				freeZone(param->zone);
			}
			param->zone = param->mysql->czone;
		}
		if (cp < 0) {
			printf("Poll error in worker thread! %s\n", strerror(errno));
		} else if (cp == 0) continue;
		else if ((fds[cc].revents & POLLIN) == POLLIN) {
			if (read(param->pipes[0], &wb, 1) < 1) printf("Error reading from pipe, infinite loop COULD happen here.\n");
			if (cp-- == 1) continue;
		}
		for (int i = 0; i < cc; i++) {
			int re = fds[i].revents;
			struct conn* conn = conns[i];
			if (conn == NULL) continue;
			if ((re & POLLERR) == POLLERR) {
				//printf("POLLERR in worker poll! This is bad!\n");
				goto cont;
			}
			if ((re & POLLHUP) == POLLHUP) {
				closeConn(param, conn);
				conn = NULL;
				goto cont;
			}
			if ((re & POLLNVAL) == POLLNVAL) {
				printf("Invalid FD in worker poll! This is bad!\n");
				closeConn(param, conn);
				conn = NULL;
				goto cont;
			}
			if ((re & POLLIN) == POLLIN) {
				size_t tr = 0;
				ioctl(fds[i].fd, FIONREAD, &tr);
				unsigned char* loc;
				if (conn->readBuffer == NULL) {
					conn->readBuffer = xmalloc(tr); // TODO: max upload?
					conn->readBuffer_size = tr;
					loc = conn->readBuffer;
				} else {
					conn->readBuffer_size += tr;
					conn->readBuffer = xrealloc(conn->readBuffer, conn->readBuffer_size);
					loc = conn->readBuffer + conn->readBuffer_size - tr;
				}
				ssize_t r = 0;
				if (r == 0 && tr == 0) { // nothing to read, but wont block.
					ssize_t x = 0;
					x = read(fds[i].fd, loc + r, tr - r);
					if (x <= 0) {
						closeConn(param, conn);
						conn = NULL;
						goto cont;
					}
					r += x;
				}
				while (r < tr) {
					ssize_t x = 0;
					x = read(fds[i].fd, loc + r, tr - r);
					if (x <= 0) {
						closeConn(param, conn);
						conn = NULL;
						goto cont;
					}
					r += x;
				}
				int p = 0;
				p = handleRead(conn, param, fds[i].fd);
				if (p == 1) {
					goto cont;
				}
			}
			if ((re & POLLOUT) == POLLOUT && conn != NULL) {
				ssize_t mtr = write(fds[i].fd, conn->writeBuffer, conn->writeBuffer_size);
				if (mtr < 0 && errno != EAGAIN) {
					closeConn(param, conn);
					conn = NULL;
					goto cont;
				} else if (mtr < 0) {
					goto cont;
				} else if (mtr < conn->writeBuffer_size) {
					memmove(conn->writeBuffer, conn->writeBuffer + mtr, conn->writeBuffer_size - mtr);
					conn->writeBuffer_size -= mtr;
					conn->writeBuffer = xrealloc(conn->writeBuffer, conn->writeBuffer_size);
				} else {
					conn->writeBuffer_size = 0;
					xfree(conn->writeBuffer);
					conn->writeBuffer = NULL;
				}
				if (conn->writeBuffer_size == 0 && conn->state == 1) {
					closeConn(param, conn);
					conn = NULL;
					goto cont;
				}
			}
			cont: ;
			if (--cp == 0) break;
		}
	}
	xfree(mbuf);
}
Exemplo n.º 20
0
static void selectPass(void) {

	int const fdSetCount = maxfd / FD_SETSIZE + 1;
#	define FD_ZERO_EXT(ar) for (int i = 0; i < fdSetCount; ++i) { FD_ZERO(&(ar)[i]); }
#	define FD_SET_EXT(fd, ar) FD_SET((fd) % FD_SETSIZE, &(ar)[(fd) / FD_SETSIZE])
#	define FD_ISSET_EXT(fd, ar) FD_ISSET((fd) % FD_SETSIZE, &(ar)[(fd) / FD_SETSIZE])

	fd_set readfds[fdSetCount], writefds[fdSetCount];
	FD_ZERO_EXT(readfds);
	FD_ZERO_EXT(writefds);
	/* Server sockets */
	for (int i = 0; i < seTotal; ++i) {
		if (seInfo[i].fd != INVALID_SOCKET) {
			FD_SET_EXT(seInfo[i].fd, readfds);
		}
	}
	/* Connection sockets */
	for (int i = 0; i < coTotal; ++i) {
		ConnectionInfo *cnx = &coInfo[i];
		if (cnx->local.fd != INVALID_SOCKET) {
			/* Accept more output from the local
				server if there's room */
			if (cnx->local.recvPos < RINETD_BUFFER_SIZE) {
				FD_SET_EXT(cnx->local.fd, readfds);
			}
			/* Send more input to the local server
				if we have any, or if we’re closing */
			if (cnx->local.sentPos < cnx->remote.recvPos || cnx->coClosing) {
				FD_SET_EXT(cnx->local.fd, writefds);
			}
		}
		if (cnx->remote.fd != INVALID_SOCKET) {
			/* Get more input if we have room for it */
			if (cnx->remote.recvPos < RINETD_BUFFER_SIZE) {
				FD_SET_EXT(cnx->remote.fd, readfds);
			}
			/* Send more output if we have any, or if we’re closing */
			if (cnx->remote.sentPos < cnx->local.recvPos || cnx->coClosing) {
				FD_SET_EXT(cnx->remote.fd, writefds);
			}
		}
	}
	select(maxfd + 1, readfds, writefds, 0, 0);
	for (int i = 0; i < coTotal; ++i) {
		ConnectionInfo *cnx = &coInfo[i];
		if (cnx->remote.fd != INVALID_SOCKET) {
			if (FD_ISSET_EXT(cnx->remote.fd, readfds)) {
				handleRead(cnx, &cnx->remote, &cnx->local);
			}
		}
		if (cnx->remote.fd != INVALID_SOCKET) {
			if (FD_ISSET_EXT(cnx->remote.fd, writefds)) {
				handleWrite(cnx, &cnx->remote, &cnx->local);
			}
		}
		if (cnx->local.fd != INVALID_SOCKET) {
			if (FD_ISSET_EXT(cnx->local.fd, readfds)) {
				handleRead(cnx, &cnx->local, &cnx->remote);
			}
		}
		if (cnx->local.fd != INVALID_SOCKET) {
			if (FD_ISSET_EXT(cnx->local.fd, writefds)) {
				handleWrite(cnx, &cnx->local, &cnx->remote);
			}
		}
	}
	/* Handle servers last because handleAccept() may modify coTotal */
	for (int i = 0; i < seTotal; ++i) {
		ServerInfo *srv = &seInfo[i];
		if (srv->fd != INVALID_SOCKET) {
			if (FD_ISSET_EXT(srv->fd, readfds)) {
				handleAccept(srv);
			}
		}
	}
}
Exemplo n.º 21
0
void sendResponse(char *allTokens[], char response[MAX_RESPONSE_SIZE], struct connection_bundle *connBundle, char request[MAX_REQUEST_SIZE]){
	//Selects a passed in statement and appropriately assigns a return message
	//General mechanism for message passing between server and client
	int clientSock = connBundle->clientSock;
	int isPrimary = IS_PRIMARY;
	
	/*2. Coordination: The primary takes each request atomically, in the order in which it
	receives it. It checks the unique identifier, in case it has already executed the request,
	and if so it simply resends the response.*/	
	//If request is an UPDATE
	if((strncmp(allTokens[0], "REGISTER", strlen(allTokens[0])) == 0) ||
		(strncmp(allTokens[0], "ADD_TWEET", strlen(allTokens[0])) == 0) ||
		(strncmp(allTokens[0], "ADD_FOLLOWEE", strlen(allTokens[0])) == 0) || 
		(strncmp(allTokens[0], "REMOVE_FOLLOWEE", strlen(allTokens[0])) == 0) ||
		(strncmp(allTokens[0], "DELETE_ACCOUNT", strlen(allTokens[0])) == 0)){
			//check log
			int found = checkLog(request); //stores only updates
			if(found){
				if(snprintf(response, MAX_RESPONSE_SIZE, "%i\n", found) < 0)
				{
					perror("Snprintf failed.");
					exit(SNPRINTF_FAILED);
				}
				//Make sure the back-ups have updated logs
				if(isPrimary)
				{
					notifyBackups(request);
				}
				if(send(clientSock, response, MAX_RESPONSE_SIZE, 0) == -1)
				{
					perror("Send Failed.");
				}
			}
			else
			{
				//3. Execution: The primary executes the request and stores the response.
				//Execute update
				
				handleUpdate(allTokens, response);
				//Log Response
				addLog(request);
				/*4. Agreement: If the request is an update, then the primary sends the updated state,
					the response and the unique identifier to all the backups. The backups send an
					acknowledgement.*/
				if(isPrimary)
				{
					notifyBackups(request);
				}
				/*5. Response: The primary responds to the front end, which hands the response back to the client.*/
				if(send(clientSock, response, MAX_RESPONSE_SIZE, 0) == -1)
				{
					perror("Send Failed.");
					//exit(SEND_FAILED);
				}
				
				
				
			}	  
		}
	else if((strncmp(allTokens[0], "LOGIN", strlen(allTokens[0])) == 0) ||
			(strncmp(allTokens[0], "LIST_FOLLOWEES", strlen(allTokens[0])) == 0) ||
			(strncmp(allTokens[0], "LIST_OTHERS", strlen(allTokens[0])) == 0) ||
			(strncmp(allTokens[0], "LIST_TWEETS", strlen(allTokens[0])) == 0)){
			//Execute read & respond
			handleRead(allTokens, response, clientSock);
			
	}
	else if((strncmp(allTokens[0], "GET_PRIMARY", strlen(allTokens[0])) == 0))
	{	
		int target = (int)atoi(allTokens[1]);
		printf("Target: %s", allTokens[1]);
		puts(request);
		int killed = killPort(target);
		if(killed){
			int myPort = MY_PORT;
			int otherPort1 = OTHER_PORT1;
			int otherPort2 = OTHER_PORT2;
			
			//make choice
			int candidate = selectCandidate(myPort, otherPort1, otherPort2);
			char electoralRequest[MAX_REQUEST_SIZE];
			pthread_mutex_lock(&isPrimaryMutex);
			PRIMARY = candidate;
			if(candidate == myPort)
			{
				IS_PRIMARY = 1;
			}
			pthread_mutex_unlock(&isPrimaryMutex);
			if(snprintf(electoralRequest, MAX_REQUEST_SIZE, "dd\tAPPOINT\t%i", candidate) < 0)
			{
				perror("Snprintf failed.");
				exit(SNPRINTF_FAILED);
			}
			if(otherPort1 != -1)
			{
				//Send candidate
				conductElections(electoralRequest, otherPort1);
			}
			if(otherPort2 != -1)
			{
				conductElections(electoralRequest, otherPort2);
			}
			if(snprintf(response, MAX_RESPONSE_SIZE, "%i\n", candidate) < 0)
			{
				perror("Snprintf failed.");
				exit(SNPRINTF_FAILED);
			}
		}
		else
		{
			//already dead
			int primary = PRIMARY;
			if(primary == -1)
			{
				puts("Invalid primary");
			}
			if(snprintf(response, MAX_RESPONSE_SIZE, "%i\n", primary) < 0)
			{
				perror("Snprintf failed.");
				exit(SNPRINTF_FAILED);
			}
		}
		printf("New leader: %s\n", response);
		if(send(clientSock, response, MAX_RESPONSE_SIZE, 0) == -1)
		{
			perror("Send Failed.");
		}
	}
	else if((strncmp(allTokens[0], "APPOINT", strlen(allTokens[0])) == 0))
	{
		int newPort = atoi(allTokens[1]);
		int myPort = MY_PORT;
		PRIMARY = newPort;
		if(myPort == PRIMARY)
		{
			pthread_mutex_lock(&isPrimaryMutex);
			IS_PRIMARY = 1;
			pthread_mutex_unlock(&isPrimaryMutex);
		}
		
	}
	else{
			exit(INVALID_SELECTION);
		}
}