Package chesstrainer.endgame

Source Code of chesstrainer.endgame.CSoapEndgameTablebase

/*
* Copyright (C) 2014 Tim Declercq <caveman1917@hotmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package chesstrainer.endgame;

import chesstrainer.CMove;
import chesstrainer.Main;
import chesstrainer.util.EPiece;
import chesstrainer.util.ESquare;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

/**
*
* @author Tim Declercq <caveman1917@hotmail.com>
*/
public class CSoapEndgameTablebase implements IEndgameTablebase {
   
    SOAPConnectionFactory soapConnectionFactory;
    SOAPConnection connection;
    MessageFactory factory;
   
    public CSoapEndgameTablebase() {
        try {
            soapConnectionFactory = SOAPConnectionFactory.newInstance();
            factory = MessageFactory.newInstance();
        } catch (SOAPException | UnsupportedOperationException e) {
            Main.LOG.log(Level.SEVERE, "Cannot create SOAP", e);
        }
       
        // remove soap loggers
        Logger log;
        LogManager logManager = LogManager.getLogManager();
        String name;
        for (Enumeration<String> names = logManager.getLoggerNames(); names.hasMoreElements();) {
            name = names.nextElement();
            if (name.contains("com.sun.xml.internal.messaging.saaj")) {
                log = logManager.getLogger(name);
                log.setUseParentHandlers(false);
                for (Handler handler : log.getHandlers()) {
                    log.removeHandler(handler);
                }
            }
        }
    }

    @Override
    public int getScore(String fen) {
       
        try {
            connection = soapConnectionFactory.createConnection();
           
            SOAPMessage message = factory.createMessage();
            SOAPHeader header = message.getSOAPHeader();
            header.detachNode();
           
            SOAPBody body = message.getSOAPBody();
            QName bodyName = new QName("http://www.lokasoft.nl/message/", "ProbePosition", "m");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
            QName name = new QName("fen");
            SOAPElement fenElement = bodyElement.addChildElement(name);
            fenElement.addTextNode(fen);
           
            URL endpoint = new URL("http://www.lokasoft.nl/tbweb/tbapi.asp");
            SOAPMessage response = connection.call(message, endpoint);
           
            connection.close();
           
            body = response.getSOAPBody();
           
            Iterator it = body.getChildElements();
            bodyElement = (SOAPBodyElement)it.next();
            it = bodyElement.getChildElements();
           
            bodyElement = (SOAPBodyElement)it.next();
            String value = bodyElement.getValue();
           
            return toInt(value);
           
        } catch(SOAPException | MalformedURLException e) {
            Main.LOG.log(Level.SEVERE, "Cannot retrieve SOAP response", e);
        }
       
        return ERROR;
    }

    @Override
    public Map<CMove, Integer> getBestMoves(String fen) {

        try {
            connection = soapConnectionFactory.createConnection();
           
            SOAPMessage message = factory.createMessage();
            SOAPHeader header = message.getSOAPHeader();
            header.detachNode();
           
            SOAPBody body = message.getSOAPBody();
            QName bodyName = new QName("http://www.lokasoft.nl/message/", "GetBestMoves", "m");
            SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
            QName name = new QName("fen");
            SOAPElement fenElement = bodyElement.addChildElement(name);
            fenElement.addTextNode(fen);
           
            URL endpoint = new URL("http://www.lokasoft.nl/tbweb/tbapi.asp");
            SOAPMessage response = connection.call(message, endpoint);
           
            connection.close();
           
            body = response.getSOAPBody();
           
            Iterator it = body.getChildElements();
            bodyElement = (SOAPBodyElement)it.next();
            it = bodyElement.getChildElements();
           
            bodyElement = (SOAPBodyElement)it.next();
            String value = bodyElement.getValue();
           
            Map<CMove, Integer> map = new HashMap<>();
            for (String line : value.split("\n")) {
                String[] pieces = line.split(" ");
                map.put(toMove(pieces[0]), toInt(pieces[1]));
            }
           
            return map;
           
        } catch(SOAPException | IOException e) {
            Main.LOG.log(Level.SEVERE, "Cannot retrieve SOAP response", e);
            return null;
        }
       
    }
   
    private int toInt(String score) {
        if (score == null) {
            return ERROR;
        }
        score = score.replaceAll("M", "");
        return Integer.parseInt(score);
    }
   
    private CMove toMove(String move) {
        String[] pieces = move.split("-");
        ESquare from = null, to = null;
       
        for (ESquare s : ESquare.values()) {
            if (pieces[0].contains(s.name())) {
                from = s;
            }
            if (pieces[1].contains(s.name())) {
                to = s;
            }
        }
        if (!move.contains("=")) {
            return new CMove(from, to);
        }
        move = move.toLowerCase();
        if (move.contains("1")) {
            if (move.contains("=q")) {
                return new CMove(from, to, EPiece.WQ);
            }
            if (move.contains("=r")) {
                return new CMove(from, to, EPiece.WR);
            }
            if (move.contains("=b")) {
                return new CMove(from, to, EPiece.WB);
            }
            if (move.contains("=n")) {
                return new CMove(from, to, EPiece.WN);
            }
        } else {
            if (move.contains("=q")) {
                return new CMove(from, to, EPiece.BQ);
            }
            if (move.contains("=r")) {
                return new CMove(from, to, EPiece.BR);
            }
            if (move.contains("=b")) {
                return new CMove(from, to, EPiece.BB);
            }
            if (move.contains("=n")) {
                return new CMove(from, to, EPiece.BN);
            }
        }
       
        return null;
    }
   
}
TOP

Related Classes of chesstrainer.endgame.CSoapEndgameTablebase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.