Пример #1
0
Файл: cim.go Проект: LDCS/cim
func NewCimServer(host, simInst string, root *CimNode, data interface{}) (*CimServer, error) {
	cs := new(CimServer)
	if host == "" {
		host = "127.0.0.1"
	}
	cs.host = host
	cs.simInst = simInst
	cs.root = root
	cs.data = data
	var ccfg *qcfg.CfgBlock = qcfg.NewCfg(simInst, cfgFile, false)
	var port = ccfg.Str("ports-lst", "ports", simInst, "")
	if port == "" {
		return nil, errors.New("Invalid sim : " + simInst)
	}
	cs.port = port
	return cs, nil
}
Пример #2
0
Файл: cim.go Проект: LDCS/cim
func NewCimConnection(host, simInst, connId string) (*CimConnection, error) {
	cc := new(CimConnection)
	if host == "" {
		host = "127.0.0.1"
	}
	cc.host = host
	cc.simInst = simInst
	var ccfg *qcfg.CfgBlock = qcfg.NewCfg(connId, cfgFile, false)
	var port = ccfg.Str("ports-lst", "ports", simInst, "")
	if port == "" {
		return nil, errors.New("Invalid sim : " + simInst)
	}
	cc.port = port
	err := errors.New("")
	cc.tcpAddr, err = net.ResolveTCPAddr("tcp4", cc.host+":"+cc.port)
	if err != nil {
		return nil, err
	}
	cc.tcpConn, err = net.DialTCP("tcp4", nil, cc.tcpAddr)
	if err != nil {
		return nil, err
	}
	err1 := cc.tcpConn.SetKeepAlive(true)
	err2 := cc.tcpConn.SetNoDelay(true)
	if err1 != nil || err2 != nil {
		cc.tcpConn.Close()
		return nil, errors.New("Cannot set KeepAlive or NoDelay")
	}
	_, err = cc.tcpConn.Write(gen_login_msg())
	if err != nil {
		cc.tcpConn.Close()
		return nil, errors.New("Cannot send login message to sim")
	}
	buf := make([]byte, 1024)
	_, err = cc.tcpConn.Read(buf)
	if err != nil {
		cc.tcpConn.Close()
		return nil, errors.New("Cannot read login msg response from sim")
	}
	//fmt.Println("Login response :", buf[:n])
	cc.resp = make([]byte, responseMaxLength)
	return cc, nil
}