Example #1
0
        // this code runs when the button "Publish" is clicked
        private async void btnPublish_Click(object sender, RoutedEventArgs e)
        {
            if (txtTopicPublish.Text != "")
            {
                string Topic = txtTopicPublish.Text.Trim();
                btnPublish2.Content = DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);

                switch (Topic.ToLower())
                {
                case "picture":
                    if (selectedFileName != "")
                    {
                        var messageImage = new MqttApplicationMessageBuilder()
                                           .WithTopic(Topic)
                                           .WithPayload(Utils.ConvertBitmapSourceToByteArray(selectedFileName))
                                           .WithExactlyOnceQoS()
                                           .WithRetainFlag()
                                           .Build();

                        await mqttClient.PublishAsync(messageImage, CancellationToken.None);
                    }
                    break;

                case "bundle":
                    DataInfo imageData = new DataInfo
                    {
                        PublisherMessage = txtPublish.Text,
                        FileName         = System.IO.Path.GetFileName(selectedFileName),
                        ImageData        = string.IsNullOrEmpty(selectedFileName) ? null : Utils.ConvertBitmapSourceToByteArray(selectedFileName),
                        SentDateTime     = DateTime.Now
                    };

                    var messageBundle = new MqttApplicationMessageBuilder()
                                        .WithTopic(Topic)
                                        .WithPayload(ExtendedSerializerExtensions.Serialize(imageData))
                                        .WithExactlyOnceQoS()
                                        .WithRetainFlag()
                                        .Build();

                    await mqttClient.PublishAsync(messageBundle, CancellationToken.None);

                    break;

                default:
                    var message = new MqttApplicationMessageBuilder()
                                  .WithTopic(Topic)
                                  .WithPayload(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff", CultureInfo.InvariantCulture) + "\n" + txtPublish.Text)
                                  .WithExactlyOnceQoS()
                                  .WithRetainFlag()
                                  .Build();
                    await mqttClient.PublishAsync(message, CancellationToken.None);     // Since 3.0.5 with CancellationToken

                    break;
                }
            }
            else
            {
                MessageBox.Show("You have to enter a topic to publish!");
            }
        }
Example #2
0
        // this code runs when the button "Publish" is clicked
        private void btnPublish_Click(object sender, RoutedEventArgs e)
        {
            if (txtTopicPublish.Text != "")
            {
                // whole topic
                //string Topic = "/ElektorMyJourneyIoT/" + txtTopicPublish.Text + "/test";
                string Topic = txtTopicPublish.Text.Trim();
                btnPublish2.Content = DateTime.Now.ToString("HH:mm:ss.fff", CultureInfo.InvariantCulture);

                switch (Topic.ToLower())
                {
                case "picture":
                    if (selectedFileName != "")
                    {
                        client.Publish(Topic, Utils.ConvertBitmapSourceToByteArray(selectedFileName), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                    }
                    //selectedFileName = "";
                    break;

                case "bundle":
                    DataInfo imageData = new DataInfo {
                        PublisherMessage = txtPublish.Text,
                        FileName         = Path.GetFileName(selectedFileName),
                        ImageData        = string.IsNullOrEmpty(selectedFileName) ? null:Utils.ConvertBitmapSourceToByteArray(selectedFileName),
                        SentDateTime     = DateTime.Now
                    };
                    client.Publish(Topic, ExtendedSerializerExtensions.Serialize(imageData), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                    //selectedFileName = "";
                    break;

                default:
                    // publish a message with QoS 2
                    client.Publish(Topic, Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff", CultureInfo.InvariantCulture) + "\n" + txtPublish.Text), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
                    break;
                }
            }
            else
            {
                MessageBox.Show("You have to enter a topic to publish!");
            }
        }
Example #3
0
        private async Task DoFS(Socket sock)
        {
            do
            {
                var clientSocket = await Task.Factory.FromAsync(
                    new Func <AsyncCallback, object, IAsyncResult>(sock.BeginAccept),
                    new Func <IAsyncResult, Socket>(sock.EndAccept), null).ConfigureAwait(false);

                Console.WriteLine("connected");

                using (var stream = new NetworkStream(clientSocket, true))
                {
                    byte[] modeBytes = new byte[4];
                    stream.Read(modeBytes, 0, modeBytes.Length);
                    int mode = BitConverter.ToInt32(modeBytes);

                    //Send file to client
                    if (mode == 2)
                    {
                        Console.WriteLine("Send file to client");

                        string path = "ServerFiles/" + clientSocket.RemoteEndPoint.ToString().Split(':')[0] + "/";
                        //Dictionary<string, string> fileList = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(path + "Manager.json")); ;

                        byte[] packLength = new byte[4];

                        stream.Read(packLength, 0, 4);
                        byte[] keyBytes = new byte[BitConverter.ToInt32(packLength)];
                        stream.Read(keyBytes, 0, keyBytes.Length);

                        string key = Encoding.UTF8.GetString(keyBytes);
                        Console.WriteLine(key);
                        byte[] fileData = File.ReadAllBytes(path + key + ".dat");

                        /*var memstream = new MemoryStream();
                         * memstream.Write(fileData);
                         * memstream.Position = 0;*/
                        //FileEncoding.FileStructure fs = FileEncoding.FileStructureRetriever(memstream);
                        //Console.WriteLine(fs.fileName);

                        stream.Write(fileData);
                    }
                    //Send list of files and times until able to be recieved
                    else if (mode == 0)
                    {
                        string path = "ServerFiles/" + clientSocket.RemoteEndPoint.ToString().Split(':')[0] + "/";
                        //Console.WriteLine(File.Exists(path+"Manager.json"));
                        Dictionary <string, string> fileList = JsonConvert.DeserializeObject <Dictionary <string, string> >(File.ReadAllText(path + "Manager.json"));;
                        List <string> fileNames = new List <string>();

                        try
                        {
                            fileList = JsonConvert.DeserializeObject <Dictionary <string, string> >(File.ReadAllText(path + "Manager.json"));
                        }
                        catch
                        {
                            stream.Write(BitConverter.GetBytes(1));
                            clientSocket.Close();
                        }
                        stream.Write(BitConverter.GetBytes(0));
                        foreach (KeyValuePair <string, string> entry in fileList)
                        {
                            DateTime endTime = DateTime.Parse(entry.Value).AddDays(SnailMailServer.days_delayed);
                            TimeSpan ts      = endTime.Subtract(DateTime.UtcNow);
                            if (ts.CompareTo(TimeSpan.Zero) > 0)
                            {
                                fileNames.Add(entry.Key + " - " + ts.ToString(@"dd\:hh\:mm\:ss"));
                            }
                            else
                            {
                                ts = new TimeSpan(0, 0, 0, 0);
                                fileNames.Add(entry.Key + " - " + ts.ToString(@"dd\:hh\:mm\:ss"));
                            }
                        }

                        /*foreach(string s in fileNames)
                         * {
                         *  Console.WriteLine(s);
                         * }*/

                        byte[] dataToSend = ExtendedSerializerExtensions.Serialize(fileNames);
                        //List<string> yeetus = (List<string>)binForm.Deserialize(memStream);

                        //Console.WriteLine(yeetus[0]);
                        List <byte> fullLoad = new List <byte>();
                        fullLoad.AddRange(BitConverter.GetBytes(dataToSend.Length));
                        fullLoad.AddRange(dataToSend);
                        List <string> yeetus = ExtendedSerializerExtensions.Deserialize <List <string> >(dataToSend);
                        Console.WriteLine(yeetus[0]);
                        stream.Write(fullLoad.ToArray());
                    }
                    //Recieve file from client
                    else if (mode == 1)
                    {
                        FileEncoding.FileStructure fs = FileEncoding.FileStructureRetriever(stream);
                        byte[] bytes = FileEncoding.StreamPrepper(fs);
                        Dictionary <string, string> fileList;
                        string path = "ServerFiles/" + fs.ipSender.ToString() + "/";

                        if (!Directory.Exists("ServerFiles/" + fs.ipSender.ToString() + "/"))
                        {
                            Directory.CreateDirectory(path);
                            FileStream stream1 = File.Create(path + "Manager.json");
                            stream1.Write(Encoding.UTF8.GetBytes("{}"));
                            stream1.Close();
                        }
                        try
                        {
                            fileList = JsonConvert.DeserializeObject <Dictionary <string, string> >(File.ReadAllText(path + "Manager.json"));
                        }
                        catch
                        {
                            fileList = new Dictionary <string, string>();
                        }
                        FileStream fstream = File.Create(path + fs.fileName + ".dat");
                        if (!fileList.TryGetValue(fs.fileName, out _))
                        {
                            fileList.Add(fs.fileName, fs.dateSent);
                        }
                        else
                        {
                            fileList[fs.fileName] = fs.dateSent;
                        }

                        File.WriteAllText(path + "Manager.json", JsonConvert.SerializeObject(fileList, Formatting.Indented));
                        fstream.Write(bytes);
                        fstream.Close();
                        Console.WriteLine(fs.ipSender.ToString());
                        clientSocket.Close();
                    }
                }
            } while (true);
        }