Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
Refactoring for protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfranz committed Jun 18, 2018
1 parent 1934617 commit 478798a
Show file tree
Hide file tree
Showing 15 changed files with 9,069 additions and 52 deletions.
12 changes: 10 additions & 2 deletions AnnelidaDispatcher/AnnelidaDispatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
<HintPath>..\packages\DnsClient.1.0.7\lib\net45\DnsClient.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Google.Protobuf, Version=3.5.1.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL">
<HintPath>..\packages\Google.Protobuf.3.5.1\lib\net45\Google.Protobuf.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MongoDB.Bson, Version=2.6.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDB.Bson.2.6.1\lib\net45\MongoDB.Bson.dll</HintPath>
<Private>True</Private>
Expand Down Expand Up @@ -108,15 +112,18 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Model\DataTransmission\AnnelidaSensors.cs" />
<Compile Include="Model\Server\AsyncAbstractServer.cs" />
<Compile Include="Model\Server\IMessageDispatcherStrategy.cs" />
<Compile Include="Model\Server\AbstractServer.cs" />
<Compile Include="Model\Server\AsyncDispatcherServer.cs" />
<Compile Include="Model\Server\BsonServer.cs" />
<Compile Include="Model\MongoWrapper.cs" />
<Compile Include="Model\Record.cs" />
<Compile Include="Model\Sensors.cs" />
<Compile Include="Model\Server\AsyncDispatcherServerDBEnabled.cs" />
<Compile Include="Model\Server\BsonServerDBEnabled.cs" />
<Compile Include="Model\Server\MongoDispatchStrategy.cs" />
<Compile Include="Model\Server\NoDBDispatchStrategy.cs" />
<Compile Include="Model\Server\ProtoBufServer.cs" />
<Compile Include="Strings.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down Expand Up @@ -168,6 +175,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Model\DataTransmission\annelida_sensors.proto" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down
4,275 changes: 4,275 additions & 0 deletions AnnelidaDispatcher/Model/DataTransmission/AnnelidaSensors.cs

Large diffs are not rendered by default.

169 changes: 169 additions & 0 deletions AnnelidaDispatcher/Model/DataTransmission/annelida_sensors.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
syntax = "proto3";

import "google/protobuf/timestamp.proto";

enum SolenoidState{

UNKNOW_SOLENOID = 0;
ACTIVED = 1;
DISABLED = 2;
}
enum CylinderState{

UNKNOW_CYLINDER = 0;
ADVANCED = 1;
WITHDRAWN = 2;
}

message AnnelidaSensors {

message UmbilicalReception{
uint32 traction = 1;
}

message NotRegulatedConverter{

repeated float internal_temperature = 1;
repeated float internal_pressure = 2;
repeated float electrical_current = 3;
repeated float electrical_power = 4;
repeated float electrical_tension_700v = 5;
}

message RegulatedConverter{

repeated float internal_temperature = 1;
repeated float internal_pressure = 2;
repeated float electrical_current = 3;
repeated float electrical_power = 4;
repeated float electrical_tension = 5;
}

message EmbeddedSystem{

repeated float internal_temperature = 1;
repeated float internal_pressure = 2;
repeated float external_module_pressure = 3;
repeated float rotation_x = 4;
repeated float rotation_y = 5;
repeated float rotation_z = 6;
repeated float displacement_x = 7;
repeated float displacement_y = 8;
repeated float displacement_z = 9;
repeated float acceleration_x = 10;
repeated float acceleration_y = 11;
repeated float acceleration_z = 12;
}

message MotorController{

repeated float internal_temperature = 1;
repeated float internal_pressure = 2;
repeated float electrical_current = 3;
repeated float electrical_tension = 4;
}

message Locomotive{

repeated CylinderState cylinder_forward = 1;
repeated CylinderState cylinder_backward = 2;
}

message PumpsEngine{

message Manifold_1_Backward{

repeated SolenoidState solenoid_cylinder_move_backward = 1;
repeated SolenoidState solenoid_cylinder_active_backward = 2;
}
message Manifold_3_Backward{

repeated SolenoidState solenoid_active_foot_backward = 1;
}
message MotorBomb_1{

repeated float oil_temperature = 1;
repeated uint32 motor_rpm = 2;
repeated float motor_power = 3;
}
message MotorBomb_2{

repeated uint32 motor_rpm = 1;
repeated float motor_power = 2;

}
message Manifold_2_Auxiliar{

repeated float hydraulic_foots_pressure = 1;
repeated SolenoidState solenoid_pressure_foots = 2;
}
message MotorBombAuxiliar{

repeated float temperature_sensor = 1;
repeated uint32 motor_rpm = 2;
repeated float motor_power = 3;
}
message Manifold_2_Main{

repeated float hydraulic_cylinder_pressure = 1;
repeated SolenoidState solenoid_move_pressure = 2;
}
message MotorBomb_3{

repeated float motor_rpm = 1;
repeated float motor_power = 2;
}
message MotorBomb_4{

repeated float oil_temperature = 1;
repeated uint32 motor_rpm = 2;
repeated float motor_power = 3;
}
message Manifold_3_Forward{

repeated SolenoidState solenoid_active_forward = 1;
}
message Manifold_1_Forward{

repeated SolenoidState solenoid_cylinder_move_forward = 1;
repeated SolenoidState solenoid_cylinder_active_forward = 2;
}

Manifold_1_Backward enc_manifold_1_backward = 1;
Manifold_3_Backward enc_manifold_3_backward = 2;
MotorBomb_1 enc_motor_bomb_1 = 3;
MotorBomb_2 enc_motor_bomb_2 = 4;
Manifold_2_Auxiliar enc_manifold_2_auxiliar = 5;
MotorBombAuxiliar enc_motor_bomb_auxiliar = 6;
Manifold_2_Main enc_manifold_2_main = 7;
MotorBomb_3 enc_motor_bomb_3 = 8;
MotorBomb_4 enc_motor_bomb_4 = 9;
Manifold_3_Forward enc_manifold_3_forward = 10;
Manifold_1_Forward enc_manifold_1_forward = 11;
}
message SgnReactor{
repeated float sgn_temperature = 1;
repeated bool obstruction_detected = 2;
repeated float reactor_pressure = 3;
}


google.protobuf.Timestamp timestamp = 1;

UmbilicalReception enc_reception = 2;
NotRegulatedConverter enc_not_regulated_1 = 3;
NotRegulatedConverter enc_not_regulated_2 = 4;
RegulatedConverter enc_regulated_1 = 5;
RegulatedConverter enc_regulated_2 = 6;
EmbeddedSystem enc_embedded_system = 7;
MotorController enc_motor_controller_1 = 8;
MotorController enc_motor_controller_2 = 9;
MotorController enc_motor_controller_3 = 10;
MotorController enc_motor_controller_4 = 11;
MotorController enc_motor_controller_5 = 12;
Locomotive enc_forward_locomotive = 13;
Locomotive enc_backward_locomotive = 14;
PumpsEngine system_pumps = 15;
SgnReactor enc_reactor = 16;

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace AnnelidaDispatcher.Model.Server
{
public class AsyncDispatcherServer : AbstractServer
public abstract class AsyncAbstractServer : AbstractServer
{
private CancellationTokenSource cts;
private TcpListener listener;
public AsyncDispatcherServer(int tcpPort)


public AsyncAbstractServer(int tcpPort)
{
cts = new CancellationTokenSource();
listener = new TcpListener(IPAddress.Any, tcpPort);
messageDispatchStrategy = new NoDBDispatchStrategy();


}

public override void Start()
Expand Down Expand Up @@ -82,13 +77,13 @@ private async Task ClientHandler(TcpClient client, int clientIndex, Cancellation
Console.WriteLine(ex);
break;
}

if (ammountRead == 0) break;


if (myType == ClientTypes.Types.Undefined)
{
myType = IdentifyClient(buf,client);
myType = IdentifyClient(buf, client);
OnClientConnected(myType, clientEndPoint.Address.ToString());
//ClientConnectedEvent?.Invoke(myType, clientEndPoint.Address.ToString());
}
Expand All @@ -97,17 +92,17 @@ private async Task ClientHandler(TcpClient client, int clientIndex, Cancellation
totalReceivedSize += ammountRead;
if (ammountToReceive == 4)
{
byte[] size = {buf[0], buf[1], buf[2], buf[3]};
byte[] size = { buf[0], buf[1], buf[2], buf[3] };
totalMessageSize = BitConverter.ToInt32(size, 0);
ammountToReceive = totalMessageSize - 4;
completedMessage = new byte[totalMessageSize];
Array.Copy(size,completedMessage,4);
Array.Copy(size, completedMessage, 4);

}
else
{
Array.Copy(buf, 0, completedMessage, totalReceivedSize - ammountRead, ammountRead);

if (totalReceivedSize == totalMessageSize)
{
HandleMessage(completedMessage, totalMessageSize, myType);
Expand Down Expand Up @@ -138,30 +133,6 @@ private ClientTypes.Types IdentifyClient(byte[] buffer, TcpClient client)
return myType;
}

private void HandleMessage(byte[] buffer, int ammountRead,ClientTypes.Types myType)
{
var message = ProcessSerializedBson(buffer, ammountRead);

messageDispatchStrategy.RedespatchMessage(message.ToBson(), connectedClients, myType);
}
private BsonDocument ProcessSerializedBson(byte[] bytes, int packageSize)
{

try
{
var doc = BsonSerializer.Deserialize<BsonDocument>(bytes);
BsonDateTime timestamp = DateTime.UtcNow;
doc["timestamp"] = timestamp;
return doc;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());

return null;
}
}


protected abstract void HandleMessage(byte[] buffer, int ammountRead, ClientTypes.Types myType);
}
}
}
48 changes: 48 additions & 0 deletions AnnelidaDispatcher/Model/Server/BsonServer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;

namespace AnnelidaDispatcher.Model.Server
{
public class BsonServer : AsyncAbstractServer
{
public BsonServer(int tcpPort) : base(tcpPort)
{

}

protected override void HandleMessage(byte[] buffer, int ammountRead,ClientTypes.Types myType)
{
var message = ProcessSerializedBson(buffer, ammountRead);

messageDispatchStrategy.RedespatchMessage(message.ToBson(), connectedClients, myType);
}
private BsonDocument ProcessSerializedBson(byte[] bytes, int packageSize)
{

try
{
var doc = BsonSerializer.Deserialize<BsonDocument>(bytes);
BsonDateTime timestamp = DateTime.UtcNow;
doc["timestamp"] = timestamp;
return doc;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());

return null;
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace AnnelidaDispatcher.Model.Server
{
public class AsyncDispatcherServerDBEnabled : AsyncDispatcherServer
public class BsonServerDBEnabled : BsonServer
{


/// <inheritdoc />
public AsyncDispatcherServerDBEnabled(string missionName, int tcpPort) : base(tcpPort)
public BsonServerDBEnabled(string missionName, int tcpPort) : base(tcpPort)
{
var sensorDb = new MongoWrapper("sensors");
var controlDb = new MongoWrapper("control");
Expand Down
2 changes: 1 addition & 1 deletion AnnelidaDispatcher/Model/Server/MongoDispatchStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public MongoDispatchStrategy(MongoWrapper sensorDb, MongoWrapper controlDb, stri
};
}

public void RedespatchMessage(byte[] message, Dictionary<ClientTypes.Types, List<TcpClient>> connectedClients,
public new void RedespatchMessage(byte[] message, Dictionary<ClientTypes.Types, List<TcpClient>> connectedClients,
ClientTypes.Types origin)
{
base.RedespatchMessage(message,connectedClients,origin);
Expand Down
Loading

0 comments on commit 478798a

Please sign in to comment.