Skip to content

Commit

Permalink
Merge branch 'feature_improve_netkeys'
Browse files Browse the repository at this point in the history
  • Loading branch information
StanAccy committed Jan 16, 2014
2 parents 61a8063 + 0e34849 commit 0b359e6
Show file tree
Hide file tree
Showing 205 changed files with 3,863 additions and 1,086 deletions.
6 changes: 0 additions & 6 deletions gradle/wrapper/gradle-wrapper.properties

This file was deleted.

File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions jformica-core/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.cowboycoders</groupId>
<artifactId>jformica-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>jformica-core</artifactId>
<packaging>jar</packaging>
<name>jformica-core</name>

<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>

<dependency>
<groupId>net.vidageek</groupId>
<artifactId>mirror</artifactId>
<version>1.5.1</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ private static long rawChannelPeriodToDefaultTimeout(
}

private Node parent;

private Network assignedNetwork;

/**
* @return the parent
Expand Down Expand Up @@ -239,6 +241,9 @@ private void cleanUp() {

// remove all channelListeners
removeAllRxListeners();

// clear name
this.setName(UUID.randomUUID().toString());
}

/**
Expand Down Expand Up @@ -472,30 +477,26 @@ private ChannelMessage prepareChannelMessageForSend(ChannelMessage msg) {
return msg;
}

/**
* Sets network of channel and assigns it
*
* @param netKeyName
* @param assignMessage
*/
public void assign(String netKeyName, ChannelAssignMessage assignMessage) {
NetworkKey key = parent.getNetworkKey(netKeyName);
assign(key, assignMessage);
}

/**
* Sets network of channel and assigns it
*
* @see org.cowboycoders.ant.messages.ChannelAssignMessage
* @param netKeyName
* @param key
* @param type
* channel type
* @param extended
* extended assignment parameters
*/
public void assign(String netKeyName, ChannelType type,
public void assign(NetworkKey key, ChannelType type,
ExtendedAssignment... extended) {
assign(netKeyName, new ChannelAssignMessage(0, type, extended));
assign(key, new ChannelAssignMessage(0, type, extended));
}

public void assign(Network network, ChannelType type,
ExtendedAssignment... extended) {
ChannelAssignMessage msg = new ChannelAssignMessage(network.getNumber(), type, extended);
sendAndWaitForResponseNoError(msg);
}

/**
Expand Down Expand Up @@ -687,14 +688,28 @@ public synchronized void unassign() {
} catch (TimeoutException e) {
handleTimeOutException(e);
}
type = null ;
type = null;

if (assignedNetwork != null) {
assignedNetwork.free();
assignedNetwork = null;
}
}

public synchronized void assign(NetworkKey key,
ChannelAssignMessage assignMessage) {
int networkNumber = 0;
if (key != null) {
networkNumber = key.getNumber();

// don't leak associated networks
if(assignedNetwork != null) {
assignedNetwork.free();
assignedNetwork = null;
}
// look up network from node
assignedNetwork = parent.getNetworkForKey(key);

if (assignedNetwork != null) {
networkNumber = assignedNetwork.getNumber();
} else {
LOGGER.warning("network key not found: default to network 0");
}
Expand Down
107 changes: 107 additions & 0 deletions jformica-core/src/main/java/org/cowboycoders/ant/Network.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.cowboycoders.ant;

import java.util.concurrent.atomic.AtomicInteger;

/**
* Encapsulates an ant network
* @author will
*
*/
public class Network {

/**
* Id of network (functions as uuid)
*/
private int number;

private AtomicInteger refCount = new AtomicInteger();

/**
* Ant network key associated with this network
*/
private NetworkKey networkKey;

private NetworkListener networkListener;

/**
*
* @param number the internal ant network id
* @param networkKey key associated with this network
* @param networkListener used to register for notifications
*/
protected Network(int number, NetworkKey networkKey,
NetworkListener networkListener) {
super();
this.number = number;
this.networkKey = networkKey;
this.networkListener = networkListener;
}

protected Network(Network network) {
this(network.getNumber(),network.getNetworkKey(),network.getNetworkListener());
refCount = network.refCount;
}

/**
* Register that you are using this network and it should not be reassigned.
*/
protected void use() {
refCount.incrementAndGet();
}

/**
* Should be called when you no longer wish to use this network. Should only be called once, so
* null your reference after calling.
*/
public void free() {
if (refCount.decrementAndGet() <= 0 && networkListener != null ) {
networkListener.onFree(this);
}
}

/**
* Provides the internal ant network
* @return internal ant network id
*/
public int getNumber() {
return number;
}

/**
* Current {@link NetworkKey} associated with this network or null if no association exists.
* @return
*/
public NetworkKey getNetworkKey() {
return networkKey;
}

protected NetworkListener getNetworkListener() {
return networkListener;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + number;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Network other = (Network) obj;
if (number != other.number)
return false;
return true;
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.cowboycoders.ant;

/**
* Indicates that the network could not be allocated
* @author will
*
*/
public class NetworkAllocationException extends AntError {

/**
*
*/
private static final long serialVersionUID = 1L;

public NetworkAllocationException() {
super();
// TODO Auto-generated constructor stub
}

public NetworkAllocationException(String detailMessage, Throwable throwable) {
super(detailMessage, throwable);
// TODO Auto-generated constructor stub
}

public NetworkAllocationException(String detailMessage) {
super(detailMessage);
// TODO Auto-generated constructor stub
}

public NetworkAllocationException(Throwable throwable) {
super(throwable);
// TODO Auto-generated constructor stub
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.cowboycoders.ant;

/**
* Ensures a network reference can only be freed once
* @author will
*
*/
public class NetworkHandle extends Network {

private boolean freed = false;

protected NetworkHandle(Network network) {
super(network);
// increment reference count
use();
}


@Override
public synchronized void free() {
// if has been freed
checkState();
super.free();
freed = true;
}


private synchronized void checkState() {
if (freed) {
throw new IllegalStateException("Trying to use a network which has been freed");
}
}


@Override
protected synchronized void use() {
checkState();
super.use();
}


@Override
public synchronized int getNumber() {
checkState();
return super.getNumber();
}


@Override
public synchronized NetworkKey getNetworkKey() {
checkState();
return super.getNetworkKey();
}


@Override
protected synchronized NetworkListener getNetworkListener() {
checkState();
return super.getNetworkListener();
}







}
Loading

0 comments on commit 0b359e6

Please sign in to comment.