~dangarner/xibo/server162-testfix

« back to all changes in this revision

Viewing changes to client/dotNET/XmdsAgents/FileAgent.cs

  • Committer: Dan Garner
  • Date: 2014-04-27 20:12:32 UTC
  • mfrom: (313.3.4 client160rc2)
  • Revision ID: dan@xibo.org.uk-20140427201232-40141z8atd62uq4t
MergedĀ lp:~dangarner/xibo/client160rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
136
136
            {
137
137
                Trace.WriteLine(new LogMessage("FileAgent - Run", "Thread alive and Lock Obtained"), LogType.Info.ToString());
138
138
 
139
 
                while (!file.Complete)
 
139
                if (file.FileType == "resource")
140
140
                {
141
 
                    byte[] getFileReturn;
142
 
 
143
 
                    // Call XMDS GetFile
 
141
                    // Download using GetResource
144
142
                    using (xmds.xmds xmds = new xmds.xmds())
145
143
                    {
146
144
                        xmds.Credentials = null;
147
145
                        xmds.Url = Settings.Default.XiboClient_xmds_xmds;
148
 
                        xmds.UseDefaultCredentials = false;
149
 
 
150
 
                        getFileReturn = xmds.GetFile(Settings.Default.ServerKey, _hardwareKey, file.Path, file.FileType, file.ChunkOffset, file.ChunkSize, Settings.Default.Version);
151
 
                    }
152
 
 
153
 
                    // Set the flag to indicate we have a connection to XMDS
154
 
                    Settings.Default.XmdsLastConnection = DateTime.Now;
155
 
 
156
 
                    if (file.FileType == "layout")
157
 
                    {
158
 
                        // Decode this byte[] into a string and stick it in the file.
159
 
                        string layoutXml = Encoding.UTF8.GetString(getFileReturn);
160
 
 
161
 
                        // Full file is downloaded
 
146
                        xmds.UseDefaultCredentials = true;
 
147
 
 
148
                        string result = xmds.GetResource(Settings.Default.ServerKey, Settings.Default.hardwareKey, file.LayoutId, file.RegionId, file.MediaId, Settings.Default.Version);
 
149
 
 
150
                        // Write the result to disk
162
151
                        using (StreamWriter sw = new StreamWriter(File.Open(Settings.Default.LibraryPath + @"\" + file.Path, FileMode.Create, FileAccess.Write, FileShare.Read)))
163
152
                        {
164
 
                            sw.Write(layoutXml);
 
153
                            sw.Write(result);
165
154
                            sw.Close();
166
155
                        }
167
156
 
 
157
                        // File completed
 
158
                        file.Downloading = false;
168
159
                        file.Complete = true;
169
160
                    }
 
161
                }
 
162
                else
 
163
                {
 
164
                    while (!file.Complete)
 
165
                    {
 
166
                        byte[] getFileReturn;
 
167
 
 
168
                        // Call XMDS GetFile
 
169
                        using (xmds.xmds xmds = new xmds.xmds())
 
170
                        {
 
171
                            xmds.Credentials = null;
 
172
                            xmds.Url = Settings.Default.XiboClient_xmds_xmds;
 
173
                            xmds.UseDefaultCredentials = false;
 
174
 
 
175
                            getFileReturn = xmds.GetFile(Settings.Default.ServerKey, _hardwareKey, file.Path, file.FileType, file.ChunkOffset, file.ChunkSize, Settings.Default.Version);
 
176
                        }
 
177
 
 
178
                        // Set the flag to indicate we have a connection to XMDS
 
179
                        Settings.Default.XmdsLastConnection = DateTime.Now;
 
180
 
 
181
                        if (file.FileType == "layout")
 
182
                        {
 
183
                            // Decode this byte[] into a string and stick it in the file.
 
184
                            string layoutXml = Encoding.UTF8.GetString(getFileReturn);
 
185
 
 
186
                            // Full file is downloaded
 
187
                            using (StreamWriter sw = new StreamWriter(File.Open(Settings.Default.LibraryPath + @"\" + file.Path, FileMode.Create, FileAccess.Write, FileShare.Read)))
 
188
                            {
 
189
                                sw.Write(layoutXml);
 
190
                                sw.Close();
 
191
                            }
 
192
 
 
193
                            file.Complete = true;
 
194
                        }
 
195
                        else
 
196
                        {
 
197
                            // Media file
 
198
                            // Need to write to the file - in append mode
 
199
                            using (FileStream fs = new FileStream(Settings.Default.LibraryPath + @"\" + file.Path, FileMode.Append, FileAccess.Write))
 
200
                            {
 
201
                                fs.Write(getFileReturn, 0, getFileReturn.Length);
 
202
                                fs.Close();
 
203
                            }
 
204
 
 
205
                            // Increment the offset by the amount we just asked for
 
206
                            file.ChunkOffset = file.ChunkOffset + file.ChunkSize;
 
207
 
 
208
                            // Has the offset reached the total size?
 
209
                            if (file.Size > file.ChunkOffset)
 
210
                            {
 
211
                                int remaining = file.Size - file.ChunkOffset;
 
212
 
 
213
                                // There is still more to come
 
214
                                if (remaining < file.ChunkSize)
 
215
                                {
 
216
                                    // Get the remaining
 
217
                                    file.ChunkSize = remaining;
 
218
                                }
 
219
 
 
220
                                // Part is complete
 
221
                                OnPartComplete(file.Id);
 
222
                            }
 
223
                            else
 
224
                            {
 
225
                                // File complete
 
226
                                file.Complete = true;
 
227
                            }
 
228
                        }
 
229
 
 
230
                        getFileReturn = null;
 
231
                    }
 
232
 
 
233
                    // File completed
 
234
                    file.Downloading = false;
 
235
 
 
236
                    // Check MD5
 
237
                    string md5 = _requiredFiles.CurrentCacheManager.GetMD5(file.Path);
 
238
                    if (file.Md5 == md5)
 
239
                    {
 
240
                        // Mark it as complete
 
241
                        _requiredFiles.MarkComplete(_requiredFileId, file.Md5);
 
242
 
 
243
                        // Add it to the cache manager
 
244
                        _requiredFiles.CurrentCacheManager.Add(file.Path, file.Md5);
 
245
 
 
246
                        Trace.WriteLine(new LogMessage("FileAgent - Run", "File Downloaded Successfully. " + file.Path), LogType.Info.ToString());
 
247
                    }
170
248
                    else
171
249
                    {
172
 
                        // Media file
173
 
                        // Need to write to the file - in append mode
174
 
                        using (FileStream fs = new FileStream(Settings.Default.LibraryPath + @"\" + file.Path, FileMode.Append, FileAccess.Write))
175
 
                        {
176
 
                            fs.Write(getFileReturn, 0, getFileReturn.Length);
177
 
                            fs.Close();
178
 
                        }
179
 
 
180
 
                        // Increment the offset by the amount we just asked for
181
 
                        file.ChunkOffset = file.ChunkOffset + file.ChunkSize;
182
 
 
183
 
                        // Has the offset reached the total size?
184
 
                        if (file.Size > file.ChunkOffset)
185
 
                        {
186
 
                            int remaining = file.Size - file.ChunkOffset;
187
 
                            
188
 
                            // There is still more to come
189
 
                            if (remaining < file.ChunkSize)
190
 
                            {
191
 
                                // Get the remaining
192
 
                                file.ChunkSize = remaining;
193
 
                            }
194
 
 
195
 
                            // Part is complete
196
 
                            OnPartComplete(file.Id);
197
 
                        }
198
 
                        else
199
 
                        {
200
 
                            // File complete
201
 
                            file.Complete = true;
202
 
                        }
 
250
                        // Just error - we will pick it up again the next time we download
 
251
                        Trace.WriteLine(new LogMessage("FileAgent - Run", "Downloaded file failed MD5 check. Calculated [" + md5 + "] & XMDS [ " + file.Md5 + "] . " + file.Path), LogType.Error.ToString());
203
252
                    }
204
 
 
205
 
                    getFileReturn = null;
206
 
                }
207
 
 
208
 
                // File completed
209
 
                file.Downloading = false;
210
 
 
211
 
                // Check MD5
212
 
                string md5 = _requiredFiles.CurrentCacheManager.GetMD5(file.Path);
213
 
                if (file.Md5 == md5)
214
 
                {
215
 
                    // Mark it as complete
216
 
                    _requiredFiles.MarkComplete(_requiredFileId, file.Md5);
217
 
 
218
 
                    // Add it to the cache manager
219
 
                    _requiredFiles.CurrentCacheManager.Add(file.Path, file.Md5);
220
 
 
221
 
                    Trace.WriteLine(new LogMessage("FileAgent - Run", "File Downloaded Successfully. " + file.Path), LogType.Info.ToString());
222
 
                }
223
 
                else
224
 
                {
225
 
                    // Just error - we will pick it up again the next time we download
226
 
                    Trace.WriteLine(new LogMessage("FileAgent - Run", "Downloaded file failed MD5 check. Calculated [" + md5 + "] & XMDS [ " + file.Md5 + "] . " + file.Path), LogType.Error.ToString());
227
253
                }
228
254
 
229
255
                // Inform the Player thread that a file has been modified.