Commit 94d591c2 authored by Benjamin Krämer's avatar Benjamin Krämer

Fixed some bugs + optimization

- Using TcpClient instead of Socket
- Modifying edge attributes works now
- Serialization of varints works now
- Improved the examples
parent 27171744
# VS2013 User-specific files #
##############################
*.suo
*.user
*.userosscache
*.sln.docstates
# VS2013 Build results #
########################
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
# Intellij stuff #
##################
*.iml
*.ipr
*.iws
.idea/
# Build and unit tests stuff #
#############################
logs/
target/
out/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.next
release.properties
work/
amps-standalone/
*/dependency-reduced-pom.xml
# Mac stuff #
#############
.DS_Store
......@@ -52,7 +52,7 @@ namespace Netstream
private readonly byte[] _streamIdArray;
private readonly String _host;
private readonly int _port;
private Socket _socket;
private TcpClient _client;
private BufferedStream _outStream;
private NetStreamPacker _packer = new DefaultPacker();
......@@ -120,9 +120,8 @@ namespace Netstream
protected void Connect()
{
_socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
_socket.Connect(new DnsEndPoint(_host, _port));
_outStream = new BufferedStream(new NetworkStream(_socket));
_client = new TcpClient(_host, _port);
_outStream = new BufferedStream(_client.GetStream());
}
/**
......@@ -131,7 +130,7 @@ namespace Netstream
private void DoSend(NetStreamStorage buff)
{
if (!_socket.Connected)
if (!_client.Connected)
{
Console.Error.WriteLine("NetStreamSender : can't send. The socket is closed.");
}
......@@ -156,43 +155,6 @@ namespace Netstream
}
}
protected void AddAttribute(String sourceId, ulong timeId, String attribute, Object value, NetStreamEvent e)
{
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(e).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(attribute).
EncodeValueWithType(value);
DoSend(buff);
}
protected void ChangeAttribute(String sourceId, ulong timeId, String attribute, Object oldValue, Object newValue,
NetStreamEvent e)
{
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(e).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(attribute).
EncodeValueWithType(oldValue).
EncodeValueWithType(newValue);
DoSend(buff);
}
protected void RemoveAttribute(String sourceId, ulong timeId, String attribute, NetStreamEvent e)
{
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(e).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(attribute);
DoSend(buff);
}
/*
* (non-Javadoc)
*
......@@ -202,7 +164,14 @@ namespace Netstream
*/
public void AddGraphAttribute(String sourceId, ulong timeId, String attribute, Object value)
{
AddAttribute(sourceId, timeId, attribute, value, NetStreamEvent.AddGraphAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.AddGraphAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(attribute).
EncodeValueWithType(value);
DoSend(buff);
}
/*
......@@ -215,7 +184,15 @@ namespace Netstream
public void ChangeGraphAttribute(String sourceId, ulong timeId, String attribute, Object oldValue,
Object newValue)
{
ChangeAttribute(sourceId, timeId, attribute, oldValue, newValue, NetStreamEvent.ChgGraphAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.ChgGraphAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(attribute).
EncodeValueWithType(oldValue).
EncodeValueWithType(newValue);
DoSend(buff);
}
/*
......@@ -227,7 +204,13 @@ namespace Netstream
*/
public void RemoveGraphAttribute(String sourceId, ulong timeId, String attribute)
{
RemoveAttribute(sourceId, timeId, attribute, NetStreamEvent.DelGraphAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.DelGraphAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(attribute);
DoSend(buff);
}
/*
......@@ -240,7 +223,15 @@ namespace Netstream
public void AddNodeAttribute(String sourceId, ulong timeId, String nodeId,
String attribute, Object value)
{
AddAttribute(sourceId, timeId, attribute, value, NetStreamEvent.AddNodeAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.AddNodeAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(nodeId).
EncodeString(attribute).
EncodeValueWithType(value);
DoSend(buff);
}
/*
......@@ -254,7 +245,16 @@ namespace Netstream
public void ChangeNodeAttribute(String sourceId, ulong timeId,
String nodeId, String attribute, Object oldValue, Object newValue)
{
ChangeAttribute(sourceId, timeId, attribute, oldValue, newValue, NetStreamEvent.ChgNodeAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.ChgNodeAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(nodeId).
EncodeString(attribute).
EncodeValueWithType(oldValue).
EncodeValueWithType(newValue);
DoSend(buff);
}
/*
......@@ -267,7 +267,14 @@ namespace Netstream
public void RemoveNodeAttribute(String sourceId, ulong timeId,
String nodeId, String attribute)
{
RemoveAttribute(sourceId, timeId, attribute, NetStreamEvent.DelNodeAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.DelNodeAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(nodeId).
EncodeString(attribute);
DoSend(buff);
}
/*
......@@ -280,7 +287,15 @@ namespace Netstream
public void AddEdgeAttribute(String sourceId, ulong timeId, String edgeId,
String attribute, Object value)
{
AddAttribute(sourceId, timeId, attribute, value, NetStreamEvent.AddEdgeAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.AddEdgeAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(edgeId).
EncodeString(attribute).
EncodeValueWithType(value);
DoSend(buff);
}
/*
......@@ -294,7 +309,16 @@ namespace Netstream
public void ChangeEdgeAttribute(String sourceId, ulong timeId,
String edgeId, String attribute, Object oldValue, Object newValue)
{
ChangeAttribute(sourceId, timeId, attribute, oldValue, newValue, NetStreamEvent.ChgEdgeAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.ChgEdgeAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(edgeId).
EncodeString(attribute).
EncodeValueWithType(oldValue).
EncodeValueWithType(newValue);
DoSend(buff);
}
/*
......@@ -307,7 +331,14 @@ namespace Netstream
public void RemoveEdgeAttribute(String sourceId, ulong timeId,
String edgeId, String attribute)
{
RemoveAttribute(sourceId, timeId, attribute, NetStreamEvent.DelEdgeAttr);
NetStreamStorage buff = new NetStreamStorage().
EncodeArray(_streamIdArray).
EncodeEvent(NetStreamEvent.DelEdgeAttr).
EncodeString(sourceId).
EncodeNative(timeId).
EncodeString(edgeId).
EncodeString(attribute);
DoSend(buff);
}
/*
......@@ -422,7 +453,7 @@ namespace Netstream
*/
public void Close()
{
_socket.Close();
_client.Close();
}
}
}
......@@ -261,25 +261,32 @@ namespace Netstream
private void Serialize(object input)
{
byte[] bytes;
byte[] bytes = null;
// Bitconvert normal types:
if (input is bool)
bytes = BitConverter.GetBytes((bool) input);
else if (input is float)
bytes = BitConverter.GetBytes((float) input);
else if (input is double)
bytes = BitConverter.GetBytes((double) input);
if (bytes != null)
{
// Make big endian if bit converted:
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
}
// Handle all other types:
else if (input is byte || input is char)
bytes = new byte[] { Convert.ToByte(input) };
bytes = new byte[] {Convert.ToByte(input)};
else if (input is short || input is int || input is long)
bytes = GetVarint(Convert.ToInt64(input));
else if (input is ushort || input is uint || input is ulong)
bytes = GetUnsignedVarint(Convert.ToInt64(input));
else if (input is float)
bytes = BitConverter.GetBytes((float) input);
else if (input is double)
bytes = BitConverter.GetBytes((double) input);
else
throw new ArgumentException();
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
Write(bytes, 0, bytes.Length);
}
}
......
......@@ -39,7 +39,10 @@
* @author Benjamin Krämer <benjamin.kraemer@in.tum.de>
*
*/
using System;
using System.Text;
using System.Threading;
namespace Netstream
{
......@@ -49,8 +52,8 @@ namespace Netstream
{
//Endless();
//EventsTest();
TypesTest();
//Example();
//TypesTest();
Example();
}
private static void Endless()
......@@ -59,10 +62,11 @@ namespace Netstream
ulong timeId = 0L;
var stream = new NetStreamSender("default", "localhost", 2001);
const string n1 = "node";
while (true)
while (timeId < 1000)
{
stream.AddNode(sourceId, timeId++, n1);
stream.AddNode(sourceId, timeId++, n1 + timeId);
}
stream.Close();
}
private static void Example()
......@@ -70,35 +74,71 @@ namespace Netstream
const string sourceId = "C++_netstream_test";
ulong timeId = 0L;
var stream = new NetStreamSender("default", "localhost", 2001);
const string style = "node{fill-mode:plain;fill-color:#567;size:6px;}";
const string style = "node {" +
" shape: rounded-box;" +
" fill-color: grey;" +
" fill-mode: dyn-plain;" +
" size-mode: fit;" +
"}" +
"node.active {" +
" fill-color: green;" +
" size:20px;" +
"}" +
"edge {" +
" shape: cubic-curve;" +
" size: 1px;" +
" arrow-shape: arrow;" +
"}" +
"edge.active {" +
" fill-color: green;" +
" size: 5px;" +
"}";
stream.AddGraphAttribute(sourceId, timeId++, "stylesheet", style);
stream.AddGraphAttribute(sourceId, timeId++, "test", "test");
stream.ChangeGraphAttribute(sourceId, timeId++, "test", "test", false);
stream.AddGraphAttribute(sourceId, timeId++, "ui.antialias", true);
stream.AddGraphAttribute(sourceId, timeId++, "ui.quality", true);
stream.AddGraphAttribute(sourceId, timeId++, "layout.stabilization-limit", 0);
for (int i = 0; i < 500; i++)
{
var n1 = new StringBuilder();
n1.Append(i);
stream.AddNode(sourceId, timeId++, n1.ToString());
var n1 = i.ToString();
AddNodeWithLabel(stream, sourceId, ref timeId, n1);
if (i > 0)
{
var n2 = new StringBuilder();
n2.Append(i - 1);
var n3 = new StringBuilder();
n3.Append(i/2);
var e1 = new StringBuilder();
e1.Append(n1).Append("-").Append(n2);
var n2 = (i - 1).ToString();
var n3 = (i/2).ToString();
var e1 = n1 + "-" + n2;
var e2 = n1 + "-" + n3;
var e2 = new StringBuilder();
e2.Append(n1).Append("-").Append(n3);
AddEdgeWithLabel(stream, sourceId, ref timeId, e1, n1, n2);
stream.AddEdge(sourceId, timeId++, e1.ToString(), n1.ToString(), n2.ToString(), false);
stream.AddEdge(sourceId, timeId++, e2.ToString(), n1.ToString(), n3.ToString(), false);
if (!e1.Equals(e2))
AddEdgeWithLabel(stream, sourceId, ref timeId, e2, n1, n3);
}
//Thread.Sleep(100);
}
stream.Close();
}
private static String lastEdge, lastNode;
private static void AddEdgeWithLabel(NetStreamSender stream, string sourceId, ref ulong timeId, string name, string nodeFrom, string nodeTo)
{
if (lastEdge != null)
stream.RemoveEdgeAttribute(sourceId, timeId++, lastEdge, "ui.class");
stream.AddEdge(sourceId, timeId++, name, nodeFrom, nodeTo, false);
stream.AddEdgeAttribute(sourceId, timeId++, name, "ui.label", name);
stream.AddEdgeAttribute(sourceId, timeId++, name, "ui.class", "active");
lastEdge = name;
}
private static void AddNodeWithLabel(NetStreamSender stream, string sourceId, ref ulong timeId, string name)
{
if (lastNode != null)
stream.RemoveNodeAttribute(sourceId, timeId++, lastNode, "ui.class");
stream.AddNode(sourceId, timeId++, name);
stream.AddNodeAttribute(sourceId, timeId++, name, "ui.label", name);
stream.AddNodeAttribute(sourceId, timeId++, name, "ui.class", "active");
lastNode = name;
}
private static void TypesTest()
......@@ -133,6 +173,7 @@ namespace Netstream
stream.AddGraphAttribute(sourceId, timeId++, "booleanArray", v6);
stream.AddGraphAttribute(sourceId, timeId++, "string", "true");
stream.Close();
}
private static void EventsTest()
......@@ -155,6 +196,7 @@ namespace Netstream
stream.RemoveEdge(sourceId, timeId++, "edge");
stream.RemoveNode(sourceId, timeId++, "node0");
stream.GraphClear(sourceId, timeId++);
stream.Close();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment