-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccumulatorOps.cs
134 lines (104 loc) · 4.51 KB
/
AccumulatorOps.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Grpc.Core;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Mvc;
namespace AccumulatorOps.Services;
public class AccumulatorOpsService : AccumulatorOps.AccumulatorOpsBase
{
private readonly ILogger<AccumulatorOpsService> _logger;
private readonly object _lockObj = new();
private static int _accumulatorValue;
// Staticki konstruktor, poziva se jednom za vreme celog izvrsenja programa.
// Sluzi za inicijalizaciju statickih promenljivih.
static AccumulatorOpsService()
{
_accumulatorValue = 0;
}
public AccumulatorOpsService(ILogger<AccumulatorOpsService> logger)
{
_logger = logger;
}
// Ne obracati mnogo paznje na ovu funkciju, tu je samo zbog thread safety-ja
private int PerformOperation(OperationType type, int? operand)
{
int valueAfterOperation;
lock (_lockObj)
{
switch (type)
{
case OperationType.Nop:
break;
case OperationType.Inc:
++_accumulatorValue;
break;
case OperationType.Dec:
--_accumulatorValue;
break;
case OperationType.Add:
_accumulatorValue += operand
?? throw new ArgumentNullException("Cannot perform ADD without second operand!");
break;
default:
throw new ArgumentOutOfRangeException($"No operation of type {type} supported.");
}
valueAfterOperation = _accumulatorValue;
}
_logger.LogInformation($"Value after operation: {valueAfterOperation}.");
return valueAfterOperation;
}
public override Task<StringValue> Ping(Empty request, ServerCallContext context)
{
return Task.FromResult(new StringValue { Value = "Pong" });
}
public override Task<Int32Value> GetAccumulatorValue(Empty request, ServerCallContext context)
{
return Task.FromResult(new Int32Value { Value = _accumulatorValue });
}
public override Task<PerformOperationResponse> PerformOperationUnary(Operation request, ServerCallContext context)
{
int valueAfterOperation = PerformOperation(request.Type, request.Operand);
_logger.LogInformation($"Value after operation: {valueAfterOperation}.");
return Task.FromResult(new PerformOperationResponse
{
AccumulatorValue = valueAfterOperation,
Message = $"Successfully performed {request.Type} operation."
});
}
public override async Task<PerformOperationResponse> PerformOperationClientStream(IAsyncStreamReader<Operation> requestStream, ServerCallContext context)
{
int? lastOperationValue = null;
int numberOfOperations = 0;
while (await requestStream.MoveNext() && !context.CancellationToken.IsCancellationRequested)
{
Operation operation = requestStream.Current;
if (operation.Type == OperationType.Nop)
break;
lastOperationValue = PerformOperation(operation.Type, operation.HasOperand ? operation.Operand : null);
_logger.LogInformation($"Value after operation: {lastOperationValue}.");
numberOfOperations++;
}
return new PerformOperationResponse
{
AccumulatorValue = lastOperationValue ?? _accumulatorValue,
Message = $"Succesfully performed {numberOfOperations} operations."
};
}
public override async Task PerformOperationBiStream(IAsyncStreamReader<Operation> requestStream, IServerStreamWriter<PerformOperationResponse> responseStream, ServerCallContext context)
{
int operationValue;
int numberOfOperations = 0;
while (!context.CancellationToken.IsCancellationRequested && await requestStream.MoveNext())
{
Operation operation = requestStream.Current;
if (operation.Type == OperationType.Nop)
break;
operationValue = PerformOperation(operation.Type, operation.HasOperand ? operation.Operand : null);
_logger.LogInformation($"Value after operation: {operationValue}.");
numberOfOperations++;
await responseStream.WriteAsync(new PerformOperationResponse
{
AccumulatorValue = operationValue,
Message = $"Succesfully performed {numberOfOperations + 1} operation."
});
}
}
}