view topology/HostMessage.cs @ 68:976d43003487

fix TopologyManager
author KaitoMaeshiro <aosskaito@cr.ie.u-ryukyu.ac.jp>
date Sun, 30 Jan 2022 18:02:09 +0900
parents 806965e04299
children 1169915705ab
line wrap: on
line source

using System;
using System.Net;
using Christie_net.datagear.dg;
using MessagePack;

namespace Christie_net.topology{
    [MessagePackObject]
    public class HostMessage /*: Cloneable*/ { 
        [Key("hostName")]
        private String hostName = "";
        [Key("port")]
    private int port ;
    [Key("nodeName")]
    private String nodeName; // this is nodeName which have these IP and port.
    [Key("connectionName")]
    private String connectionName;
    [Key("remoteNodeName")]
    private String remoteNodeName;
    [Key("cookie")]
    private String cookie; // MD5
    [Key("alive")]
    private Boolean alive;

    public HostMessage() {}

    public void setLocalHostAndPort(int port){
        
        /*
        //localhostからIPアドレスを取得
        hostName = IPAddress.Loopback.ToString();
        */
        
        string ipaddress = "";
        IPHostEntry ipentry = Dns.GetHostEntry(Dns.GetHostName());
 
        foreach (IPAddress ip in ipentry.AddressList)
        {
            if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                hostName = ip.ToString();
            }
        }
        
        this.port = port;
    }

    public void setHostAndPort(HostMessage hostMessage) {
        setHostAndPort(hostMessage.getHostName(), hostMessage.getPort());
    }

    public void setHostAndPort(String hostName, int port) {
        this.hostName = hostName;
        this.port = port;
    }

    public void setNodeInfo(String nodeName, String connectionName, String remoteNodeName){
        this.nodeName = nodeName;
        this.connectionName = connectionName;
        this.remoteNodeName = remoteNodeName;
    }

    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }

    public String getHostName() { return hostName; }

    public int getPort() { return port; }

    public String getNodeName() { return nodeName; }

    public String getConnectionName() { return connectionName; }

    public String getRemoteNodeName() { return remoteNodeName; }

    public void setAlive(Boolean alive) { this.alive = alive; }

    public Boolean isAlive() { return alive; }

    public void setCookie(String cookie) { this.cookie = cookie; }

    public String getCookie() { return cookie; }

    public String toString() {
        return "HostMessage : name = " + hostName + ", port = " + Convert.ToInt32(port) + " connectionName = " +
                connectionName + " nodeName = " + nodeName + " remoteNodeName = " + remoteNodeName
                + " cokkie = " + cookie ;
    }

    /*
    public HostMessage clone(){

        HostMessage cloneHostMessage = new HostMessage();
        try {
            cloneHostMessage = (HostMessage)super.clone();
        }catch (Exception e){
            e.printStackTrace();
        }
        return cloneHostMessage;
    }
    */
    
    }
}