package com.mercury.chat.client.protocol.impl;

import static com.mercury.chat.common.MessageType.LOGIN;
import io.netty.channel.Channel;

import com.mercury.chat.client.json.MessageBox;
import com.mercury.chat.client.protocol.Connection;
import com.mercury.chat.client.protocol.LoginAuthHandler;
import com.mercury.chat.client.protocol.Session;
import com.mercury.chat.common.struct.protocol.Header;
import com.mercury.chat.common.struct.protocol.Message;
import com.mercury.chat.user.User;

public class ConnectionImpl implements Connection{

	private Channel channel;
	
	public ConnectionImpl() {
		super();
	}
	
	public ConnectionImpl channel(Channel channel){
		this.channel = channel;
		return this;
	}
	
	@Override
	public Session login(String userId, String password) {
		try {
			Message message = new Message().header(new Header().type(LOGIN.value())).body(new User(userId,password));
			channel.writeAndFlush(message).sync();//send login request to chat service
			MessageBox loginMessageBox = channel.pipeline().get(LoginAuthHandler.class).getLoginMessageBox();
			loginMessageBox.get();
		} catch (InterruptedException e) {
			return null;
		}
		return new SessionImpl(channel);
	}
	
}

@@ -1124,16 +1124,56 @@
 x.get();
+//wait until receive the login response.
 %0D%0A%09%09%7D ca

add some comments.