// Tencent is pleased to support the open source community by making Mars available.
// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.

// Licensed under the MIT License (the "License"); you may not use this file except in 
// compliance with the License. You may obtain a copy of the License at
// http://opensource.org/licenses/MIT

// 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.

/*
*  stn_callback.cpp
*
*  Created on: 2017-7-7
*      Author: chenzihao
*/

#include "stn_callback.h"

#include <mars/comm/autobuffer.h>
#include <mars/comm/xlogger/xlogger.h>
#include <mars/stn/stn.h>
#include "Wrapper/NetworkService.h"

namespace mars {
    namespace stn {
        
		//using mars::stn::StnCallBack;
		StnCallBack* StnCallBack::instance_ = NULL;   
		StnCallBack* StnCallBack::Instance() {
			if(instance_ == NULL) {
					instance_ = new StnCallBack();
			}
			return instance_;
		}

		void StnCallBack::Release() {
			delete instance_;
			instance_ = NULL;
		}
        
		bool StnCallBack::MakesureAuthed() {
			return true;
		}

		void StnCallBack::TrafficData(ssize_t _send, ssize_t _recv) {
			xdebug2(TSF"send:%_, recv:%_", _send, _recv);
		}
        
		std::vector<std::string> StnCallBack::OnNewDns(const std::string& _host) {
			std::vector<std::string> vector;
			//vector.push_back("118.89.24.72");
			return vector;
		}

		void StnCallBack::OnPush(uint64_t _channel_id, uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend) {
			pNetworkService.OnPush(_channel_id, _cmdid, _taskid, _body, _extend);
    
		}

		bool StnCallBack::Req2Buf(uint32_t _taskid, void* const _user_context, AutoBuffer& _outbuffer, AutoBuffer& _extend, int& _error_code, const int _channel_select) {
	
			return pNetworkService.Req2Buf(_taskid, _user_context, _outbuffer, _extend, _error_code, _channel_select);
		}

		int StnCallBack::Buf2Resp(uint32_t _taskid, void* const _user_context, const AutoBuffer& _inbuffer, const AutoBuffer& _extend, int& _error_code, const int _channel_select) {
    
			return pNetworkService.Buf2Resp(_taskid, _user_context, _inbuffer, _extend, _error_code, _channel_select);
		}

		int StnCallBack::OnTaskEnd(uint32_t _taskid, void* const _user_context, int _error_type, int _error_code) {
			pNetworkService.OnTaskEnd(_taskid, _user_context, _error_type, _error_code);
			return 0;
		}

		void StnCallBack::ReportConnectStatus(int _status, int longlink_status) {

			switch (longlink_status) {
				case kServerFailed:
				case kServerDown:
				case kGateWayFailed:
					break;
				case kConnecting:
					break;
				case kConnected:
					break;
				case kNetworkUnkown:
					return ;
				default:
					return ;
			}
    
		}

		// synccheck：长链成功后由网络组件触发
		// 需要组件组包，发送一个req过去，网络成功会有resp，但没有taskend，处理事务时要注意网络时序
		// 不需组件组包，使用长链做一个sync，不用重试

		int  StnCallBack::GetLonglinkIdentifyCheckBuffer(AutoBuffer& _identify_buffer, AutoBuffer& _buffer_hash, int32_t& _cmdid) {
    

			return pNetworkService.GetLonglinkIdentifyCheckBuffer(_identify_buffer, _buffer_hash, _cmdid);
		}

		bool StnCallBack::OnLonglinkIdentifyResponse(const AutoBuffer& _response_buffer, const AutoBuffer& _identify_buffer_hash) {
    
			return pNetworkService.OnLonglinkIdentifyResponse(_response_buffer, _identify_buffer_hash);
		}
		//
		void StnCallBack::RequestSync() {
			pNetworkService.RequestSync();
		}
	}
}






