/*
 * Copyright 2012 IFH-InfoCentre, Ltd.
*/

package ru.mfd.api.examples;

import ru.mfd.api.*;
import ru.mfd.api.consts.LoginError;

import java.util.Arrays;
import java.util.List;

/**
 * User: MMarkelov
 * Date: 12.10.12
 * Time: 18:34
 */
public class GetQuotes {

	private static final String host = "nidemo.mfd.ru";
	private static final int port = 2900;
	private static final String login = "";
	private static final String password = "";
	private static final int connectionTimeout = 60000;

	private SrvrSessionListener listener;
	private SrvrSession srvrSession;
	private Table table;

	private class TableListenerImpl implements TableListener {
		public void onData(String code, String tableName, List<Row> rows) {
			System.out.println("onData: " + tableName + " " + rows.size() + " rows");
		}

		public void onDataComplete(String code, String tableName) {
			System.out.println("onDataComplete: " + tableName);
			try {
				table.setOnlineUpdates(true);
			} catch (ClientException e) {
				System.err.println(e);
			}
		}

		public void onInsert(String tableName, Row row) {
			System.out.println("onInsert: " + row);
		}

		public void onPartInsert(String tableName, Row row) {
			System.out.println("onPartInsert: " + row);
		}

		public void onModify(String tableName, Row row) {
			System.out.println("onModify: " + row);
		}

		public void onDelete(String tableName, Long recid) {
			System.out.println("onDelete: " + tableName + " " + recid);
		}

		public void onClear(String tableName) {
			System.out.println("onClear: " + tableName);
		}

		public void onClose(String tableName) {
			System.out.println("onClose: " + tableName);
		}
	}

	private class Listener implements SrvrSessionListener {
		public void onLogin(int result, String description) {
			if (result != LoginError.LOGIN_SUCCESS) {
				System.err.println("onLogin: failed [" + result + "] " + description);
				return;
			}

			try {
				table = new Table(srvrSession, "QUOTES_PSFU", new TableListenerImpl());
				table.getSlice(null, null);
			} catch (ClientException e) {
				System.err.println(e);
			}
		}

		public void onConnectionLost(String description) {
			System.err.println("onConnectionLost: " + description);
			try {
				Thread.sleep(1000);
				srvrSession.connect(host, port, login, password, listener, connectionTimeout);
			} catch (Exception ex) {
				System.err.println("Failed to reconnect: " + ex);
			}
		}

		public void onStatusMessage(int i, String[] s) {
			System.out.println("onStatusMessage: " + i + " " + Arrays.toString(s));
		}
	}

	private void run() {
		try {
			Runtime.getRuntime().addShutdownHook(new Thread() {
				public void run() {
					if (srvrSession != null) {
						srvrSession.disconnect();
					}
				}
			});

			listener = new Listener();
			srvrSession = new SrvrSession();
			srvrSession.setPartInsert(true);
			srvrSession.connect(host, port, login, password, listener, connectionTimeout);
		} catch (Exception e) {
			System.err.println("Exception: " + e);
		}
	}

	public static void main(String[] args) throws Exception {
		System.out.println("GetData example");
		System.out.println("Press ENTER to quit");
		GetQuotes example = new GetQuotes();
		example.run();
		System.in.read();
	}
}
