using System; using System.Collections.Generic; using System.Text; using p4dn; using System.Diagnostics; using P4API.Exceptions; namespace P4API { public class P4Connection : IDisposable { private p4dn.ClientApi m_ClientApi; private bool _tagged = false; private bool _Initialized; private string _CallingProgram; private string _CallingProgramVersion; private string _Client = null; private string _Port = null; private string _User = null; private string _Host = null; private string _CWD = null; private string _Charset = null; private p4dn.ClientApi _ClientAPI { get { if (m_ClientApi == null) { m_ClientApi = new ClientApi(); } //if (!_Initialized) throw new Exceptions.ServerNotConnected(); return m_ClientApi; } set { m_ClientApi = value; } } public P4Connection() { this._Initialized = false; this._CallingProgram = "P4.Net API"; this._CallingProgramVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); } /// <summary> /// Gets/Sets the client workspace. /// </summary> public string Client { get { return _ClientAPI.Client; } set { _Client = value; _ClientAPI.SetClient(value); } } /// <summary> /// Gets/Sets the current working directory. /// </summary> public string CWD { get { return _ClientAPI.Cwd; } set { _CWD = value; _ClientAPI.SetCwd(value); } } /// <summary> /// Gets/Sets the Host-name of the client. /// </summary> public string Host { get { return _ClientAPI.Host; } set { _Host = value; _ClientAPI.SetHost(value); } } /// <summary> /// Gets/Sets the Perforce Server port. /// </summary> public string Port { get { return _ClientAPI.Port; } set { if (_Initialized) throw new ServerAlreadyConnected(); _Port = value; _ClientAPI.SetPort(value); } } /// <summary> /// Gets/Sets the User login used to connect to Perforce. /// </summary> public string User { get { return _ClientAPI.User; } set { _User = value; _ClientAPI.SetUser(value); } } /// <summary> /// Gets/Sets the client character set. /// </summary> public string Charset { get { return _ClientAPI.Charset; } set { _Charset = value; _ClientAPI.SetCharset(value); } } /// <summary> /// Get/Sets the name of the calling program. /// </summary> /// <remarks>This value is seen in p4 monitor and in the server log files.</remarks> /// <value>Defaults to P4.Net API</value> public string CallingProgram { get { return _CallingProgram; } set { if (_Initialized) throw new ServerNotConnected_SetVar_AfterInit(); _CallingProgram = value; // Set these in the initialize section } } /// <summary> /// Gets/Sets the version of the calling program /// </summary> /// <remarks>This value is seen in p4 monitor and the server log files.</remarks> /// <value>Defaults to the version of the P4API.dll</value> public string CallingVersion { get { return _CallingProgramVersion; } set { if (_Initialized) throw new ServerNotConnected_SetVar_AfterInit(); _CallingProgramVersion = value; // Set these in the initialize section } } /// <summary> /// Sets the password to conenct with. /// </summary> /// <remarks>Do not set this value with a server running security level 2 ??? or higher. Use the Login() method.</remarks> public string Password { set { _ClientAPI.SetPassword(value); } } /// <summary> /// Sets the ticket file used for Authentication. /// </summary> public string TicketFile { set { // _ClientAPI.SetTicketFile(value); } } /// <summary> /// Connect to the Perforce server /// </summary> public void Connect() { EstablishConnection(_tagged); } /// <summary> /// Disconnect from the Perforce Server /// </summary> public void Disconnect() { CloseConnection(); } /// <summary> /// Login to the Perforce Server /// </summary> public void Login(string password) { throw new System.NotImplementedException(); } /// <summary> /// Fetch a form object from Perforce. /// </summary> /// <remarks> /// These are the things that pop-up in an editor, like: /// client /// branch /// label /// user /// group /// protect /// triggers /// branch /// </remarks> /// <returns>A Form object. The fields of the form can be read in, or updated. If you update a filed, you can save it with Save_Form.</returns> public P4Form Fetch_Form(string FormCommand) { throw new System.NotImplementedException(); } /// <summary> /// Saves the form to Perforce. /// </summary> /// <remarks>Executes the equivilent of p4 [form] -i</remarks> /// <returns>P4StringsResult. Output can be parsed to verify the form was processed correctly.</returns> public P4StringsResult Save_Form(P4Form Form) { throw new System.NotImplementedException(); } /// <summary> /// Executes the command in tagged mode. /// </summary> public P4TaggedResult Run_Tags(string Command, params string[] Args) { P4TaggedResult r = new P4TaggedResult(); EstablishConnection(true); RunIt(Command, Args, r.ResultClientUser, true); return r; } /// <summary> /// Executes the command in non-tagged mode. /// </summary> public P4StringsResult Run_Strings(string Command, params string[] Args) { P4StringsResult r = new P4StringsResult(); EstablishConnection(false); RunIt(Command, Args, r.ResultClientUser, false); return r; } #region Private Helper Methods private void EstablishConnection(bool tagged) { // If we're switching contexts from tagged to un-tagged, then we need to reset // the connection if ((m_ClientApi != null) && tagged != _tagged) { CloseConnection(); } if (m_ClientApi == null) { m_ClientApi = new ClientApi(); } if (!_Initialized) { Error err = null; try { err = new Error(); if (tagged) m_ClientApi.SetProtocol("tag", ""); if (tagged) m_ClientApi.SetProtocol("specstring", ""); if (this._Port != null) m_ClientApi.SetPort(_Port); m_ClientApi.Init(err); if (err.IsFatal()) { Trace.WriteLine(err.Fmt()); Trace.WriteLine("Unable to connect to Perforce!"); throw new Exception("Unable to connect to Perforce!"); } m_ClientApi.SetProg(_CallingProgram); m_ClientApi.SetVersion(_CallingProgramVersion); //May have lost our settings... reset here if (this._Client != null) m_ClientApi.SetClient(_Client); if (this._User != null) m_ClientApi.SetUser(_User); if (this._CWD != null) m_ClientApi.SetCwd(_CWD); if (this._Charset != null) m_ClientApi.SetCharset(_Charset); if (this._Host != null) m_ClientApi.SetHost(_Host); this._Initialized = true; err.Dispose(); } catch (Exception e) { Trace.WriteLine(e.Message); m_ClientApi.Final(err); err.Dispose(); m_ClientApi.Dispose(); m_ClientApi = null; } } } private void CloseConnection() { // Need to reset the connection if (_Initialized) { Error err = new Error(); m_ClientApi.Final(err); err.Dispose(); } m_ClientApi.Dispose(); m_ClientApi = null; this._Initialized = false; } private void RunIt(string command, string[] args, ClientUser cu, bool tagged) { RunIt(command, args, cu, tagged, false, false); } private void RunIt(string command, string[] args, ClientUser cu, bool tagged, bool ParseForms) { RunIt(command, args, cu, tagged, false, false); } private void RunIt(string command, string[] args, ClientUser cu, bool tagged, bool ParseForms, bool SaveForm) { m_ClientApi.SetArgv(args); if (SaveForm) { m_ClientApi.SaveForm(command, cu); } else { m_ClientApi.Run(command, cu); } } #endregion #region IDisposable Members /// <summary> /// Calls Disconnect. /// </summary> public void Dispose() { this.Disconnect(); } #endregion } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#18 | 5830 | Shawn Hladky | P4.Net: reorg to support release branches | ||
#17 | 5812 | Shawn Hladky | P4.Net: More documentation. | ||
#16 | 5801 | Shawn Hladky |
P4.Net. More misc changes |
||
#15 | 5798 | Shawn Hladky |
P4.Net... still not ready for beta Added license to all files Added several doc files Misc bugs |
||
#14 | 5734 | Shawn Hladky | More unit tests, and bug fixes. | ||
#13 | 5678 | Shawn Hladky |
WIP... more tests. OnPrompt event to recieve input from prompts. |
||
#12 | 5636 | Shawn Hladky |
1. Added test harness framework, and some initial tests 2. Fixed many bugs (oddly enough identified by the unit tests) 3. Fixes so will build 1.1 Framework (and build batch files actually work) 4. Pathetic attempt at documentation |
||
#11 | 5447 | Shawn Hladky | refactor, and added options class | ||
#10 | 5438 | Shawn Hladky | refactoring, and documentation code | ||
#9 | 5436 | Shawn Hladky | P4.Net -- Added some high-level functionality | ||
#8 | 5433 | Shawn Hladky | P4.Net More refactoring | ||
#7 | 5431 | Shawn Hladky |
Refactoring... step 1. |
||
#6 | 5427 | Shawn Hladky | P4.Net -- several fixes and added sample application | ||
#5 | 5411 | Shawn Hladky | WIP -- forms are working now. | ||
#4 | 5373 | Shawn Hladky | P4.Net: Still WIP, but some things starting to work | ||
#3 | 5362 | Shawn Hladky | Chipping away at the API changes | ||
#2 | 5350 | Shawn Hladky | WIP few more changes | ||
#1 | 5349 | Shawn Hladky |
Initial check-in for the new API interface. Nothing works yet, but it should compile at least. |