Skip to content

Commit

Permalink
Add password feature
Browse files Browse the repository at this point in the history
  • Loading branch information
“寧々” committed Jun 22, 2022
1 parent 0e9445d commit 0fdbcff
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
12 changes: 10 additions & 2 deletions ReservoirServer/AMQCommunicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ public AMQCommunicator(string uri,string queue_name,string topic_name,string pla
//Init will start communication
public void Initialization()
{
string user = null, pass = null;
if (GlobalConfig.AMQIsAuth != 0)
{
user = GlobalConfig.AMQUsername;
pass = GlobalConfig.AMQPassword;
}

Dispose();
_sender = new SimpleQueueSender(queue_name, uri);
_subscriber = new SimpleTopicSubscriber(topic_name, uri, "client_"+ platform_id, "consumer_"+ platform_id, null);
_sender = new SimpleQueueSender(queue_name, uri, user, pass);
_subscriber = new SimpleTopicSubscriber(topic_name, uri, "client_" + platform_id, "consumer_" + platform_id, null, user, pass);
_subscriber.OnMessageReceived += _subscriber_OnMessageReceived;

}

public void SendQueue(string data)
Expand Down
35 changes: 27 additions & 8 deletions ReservoirServer/Driver/SimpleAMQSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public class SimpleTopicPublisher : IDisposable
private readonly ISession session;
private readonly IMessageProducer producer;
private bool isDisposed = false;


public SimpleTopicPublisher(string topicName, string brokerUri)
public SimpleTopicPublisher(string topicName, string brokerUri, string UserName, string PassWord)
{
this.topicName = topicName;
this.connectionFactory = new ConnectionFactory(brokerUri);
this.connection = this.connectionFactory.CreateConnection();
if(UserName == null)
this.connection = this.connectionFactory.CreateConnection();
else
this.connection = this.connectionFactory.CreateConnection(UserName,PassWord);
this.connection.Start();
this.session = connection.CreateSession();
ActiveMQTopic topic = new ActiveMQTopic(topicName);
Expand Down Expand Up @@ -71,12 +75,17 @@ public class SimpleTopicSubscriber : IDisposable
private bool isDisposed = false;
public event MessageReceivedDelegate OnMessageReceived;

public SimpleTopicSubscriber(string topicName, string brokerUri, string clientId, string consumerId,string filter)


public SimpleTopicSubscriber(string topicName, string brokerUri, string clientId, string consumerId,string filter, string UserName, string PassWord)
{
string ft = filter == null ? null : $"filter='{filter}'";
this.topicName = topicName;
this.connectionFactory = new ConnectionFactory(brokerUri);
this.connection = this.connectionFactory.CreateConnection();
if(UserName == null)
this.connection = this.connectionFactory.CreateConnection();
else
this.connection = this.connectionFactory.CreateConnection(UserName,PassWord);
this.connection.ClientId = clientId;
this.connection.Start();
this.session = connection.CreateSession();
Expand Down Expand Up @@ -122,11 +131,16 @@ public class SimpleQueueSender : IDisposable
private readonly IMessageProducer producer;
private string queue_name;

public SimpleQueueSender(string queueName, string brokerUri, AcknowledgementMode ackmode = AcknowledgementMode.AutoAcknowledge)


public SimpleQueueSender(string queueName, string brokerUri,string UserName,string PassWord, AcknowledgementMode ackmode = AcknowledgementMode.AutoAcknowledge)
{
this.queue_name = queueName;
factory = new ConnectionFactory(brokerUri);
connection = factory.CreateConnection();
if (UserName == null)
connection = factory.CreateConnection();
else
connection = factory.CreateConnection(UserName, PassWord);
this.connection.Start();
session = connection.CreateSession(ackmode);
ActiveMQQueue queue = new ActiveMQQueue(queueName);
Expand Down Expand Up @@ -168,11 +182,16 @@ public class SimpleQueueReciever : IDisposable
private readonly IMessageConsumer consumer;
public event MessageReceivedDelegate OnMessageReceived;

public SimpleQueueReciever(string queueName, string brokerUri, string clientId, string filter, AcknowledgementMode ackmode = AcknowledgementMode.AutoAcknowledge)


public SimpleQueueReciever(string queueName, string brokerUri, string clientId, string filter, string UserName, string PassWord, AcknowledgementMode ackmode = AcknowledgementMode.AutoAcknowledge)
{
string ft = filter == null ? null : $"filter='{filter}'";
connectionFactory = new ConnectionFactory(brokerUri);
connection = connectionFactory.CreateConnection();
if (UserName == null)
connection = connectionFactory.CreateConnection();
else
connection = connectionFactory.CreateConnection(UserName, PassWord);
connection.ClientId = clientId;
connection.Start();
session = connection.CreateSession(ackmode);
Expand Down
3 changes: 3 additions & 0 deletions ReservoirServer/GlobalConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ static class GlobalConfig
public static byte ReporterMaxCoreUsage => (byte)ini.GetInteger("reporter", "maxcoreusage");
public static int ReportInterval => ini.GetInteger("reporter", "reportinterval");
public static string BoxServerCharset=> ini.GetValue("boxserver", "charset");
public static int AMQIsAuth => ini.GetInteger("activemq", "isauth");
public static string AMQUsername => ini.GetValue("activemq", "username");
public static string AMQPassword => ini.GetValue("activemq", "password");

public static void ParseConfigFile(string fname)
{
Expand Down
2 changes: 1 addition & 1 deletion ReservoirServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace ReservoirServer
class Program
{
//TODO: Log file, Safety(Encryption), JSON error catch, system service
public static readonly string version = "1.09";
public static readonly string version = "1.10";

#if DEBUG
public static string VERSION => version + "a";
Expand Down

0 comments on commit 0fdbcff

Please sign in to comment.