package p4go import "errors" // This is that higher level interface object that simplifies a lot of the // common bookkeeping. Most usage is going to use a single instance of // ClientApi, ClientUser, and Error in tandem. // // Since these allocate non-managed memory, you will also need to call Close() // to close the connection and free up data. type P4DAO interface { GetClientApi() ClientApi GetClientUser() ClientUser GetError() Error // Finalizes the connection and frees all memory associated with the connection Close() error // Returs true if the current object's internal Error has an error state HasError() bool // Return the current Error object as a standard go error CreateError() error ClientDAO ClientsDAO } type p4DaoHandle struct { ClientApi ClientApi ClientUser ClientUser Error Error } func (dao *p4DaoHandle) GetClientApi() ClientApi { return dao.ClientApi } func (dao *p4DaoHandle) GetClientUser() ClientUser { return dao.ClientUser } func (dao *p4DaoHandle) GetError() Error { return dao.Error } // Initializes the structure and connects to the underlying server, setting // the protocol to tagged output func AllocAndConnect(prog string, user string, password string, port string, client string) (P4DAO, error) { dao := alloc(prog, user, password, port, client) dao.GetClientApi().SetProtocol("tag", "") dao.GetClientApi().Init(dao.GetError()) if dao.HasError() { e := dao.CreateError() dao.free() return nil, e } return dao, nil } // Just allocates the memory for the new dao and sets common parameters func Alloc(prog string, user string, password string, port string, client string) P4DAO { return alloc(prog, user, password, port, client) } // Just allocates the memory for the new dao and sets common parameters func alloc(prog string, user string, password string, port string, client string) *p4DaoHandle { dao := p4DaoHandle{ ClientApi: NewClientApi(), ClientUser: NewClientUser(), Error: NewError(), } dao.ClientApi.SetUser(user) dao.ClientApi.SetPassword(password) dao.ClientApi.SetPort(port) dao.ClientApi.SetClient(client) dao.ClientApi.SetProg(prog) return &dao } func (dao *p4DaoHandle) Close() error { var e error = nil dao.GetClientApi().Final(dao.GetError()) if dao.HasError() { e = dao.CreateError() } dao.free() return e } // Just frees memory assoicated with the connection func (dao *p4DaoHandle) free() { dao.GetClientApi().Delete() dao.GetClientUser().Delete() dao.GetError().Delete() } // Reset any informative state before running the next command func (dao *p4DaoHandle) Clear() { dao.GetClientUser().Clear() dao.GetError().Clear() } // Returs true if the current object's p4go.Error has an error state func (dao *p4DaoHandle) HasError() bool { return dao.GetError().Test() != 0 } // Return the current p4go.Error object as a standard go error func (dao *p4DaoHandle) CreateError() error { sb := NewStrBuf() defer sb.Delete() dao.Error.Fmt(sb) return errors.New(sb.Text()) }