/// <summary> /// Create a new topic with the specified name and add it to the collection of active topics /// </summary> /// <param name="name">The desired name of the new topic</param> /// <returns>The name and assigned unique ID of the new topic</returns> protected NameIdPair CreateTopic(string name) { Guid id = Guid.NewGuid(); NameIdPair topicInfo = new NameIdPair(name, id); topics[id] = new Topic(topicInfo); return(topicInfo); }
/// <summary> /// Process a request received from the client and take action based on the request type /// </summary> /// <param name="request">The received request to process</param> /// <returns>An appropriate response to the client's request</returns> protected Response ProcessRequest(Request request) { Response response = new InfoResponse("Error: Unknown request"); if (request.Type == RequestType.CREATE_TOPIC) { // Create a new topic and send back the created topic's name and assigned unique ID NameIdPair topicInfo = CreateTopic((request as CreateTopicRequest).TopicName); response = new TopicCreatedResponse(topicInfo); } else if (request.Type == RequestType.LIST_TOPICS) { // Send back a list of active topics (without the topics' subscriber lists) response = new ListTopicsResponse(GetClientTopicList()); } else if (request.Type == RequestType.PUBLISH) { // Try to publish the received message Message <string> message = (request as PublishRequest <string>).Message; if (PublishMessage(message)) { response = new InfoResponse("Message published to topic \"" + message.TopicInfo.Name + "\""); } else { // Send back an error message if the request was for a nonexistent topic response = new InfoResponse("Message could not be published (Topic \"" + message.TopicInfo.Name + "\" does not exist)"); } } else if (request.Type == RequestType.SUBSCRIBE) { // Try to subscribe the client to the requested topic if (Subscribe(request as SubscribeRequest)) { response = new InfoResponse("Subscription added"); } else { // Send back an error message if the request was for a nonexistent topic response = new InfoResponse("Unable to add subscription (Topic does not exist)"); } } else if (request.Type == RequestType.UNSUBSCRIBE) { // Try to unsubscribe the client from the requested topic if (Unsubscribe(request as UnsubscribeRequest)) { response = new InfoResponse("Subscription removed"); } else { // Send back an error message if the subscription couldn't be removed response = new InfoResponse("Unable to remove subscription (Subscription may not exist)"); } } return(response); }
/// <summary> /// Construct a new publish request to publish the provided message to the specified topic /// </summary> /// <param name="topicInfo">The name and unique ID of the topic to publish to</param> /// <param name="messageContent">The content of the message to publish</param> /// <returns>A new publish request to publish the provided message to the specified topic</returns> public PublishRequest <T> MakePublishRequest <T>(NameIdPair topicInfo, T messageContent) { Message <T> message = new Message <T> { Timestamp = DateTime.UtcNow, PublisherInfo = new NameIdPair(Name, ID), TopicInfo = topicInfo, Content = messageContent }; return(new PublishRequest <T>(message)); }
/// <summary> /// Process a response received from the server and take action based on the response type /// </summary> /// <param name="response">The received response to process</param> protected void ProcessResponse(Response response) { if (waitingForResponse) { // Handle responses to previous client requests if (response.Type == ResponseType.TOPIC_CREATED) { // Add the new topic to the list of owned topics NameIdPair topicInfo = (response as TopicCreatedResponse).TopicInfo; OwnedTopics.Add(topicInfo); Console.WriteLine("[Topic \"{0}\" has been created]", topicInfo.Name); } else if (response.Type == ResponseType.INFO) { // Show received information response Console.WriteLine("[Info] {0}", (response as InfoResponse).Text); } waitingForResponse = false; } }
/// <summary> /// Construct an UnsubscribeRequest for the specified topic /// </summary> /// <param name="subscriber">The name and ID of the subscriber client</param> /// <param name="topicId">The ID of the topic to unsubscribe from</param> public UnsubscribeRequest(NameIdPair subscriber, Guid topicId) { Type = RequestType.UNSUBSCRIBE; Subscriber = subscriber; TopicID = topicId; }
/// <summary> /// Construct a TopicCreatedResponse with the new topic's information /// </summary> /// <param name="topicInfo">The name and assigned unique ID of the created topic</param> public TopicCreatedResponse(NameIdPair topicInfo) { Type = ResponseType.TOPIC_CREATED; TopicInfo = topicInfo; }
/// <summary> /// Constructor to initialize a new topic and create its subscriber list /// </summary> public Topic(NameIdPair info) { Info = info; Subscribers = new ConcurrentDictionary <Guid, string>(); }