var app_mod = require("./app");
var appid = "app1";
var df = 1000;
if(process.argv.length >= 3){
	appid = process.argv[2];
	if(process.argv.length >=4){
		df = process.argv[3];
	}
}
//192.168.1.58 112.124.0.222 115.29.162.108 112.124.44.47
var arr = ["test.vvku.com:8365"];

console.log(appid, arr);
var app = app_mod.Create(appid,arr,"123456");
console.log(app_mod.version);
var timer;
var num_pkt = 0;
var num_cli = 0;

var client_id_map = {};
app.on('open',function(){
	console.log("app.started.at."+appid);
	app.FollowOtherApp("multimedia_service");
	app.FollowOtherApp("game_line_58_9198");
	app.FollowOtherApp("game_line_room58");
	app.FollowOtherApp("game_room_57_9198");
	app.FollowOtherApp("app2");
	app.FollowOtherApp("game_line_5_9198");
	app.FollowOtherApp("game_line_test_9198");
	app.FollowOtherApp("myappid");
})


app.on("client_in",function(id,addr,plat,buf){
	num_cli++;
	console.log("client_in",id,addr,plat);
	app.AddScope("a",id);
	client_id_map[id] = {id:-1};
})
app.on("client_data",function(cid,buf){
	var str = buf.toString();
	try{
		var obj = JSON.parse(str);
		var now = new Date().getTime();
		var item = client_id_map[id];
		if(item.id != obj.id - 1){
			console.log(cid,item.id,obj.id);
		}
		item.id = obj.id;
	}catch(err){}
	app.SendToClient(cid,buf);
	app.SendToScope("a",buf);
	num_pkt++;
})
app.on("client_out",function(id){
	num_cli--;
	console.log("client_out",id);
})
app.on("app_data",function(appid,buf){
	console.log("app_data",appid,buf.toString());
	try{
		var str = buf.toString();
		var obj = JSON.parse(str);
	}catch(err){
		console.log("josn.parse.fail ",str,buf,err,err.stack);
		return;
	}
	if(obj.cmd == "CMD_CREATE_CHANNEL_BACK"){
		if(obj.code == 0){
			console.log(obj.id);
		}
	}
})
function m(){
	var pkt = {
		cmd:"CMD_CREATE_CHANNEL",
		__session:0,
	}
	app.SendToOtherApp("multimedia_service",pkt);
}
var timer_;
app.on("app_enter",function(appid){
	console.log("app_enter",appid);
	if(appid == "multimedia_service"){
		m();
	}else if(appid == "app2"){
		timer_ = setInterval(function(){
			app.SendToOtherApp(appid,"hello");
		},1000)
	}
})
app.on("app_leave",function(appid){
	console.log("app_leave",appid);
	if(appid == "app2"){
		clearInterval(timer_);
	}
})
app.on('close',function(err){
	console.log("app.close.at."+appid+".with.err={"+err+"}");
	clearInterval(timer);
	app.Stop();
	process.exit(0);
})
setInterval(function(){
	/*if(num_pkt > 0 || num_cli){
		console.log("pkt:",num_pkt,"cli:",num_cli);
		num_pkt = 0;
	}*/
},1000)
app.Start();


process.on('SIGHUP', function() {
  log.Info('Got SIGHUP signal.');
  app.Stop();
  setTimeout(ReStart,1000);
});
process.on('SIGQUIT', function() {
  log.Info('Got SIGQUIT signal.');
  app.Stop();
  setTimeout(function(){
  	process.exit(0);
  },1000);
});
process.on('SIGINT', function() {
  console.log('Got SIGINT signal.');
  app.Stop();
  setTimeout(function(){
  	process.exit(0);
  },200);
});

function ReStart(){
	log.Info("restart");
	var childprocess = require('child_process');
	var arr = [];
	for(var i = 2;i<process.argv.length;i++){
		arr.push(process.argv[i]);
	}
	childprocess.fork(__filename,arr);
	process.exit();
}
process.on('uncaughtException', function (err) {
	console.log('Caught exception: ' ,err,err.stack);
});