Example #1
0
File: tsdb.cs Project: emm274/fcObj
        void __sendData(string what, string method, byte[] data, string path, tquery query, tnotify notify)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Host + "/" + what);
            request.Credentials = new NetworkCredential(Login, Password);
            request.Method = method;
            request.Timeout = 1000 * 60 * 5;    // 5 min

            request.Accept = "*/*";
            request.ContentType = "application/json";

            if (data != null)
            {
                int cx = data.Length;
                request.ContentLength = cx;

                request.ServicePoint.ConnectionLimit = 1;
                request.ServicePoint.Expect100Continue = true;

                Stream stream = request.GetRequestStream();

                if (cx < bufferSize)
                    stream.Write(data, 0, cx);
                else
                {
                    int bx = 0;
                    while (bx < cx)
                    {
                        int dx = cx - bx;
                        if (dx > bufferSize) dx = bufferSize;

                        stream.Write(data, bx, dx);
                        stream.Flush();

                        bx += dx;
                    }
                }

                stream.Close();
            }
            else
            {
                byte[] buf = new byte[bufferSize];

                using (BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open)))
                {
                    int cx = (int)br.BaseStream.Length;
                    request.ContentLength = cx;
                    Stream stream = request.GetRequestStream();

                    int bx = 0;
                    while (bx < cx)
                    {
                        int dx = cx - bx;
                        if (dx > bufferSize) dx = bufferSize;
                        br.Read(buf, 0, dx);
                        stream.Write(buf, 0, dx);
                        bx += dx;
                    }

                    stream.Close();
                }
            }

            if (request.ContentLength > 0)
            request_json(request, query, notify);

            __endTask(this, null);
        }
Example #2
0
 void taskCallback(object sender, tnotify cb)
 {
     if (cb != null)
     this.Invoke( new EventHandler(cb) );
 }
Example #3
0
File: tsdb.cs Project: emm274/fcObj
 void __postJson(string what, string data, tquery query, tnotify notify)
 {
     __sendData(what,"POST", convert.GetBytes(data),null, query, notify);
 }
Example #4
0
File: tsdb.cs Project: emm274/fcObj
 void __request(string what, string method, tquery query, tnotify notify)
 {
     WebRequest request = WebRequest.Create(Host + "/" + what);
     request.Credentials = new NetworkCredential(Login, Password);
     request.Method = method;
     request_json(request, query, notify);
     __endTask(this, null);
 }
Example #5
0
File: tsdb.cs Project: emm274/fcObj
 void __get(string what, tquery query, tnotify notify)
 {
     __request(what, "GET", query, notify);
 }
Example #6
0
File: tsdb.cs Project: emm274/fcObj
 void __postFile(string what, string path, tquery query, tnotify notify)
 {
     FileInfo inf = new FileInfo(path);
     if (inf.Length >= 0xffff)
         xmap_post(path,query,notify);
     else
         __sendData(what,"POST",  File.ReadAllBytes(path), null, query, notify);
 }
Example #7
0
File: tsdb.cs Project: emm274/fcObj
        void request_notify(tnotify notify)
        {
            if (notify != null)

                if (fnotifyTask != null)
                    fnotifyTask(this, notify);
                else
                    notify(this, null);
        }
Example #8
0
File: tsdb.cs Project: emm274/fcObj
        void xmap_put(string what, string path, tquery query, tnotify notify)
        {
            xmap.Ixmap_auto map = new xmap_auto();

            string response = Path.GetDirectoryName(path) + "\\response";
            if (File.Exists(response)) File.Delete(response);

            int rc;
            map.HttpPut(Host + "/" + what, Login, Password,
                         "application/json", path, response, out rc);

            if (rc == 1)
            if (File.Exists(response))
            using (StreamReader reader = new StreamReader(response))
            {
                fquery = query; Parse(reader);
                request_notify(notify);
            }
        }
Example #9
0
File: tsdb.cs Project: emm274/fcObj
        void curl_post(string path, tquery query, tnotify notify)
        {
            string _auth = Login+":"+Password;
            string _enc = System.Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));

            string dir = Path.GetDirectoryName(path);
            string json = Path.GetFileName(path);

            string bin = Directory.GetCurrentDirectory();
            Directory.SetCurrentDirectory(dir);

            string arguments = "-s" +
                               " -H \"Content-Type: application/json\"" +
                               " -H \"Authorization: Basic " + _enc + "\"" +
                               " -X POST --data-binary @" + json +
                               " " + Host + "/" + "commit";

            __message("curl.exe", "commit...");

            try
            {
                Process proc = new Process();
                proc.StartInfo.FileName = bin + "\\curl.exe";
                proc.StartInfo.Arguments = arguments;

                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.CreateNoWindow = true;

                proc.Start();

                string ack = dir + "\\response";
                if (File.Exists(ack)) File.Delete(ack);

                using (StreamWriter sw = new StreamWriter(ack))
                {

                    while (!proc.StandardOutput.EndOfStream)
                    {
                        string line = proc.StandardOutput.ReadLine();
                        sw.WriteLine(line);
                    }

                    sw.Close();
                }

                if (File.Exists(ack))
                using (StreamReader reader = new StreamReader(ack))
                {
                    fquery = query; Parse(reader);
                    request_notify(notify);
                }
            }
            catch (Exception e)
            {
                __message("curl", e.Message);
            }

            Directory.SetCurrentDirectory(bin);
        }
Example #10
0
File: tsdb.cs Project: emm274/fcObj
        void request_json(WebRequest request, tquery query, tnotify notify)
        {
            try
            {
                WebResponse response = request.GetResponse();
                response_json(response, query);
                response.Close();

                request_notify(notify);
            }
            catch (WebException e)
            {
                web_error(e,request.Method);
            }
        }
Example #11
0
File: tsdb.cs Project: emm274/fcObj
        public void undo_commit(string branch, string commit, tnotify notify)
        {
            XJsonStringWriter sw = new XJsonStringWriter();
            JsonWriter writer = sw.writer;
            writer.WriteStartObject();
            writer.WritePropertyName("commit");
            writer.WriteValue(commit);
            writer.WriteEndObject();
            writer.Close();

            byte[] data = sw.Data();
            if (data != null)
            {
                string tmp = OFiles.GetTmpPath("send");
                OFiles.dumpData(tmp, data);

                string what = "/branch/"+branch+"?force=true";

                __beginTask(String.Format("{0}: undo commit {1}", branch, commit));
                task = Task.Run(() => xmap_put(what, tmp, tquery.delete, notify));
            //              task = Task.Run(() => __sendData(what, "PUT", data, null, tquery.delete, notify));
            }
        }
Example #12
0
File: tsdb.cs Project: emm274/fcObj
 public void getVersion(tnotify notify)
 {
     if (convert.IsString(Login) && convert.IsString(Password))
     {
         __beginTask("get version");
         task = Task.Run(() => __get("", tquery.getVersion, notify));
     }
 }
Example #13
0
File: tsdb.cs Project: emm274/fcObj
 public void getCommits(string name, tnotify notify)
 {
     fCommits.Clear(); __beginTask("get commits");
     task = Task.Run(() => __get("branch/" + name + "/log", tquery.getCommits, notify));
 }
Example #14
0
File: tsdb.cs Project: emm274/fcObj
 public void getBranches(tnotify notify)
 {
     fBranches.Clear(); __beginTask("get branches");
     task = Task.Run(() => __get("branches", tquery.getBranches, notify));
 }
Example #15
0
File: tsdb.cs Project: emm274/fcObj
 public void commit(string name, string path, tnotify notify)
 {
     __beginTask("commit: " + name);
     task = Task.Run(() => __postFile("/commit", path, tquery.post, notify));
 }