<commit_before>/*
 * Swift Parallel Scripting Language (http://swift-lang.org)
 *
 * Copyright 2012-2014 University of Chicago
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*
 * coaster-channel.cpp
 *
 *  Created on: Jun 9, 2012
 *      Author: mike
 */

#include "CoasterChannel.h"
#include "CoasterError.h"
#include "CmdCBCV.h"
#include "HeartBeatCommand.h"
#include "ShutdownCommand.h"
#include <cassert>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "Logger.h"

#include <algorithm>
#include <cassert>
#include <sstream>

using namespace Coaster;

using std::exception;
using std::list;
using std::map;
using std::min;
using std::string;

static int socksend(int fd, const char* buf, int len, int flags);
static void pp(char* dest, const char* src, int len);

class HeartBeatCommand;

DataChunk::DataChunk() {
	bufpos = 0;
}

DataChunk::DataChunk(Buffer* pbuf, ChannelCallback* pcb) {
	buf = pbuf;
	cb = pcb;
	bufpos = 0;
}

void DataChunk::reset() {
	bufpos = 0;
}

void HeartbeatCallback::errorReceived(Command* cmd, string* message, RemoteCoasterException* details) {
	delete cmd;
	LogWarn << "Heartbeat failed: " << message << endl;
}

void HeartbeatCallback::replyReceived(Command* cmd) {
	delete cmd;
}

CoasterChannel::CoasterChannel(CoasterClient* client, CoasterLoop* loop,
			       HandlerFactory* handlerFactory) :
			       rhdr_buf(HEADER_LENGTH),
			       rhdr(&rhdr_buf, (ChannelCallback*)NULL) {
	assert(loop != NULL);
	assert(client != NULL);
	sockFD = 0;
	this->handlerFactory = handlerFactory;
	tagSeq = rand() % 65536;
	this->loop = loop;
	this->client = client;

	readState = READ_STATE_HDR;
	msg.buf = NULL;
}

CoasterChannel::~CoasterChannel() {
	for (map<int, Handler*>::iterator it = handlers.begin();
	     it != handlers.end(); ++it) {
		delete it->second;
	}

}

void CoasterChannel::start() {
	if (sockFD == 0) {
		throw CoasterError("channel start() called but no socket set");
	}
}

void CoasterChannel::shutdown() {
	ShutdownCommand* cmd = new ShutdownCommand();
	CmdCBCV callback;

	cmd->send(this, &callback);

	callback.wait();
}

int CoasterChannel::getSockFD() {
	return sockFD;
}

void CoasterChannel::setSockFD(int psockFD) {
	sockFD = psockFD;
}

void CoasterChannel::read() {
	switch(readState) {
		case READ_STATE_HDR:
			if (read(&rhdr)) {
				// full header read
				decodeHeader(&rtag, &rflags, &rlen);
				// setup message buffer for rest of message
				msg.reset();
				// Buffer should be cleared
				assert(msg.buf == NULL);
				msg.buf = new DynamicBuffer(rlen);
				readState = READ_STATE_DATA;
			}
			else {
				break;
			}
			/* no break */
		case READ_STATE_DATA:<commit_msg>Avoid leaking command<commit_after>/*
 * Swift Parallel Scripting Language (http://swift-lang.org)
 *
 * Copyright 2012-2014 University of Chicago
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*
 * coaster-channel.cpp
 *
 *  Created on: Jun 9, 2012
 *      Author: mike
 */

#include "CoasterChannel.h"
#include "CoasterError.h"
#include "HeartBeatCommand.h"
#include "ShutdownCommand.h"
#include <cassert>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "Logger.h"

#include <algorithm>
#include <cassert>
#include <sstream>

using namespace Coaster;

using std::exception;
using std::list;
using std::map;
using std::min;
using std::string;

static int socksend(int fd, const char* buf, int len, int flags);
static void pp(char* dest, const char* src, int len);

class HeartBeatCommand;

DataChunk::DataChunk() {
	bufpos = 0;
}

DataChunk::DataChunk(Buffer* pbuf, ChannelCallback* pcb) {
	buf = pbuf;
	cb = pcb;
	bufpos = 0;
}

void DataChunk::reset() {
	bufpos = 0;
}

void HeartbeatCallback::errorReceived(Command* cmd, string* message, RemoteCoasterException* details) {
	delete cmd;
	LogWarn << "Heartbeat failed: " << message << endl;
}

void HeartbeatCallback::replyReceived(Command* cmd) {
	delete cmd;
}

CoasterChannel::CoasterChannel(CoasterClient* client, CoasterLoop* loop,
			       HandlerFactory* handlerFactory) :
			       rhdr_buf(HEADER_LENGTH),
			       rhdr(&rhdr_buf, (ChannelCallback*)NULL) {
	assert(loop != NULL);
	assert(client != NULL);
	sockFD = 0;
	this->handlerFactory = handlerFactory;
	tagSeq = rand() % 65536;
	this->loop = loop;
	this->client = client;

	readState = READ_STATE_HDR;
	msg.buf = NULL;
}

CoasterChannel::~CoasterChannel() {
	for (map<int, Handler*>::iterator it = handlers.begin();
	     it != handlers.end(); ++it) {
		delete it->second;
	}

}

void CoasterChannel::start() {
	if (sockFD == 0) {
		throw CoasterError("channel start() called but no socket set");
	}
}

void CoasterChannel::shutdown() {
	ShutdownCommand cmd;
        cmd.execute(this);
}

int CoasterChannel::getSockFD() {
	return sockFD;
}

void CoasterChannel::setSockFD(int psockFD) {
	sockFD = psockFD;
}

void CoasterChannel::read() {
	switch(readState) {
		case READ_STATE_HDR:
			if (read(&rhdr)) {
				// full header read
				decodeHeader(&rtag, &rflags, &rlen);
				// setup message buffer for rest of message
				msg.reset();
				// Buffer should be cleared
				assert(msg.buf == NULL);
				msg.buf = new DynamicBuffer(rlen);
				readState = READ_STATE_DATA;
			}
			else {
				break;
			}
			/* no break */
		case READ_STATE_DATA:
{'fraction_non_alphanumeric': 0.12878906863515, 'fraction_numerical': 0.008481231349144023, 'mean_word_length': 4.0984787830264215, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6035523', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2069, 'n_words': 609}