package cn.edu.zjvtit.cloudstorage.dataservice.rabbitmq.topic;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
public class Sender {
    // 心跳发送端关键代码
    @Autowired//自动装配
    private RabbitTemplate rabbitTemplate;//定义私有的消息模板
    @Autowired//自动装配
    private ServletWebServerApplicationContext webServerAppCtxt;//定义私有的容器



    private String getAddress(){
        int port = webServerAppCtxt.getWebServer().getPort();//获取运行中的端口号

        try {
            String addr =  InetAddress.getLocalHost().getHostAddress();//根据 主机名在host文件对应的ip来获取ip地址
            return addr+":"+port;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    public void sendServerAddress() {
        this.rabbitTemplate.convertAndSend("apiServers", getAddress());
    }
    //确定api服务器获取到地址
    public void sendFileInfo(String filename){
        this.rabbitTemplate.convertAndSend(filename, getAddress());
    }
    //确定获取到文件的地址
}
