public void ReadQueueTestNegativeNoGracePeriod() { var msg = "Hello, world!"; var uid = new UserEngine().RegisterUser(0, Encoding.ASCII.GetBytes("Aa123456")); var uid1 = new UserEngine().RegisterUser(0, Encoding.ASCII.GetBytes("Aa123456")); new QueueEngine().CreateQueue(uid, 0, "A", new List <List <int> > { new List <int> { uid1, 0 } }); try { new QueueEngine().WriteBufferedQueue(uid, 0, "A", msg); var messages = new QueueEngine().ReadQueue(uid1, uid, 0, "A", false); if (messages.Count != 0) { Assert.Fail("Invalid number of read messages"); } } catch (Exception e) { Assert.Fail(e.Message); } }
public void ReadQueueTestNoCommit() { var msg = "Hello, world!"; var uid = new UserEngine().RegisterUser(0, Encoding.ASCII.GetBytes("Aa123456")); var uid1 = new UserEngine().RegisterUser(0, Encoding.ASCII.GetBytes("Aa123456")); new QueueEngine().CreateQueue(uid, 0, "A", new List <List <int> > { new List <int> { uid1, 0 } }); try { new QueueEngine().WriteBufferedQueue(uid, 0, "A", msg); /* Wait for grace period */ Thread.Sleep(Config <int> .GetInstance()["QUEUE_GRACE_PERIOD"]); new QueueEngine().HandleQueueBuffer(); var messages = new QueueEngine().ReadQueue(uid1, uid, 0, "A", false); if (messages.Count != 1) { Assert.Fail("Invalid number of read messages"); } if (messages[0] != msg) { Assert.Fail("Invalid read message"); } messages = new QueueEngine().ReadQueue(uid1, uid, 0, "A", false); if (messages.Count != 1) { Assert.Fail("Invalid number of read messages"); } if (messages[0] != msg) { Assert.Fail("Invalid read message"); } } catch (Exception e) { Assert.Fail(e.Message); } }
public void Serve(HttpListenerRequest request, HttpListenerResponse response, Url url) { var queueCreateRequest = (QueueCreateRequest) new DataContractJsonSerializer(typeof(QueueCreateRequest)).ReadObject( request.InputStream); var jsonResponse = new BooleanResponse { Success = false }; if (!UserEngine.LoginBySessionId(queueCreateRequest.Id, queueCreateRequest.NodeId, queueCreateRequest.SessionKey)) { jsonResponse.Message = "Login Failed"; } else { try { QueueEngine.CreateQueue(queueCreateRequest.Id, queueCreateRequest.NodeId, queueCreateRequest.QueueName, queueCreateRequest.Readers); PartnersEngine.PartnersUpdateRequest(new PartnerSyncQueueCreate { NodeId = queueCreateRequest.NodeId, UID = queueCreateRequest.Id, QueueName = queueCreateRequest.QueueName, Readers = queueCreateRequest.Readers }); jsonResponse.Success = true; } catch (Exception e) { jsonResponse.Message = e.Message; } } new DataContractJsonSerializer(typeof(BooleanResponse)).WriteObject(response.OutputStream, jsonResponse); }
public void Serve(HttpListenerRequest request, HttpListenerResponse response, Url url) { var partnerSyncRequest = JSONSerializer <PartnerSyncMessage> .Deserialize(request.InputStream); var jsonResponse = new BooleanResponse { Success = false }; /* Validate incoming certificate */ try { if (!CryptoEngine.GetInstance().verifyCertificate(partnerSyncRequest.key, partnerSyncRequest.certId, partnerSyncRequest.cert) .VerifyData(partnerSyncRequest.data, partnerSyncRequest.signature, HashAlgorithmName.SHA256)) { throw new CryptographicException("Data verification failed"); } /* Parse action */ var partnerSyncRequestData = JSONSerializer <PartnerSyncMessageData> .Deserialize(partnerSyncRequest.data); /* Figure out which message type need to be handled */ switch (partnerSyncRequestData.MessageType) { case PartnerSyncMessageType.PARTNER_JOIN: { /* Parse join request */ var partnerJoinRequest = JSONSerializer <PartnerSyncRequestJoin> .Deserialize(partnerSyncRequestData.Data); /* Add to partners */ PartnersEngine.AddPartner(partnerJoinRequest.Address); /* Create a DB Dump object */ var partnerDBDump = new PartnerSyncResponseDBDump { Partners = PartnersEngine.Partners.ToArray() }; /* Dump te DB */ var dbFile = File.Open(Config <string> .GetInstance()["DB_Filename"], FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (var reader = new BinaryReader(dbFile)) { /* Hopefully DB will not be larger than 2GB */ partnerDBDump.DBDump = reader.ReadBytes((int)dbFile.Length); } JSONSerializer <PartnerSyncMessage> .Serialize(PartnersEngine.PrepareSignedMessage(partnerDBDump), response.OutputStream); break; } case PartnerSyncMessageType.USER_CREATE: { /* Parse register request */ var userRegisterRequest = JSONSerializer <PartnerSyncUserCreate> .Deserialize(partnerSyncRequestData.Data); /* Update here */ UserEngine.RegisterUser(partnerSyncRequest.certId, userRegisterRequest.Id, Encoding.ASCII.GetBytes(userRegisterRequest.Key)); jsonResponse.Success = true; jsonResponse.Message = "Success"; JSONSerializer <PartnerSyncMessage> .Serialize(PartnersEngine.PrepareSignedMessage(jsonResponse), response.OutputStream); break; } case PartnerSyncMessageType.QUEUE_CREATE: { /* Parse queue create request */ var queueCreateRequest = JSONSerializer <PartnerSyncQueueCreate> .Deserialize(partnerSyncRequestData.Data); QueueEngine.CreateQueue(queueCreateRequest.UID, queueCreateRequest.NodeId, queueCreateRequest.QueueName, queueCreateRequest.Readers); jsonResponse.Success = true; jsonResponse.Message = "Success"; JSONSerializer <PartnerSyncMessage> .Serialize(PartnersEngine.PrepareSignedMessage(jsonResponse), response.OutputStream); break; } case PartnerSyncMessageType.QUEUE_WRITE: { /* Parse queue write request */ var queueWriteRequest = JSONSerializer <PartnerSyncQueueWrite> .Deserialize(partnerSyncRequestData.Data); /* Try to correct timezone issues */ Config <long> .GetInstance()["TIMEZONE_CORRECTION"] = queueWriteRequest.Timestamp.ToFileTimeUtc() - DateTime.UtcNow.ToFileTimeUtc(); /* Add to buffered queue */ if (QueueEngine.WriteBufferedQueue(queueWriteRequest.UID, queueWriteRequest.NodeId, queueWriteRequest.QueueName, queueWriteRequest.Data, queueWriteRequest.Timestamp)) { jsonResponse.Success = true; jsonResponse.Message = "Success"; } else { jsonResponse.Success = false; jsonResponse.Message = "Not enough space in queue"; } JSONSerializer <PartnerSyncMessage> .Serialize(PartnersEngine.PrepareSignedMessage(jsonResponse), response.OutputStream); break; } case PartnerSyncMessageType.QUEUE_COMMIT: { /* Parse queue commit request */ var queueCommitRequest = JSONSerializer <PartnerSyncRequestCommit> .Deserialize(partnerSyncRequestData.Data); QueueEngine.CommitQueue(queueCommitRequest.UID, queueCommitRequest.NodeId, queueCommitRequest.ReaderId, queueCommitRequest.ReaderNodeId, queueCommitRequest.QueueName); jsonResponse.Success = true; jsonResponse.Message = "Success"; JSONSerializer <PartnerSyncMessage> .Serialize(PartnersEngine.PrepareSignedMessage(jsonResponse), response.OutputStream); break; } default: { jsonResponse.Message = "Invalid Message ID"; break; } } } catch (CryptographicException e) { Console.WriteLine(e); jsonResponse.Message = e.Message; JSONSerializer <PartnerSyncMessage> .Serialize(PartnersEngine.PrepareSignedMessage(jsonResponse), response.OutputStream); } }
public void Serve(HttpListenerRequest request, HttpListenerResponse response, Url url) { /* Know what action is being performed */ var details = url.Value.Split('/'); if (!int.TryParse(details[2], out int node)) { node = -1; } if (!int.TryParse(details[3], out int user)) { user = -1; } var queue = details[4]; var action = (QueueRequest) new DataContractJsonSerializer(typeof(QueueRequest)).ReadObject( request.InputStream); if (!UserEngine.LoginBySessionId(action.Id, action.NodeId, action.SessionKey)) { new DataContractJsonSerializer(typeof(BooleanResponse)).WriteObject(response.OutputStream, new BooleanResponse { Success = false, Message = "Failed Login" }); } else { /* Parse according to action */ if (action.Action == e_action.E_WRITE) { var jsonResponse = new BooleanResponse { Success = false }; try { if ((user != action.Id) || (node != action.NodeId)) { Console.WriteLine("Permission denied: Invalid writer user. user = "******" action.Id = " + action.Id + " node = " + node + " action.NodeId = " + action.NodeId); for (int i = 0; i < details.Length; ++i) { Console.WriteLine(details[i]); } throw new Exception("Permission denied: Invalid writer user"); } if (QueueEngine.WriteBufferedQueue(user, node, queue, action.Data)) { var errorNodes = PartnersEngine.PartnersUpdateRequest(new PartnerSyncQueueWrite { NodeId = node, UID = user, QueueName = queue, Data = action.Data, Timestamp = DateTime.UtcNow }); if (errorNodes.Count > 0) { jsonResponse.Message = "Could not notify the following partners on write: " + String.Join(" ", errorNodes.ToArray() + " (Port closed?)"); Console.WriteLine(jsonResponse.Message); } jsonResponse.Success = true; jsonResponse.Message = "Success"; } else { jsonResponse.Success = false; jsonResponse.Message = "Not enough space in queue."; } } catch (Exception e) { jsonResponse.Message = e.Message; } finally { new DataContractJsonSerializer(typeof(BooleanResponse)).WriteObject(response.OutputStream, jsonResponse); } } else if (action.Action == e_action.E_READ) { /* BAE Handle buffered queue */ QueueEngine.HandleQueueBuffer(); var jsonResponse = new QueueReadResponse { Success = false }; try { jsonResponse.Messages = QueueEngine.ReadQueue(action.Id, user, node, queue, action.Commit); if (action.Commit) { var errorNodes = PartnersEngine.PartnersUpdateRequest(new PartnerSyncRequestCommit { NodeId = node, UID = user, QueueName = queue, Commit = true, Timestamp = DateTime.UtcNow }); if (errorNodes.Count > 0) { jsonResponse.Message = "Could not notify the following partners on commit: " + String.Join(" ", errorNodes.ToArray() + " (Port closed?)"); Console.WriteLine(jsonResponse.Message); } } jsonResponse.Success = true; } catch (Exception e) { jsonResponse.Message = e.Message; } finally { new DataContractJsonSerializer(typeof(QueueReadResponse)).WriteObject(response.OutputStream, jsonResponse); } } } }