aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Communication/PMR/IO/FileChunkDownloadRequest.pb-c.c
blob: e54f04f95ff0fa5470a0180f643b39f9180b39d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Generated by the protocol buffer compiler.  DO NOT EDIT! */
/* Generated from: FileChunkDownloadRequest.proto */

/* Do not generate deprecated warnings for self */
#ifndef PROTOBUF_C__NO_DEPRECATED
#define PROTOBUF_C__NO_DEPRECATED
#endif

#include "FileChunkDownloadRequest.pb-c.h"
void   file_chunk_download_request__init
                     (FileChunkDownloadRequest         *message)
{
  static const FileChunkDownloadRequest init_value = FILE_CHUNK_DOWNLOAD_REQUEST__INIT;
  *message = init_value;
}
size_t file_chunk_download_request__get_packed_size
                     (const FileChunkDownloadRequest *message)
{
  assert(message->base.descriptor == &file_chunk_download_request__descriptor);
  return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
}
size_t file_chunk_download_request__pack
                     (const FileChunkDownloadRequest *message,
                      uint8_t       *out)
{
  assert(message->base.descriptor == &file_chunk_download_request__descriptor);
  return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
}
size_t file_chunk_download_request__pack_to_buffer
                     (const FileChunkDownloadRequest *message,
                      ProtobufCBuffer *buffer)
{
  assert(message->base.descriptor == &file_chunk_download_request__descriptor);
  return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
}
FileChunkDownloadRequest *
       file_chunk_download_request__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data)
{
  return (FileChunkDownloadRequest *)
     protobuf_c_message_unpack (&file_chunk_download_request__descriptor,
                                allocator, len, data);
}
void   file_chunk_download_request__free_unpacked
                     (FileChunkDownloadRequest *message,
                      ProtobufCAllocator *allocator)
{
  if(!message)
    return;
  assert(message->base.descriptor == &file_chunk_download_request__descriptor);
  protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
static const ProtobufCFieldDescriptor file_chunk_download_request__field_descriptors[3] =
{
  {
    "DownloadID",
    1,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_STRING,
    0,   /* quantifier_offset */
    offsetof(FileChunkDownloadRequest, downloadid),
    NULL,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
  {
    "FileName",
    2,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_STRING,
    0,   /* quantifier_offset */
    offsetof(FileChunkDownloadRequest, filename),
    NULL,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
  {
    "Position",
    3,
    PROTOBUF_C_LABEL_OPTIONAL,
    PROTOBUF_C_TYPE_INT64,
    offsetof(FileChunkDownloadRequest, has_position),
    offsetof(FileChunkDownloadRequest, position),
    NULL,
    NULL,
    0,             /* flags */
    0,NULL,NULL    /* reserved1,reserved2, etc */
  },
};
static const unsigned file_chunk_download_request__field_indices_by_name[] = {
  0,   /* field[0] = DownloadID */
  1,   /* field[1] = FileName */
  2,   /* field[2] = Position */
};
static const ProtobufCIntRange file_chunk_download_request__number_ranges[1 + 1] =
{
  { 1, 0 },
  { 0, 3 }
};
const ProtobufCMessageDescriptor file_chunk_download_request__descriptor =
{
  PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
  "FileChunkDownloadRequest",
  "FileChunkDownloadRequest",
  "FileChunkDownloadRequest",
  "",
  sizeof(FileChunkDownloadRequest),
  3,
  file_chunk_download_request__field_descriptors,
  file_chunk_download_request__field_indices_by_name,
  1,  file_chunk_download_request__number_ranges,
  (ProtobufCMessageInit) file_chunk_download_request__init,
  NULL,NULL,NULL    /* reserved[123] */
};
; /// Gets the machine studio executable path. /// </summary> /// <returns></returns> public String GetMachineStudioExecutablePath() { String appPath = Path.Combine(Options.GetApplicationPath(), "Tango.MachineStudio.UI.exe"); return appPath; } /// <summary> /// Login to machine service and returns an access token. /// </summary> /// <returns></returns> private Task Login() { return Task.Factory.StartNew(() => { return _client.Login(new LoginRequest() { Email = Options.Email, Password = Options.Password, Version = GetLocalVersion(), }).Result; }); } /// <summary> /// Publish a machine studio version using the specified <see cref="Options"/>. /// </summary> /// <returns></returns> public Task Publish() { _client.Environment = Options.Environment; String appPath = GetMachineStudioExecutablePath(); String folder = Options.GetApplicationPath(); if (!File.Exists(appPath)) { throw new FileNotFoundException($"Could not locate the machine studio executable at {appPath}."); } if (!String.IsNullOrWhiteSpace(Options.InstallerProject)) { if (!File.Exists(Options.InstallerProject)) { throw new FileNotFoundException($"Installer project not found at '{Options.InstallerProject}'."); } if (!Directory.Exists(Options.InstallerOutputFolder)) { throw new DirectoryNotFoundException($"Installer output folder does not exists '{Options.InstallerOutputFolder}'."); } } String tempFile = TemporaryManager.CreateFile(); return Task.Factory.StartNew(() => { try { OnPublishProgress(0, 100, $"Logging in to machine service at {Options.Environment.ToAddress()}..."); Login().Wait(); OnPublishProgress(0, 100, $"Fetching remote version from {Options.Environment.ToAddress()}..."); String remote_version = GetRemoteVersion().Result; String local_version = GetLocalVersion(); OnPublishProgress(0, 100, $"Remote version: {remote_version}"); OnPublishProgress(0, 100, $"Local version: {local_version}"); if (Version.Parse(local_version) <= Version.Parse(remote_version)) { throw new InvalidOperationException($"The local version '{local_version}' is not greater than the remote version '{remote_version}'."); } OnPublishProgress(0, 100, $"Requesting version upload..."); var response = _client.UploadVersion(new UploadVersionRequest() { Version = local_version, Comments = Options.Comments, WithInstaller = !String.IsNullOrWhiteSpace(Options.InstallerProject), }).Result; if (!String.IsNullOrWhiteSpace(Options.InstallerProject)) { String output_folder = Options.InstallerOutputFolder; OnPublishProgress(0, 100, $"Building installer project..."); InstallerBuilder installerBuilder = new InstallerBuilder(Options.InstallerProject); String installer_name = $"Machine Studio Installer_v{Version.Parse(local_version).ToString(3)}.exe"; String output_file = Path.Combine(output_folder, installer_name); installerBuilder.Build(local_version, output_file).Wait(); Thread.Sleep(5000); OnPublishProgress(0, 100, $"Uploading installer '{installer_name}'..."); using (StorageBlobUploader uploader = new StorageBlobUploader(response.InstallerBlobAddress, output_file)) { uploader.Progress += (x, e) => { OnPublishProgress(e.Current, e.Total, $"Uploading installer {(((double)e.Current / (double)e.Total) * 100d).ToString("0.0")}%...", true); }; uploader.Upload().Wait(); } } OnPublishProgress(0, 100, $"Starting version packaging..."); using (ZipFile zip = new ZipFile()) { foreach (var directory in Directory.GetDirectories(folder)) { zip.AddDirectory(directory, $"/{Path.GetFileName(directory)}"); } var files = Directory.GetFiles(folder, "*.*", SearchOption.TopDirectoryOnly).ToList(); foreach (var file in files) { zip.AddFile(file, "/"); } zip.SaveProgress += (x, e) => { if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry) { OnPublishProgress(e.EntriesSaved + 1, e.EntriesTotal, $"Compressing files {(((double)(e.EntriesSaved + 1) / (double)e.EntriesTotal) * 100d).ToString("0.0")}%...", true); } }; zip.ParallelDeflateThreshold = -1; zip.Save(tempFile); } OnPublishProgress(0, 100, $"Starting version upload..."); using (StorageBlobUploader uploader = new StorageBlobUploader(response.BlobAddress, tempFile)) { uploader.Progress += (x, e) => { OnPublishProgress(e.Current, e.Total, $"Uploading version {(((double)e.Current / (double)e.Total) * 100d).ToString("0.0")}%...", true); }; uploader.Upload().Wait(); } OnPublishProgress(100, 100, $"Finalizing version upload..."); _client.NotifyVersionUploadCompleted(new UploadCompletedRequest() { Token = response.Token, }).Wait(); remote_version = GetRemoteVersion().Result; local_version = GetLocalVersion(); OnPublishProgress(0, 0, $"Remote version: {remote_version}"); OnPublishProgress(0, 0, $"Local version: {local_version}"); if (remote_version != local_version) { throw new InvalidOperationException("The remote version does not seems to have been updated."); } OnPublishProgress(0, 0, "Version published successfully."); } catch (Exception ex) { OnPublishProgress(0, 100, $"Failed: {ex.Message}"); throw ex; } finally { PathHelper.TryDeleteFile(tempFile); } }); } /// <summary> /// Raises the publish progress event. /// </summary> /// <param name="progress">The progress.</param> /// <param name="total">The total.</param> /// <param name="message">The message.</param> protected virtual void OnPublishProgress(double progress, double total, String message, bool singleLine = false) { PublishProgress?.Invoke(this, new PublishProgressEventArgs() { Progress = progress, Total = total, Message = message, SingleLineRecommended = singleLine, }); } } }