package com.kurento.kmf.media;

import java.io.IOException;

import org.apache.thrift.TException;
import org.apache.thrift.async.AsyncMethodCallback;

import com.kurento.kmf.media.internal.MediaServerServiceManager;
import com.kurento.kms.api.MediaObjectNotFoundException;
import com.kurento.kms.api.MediaServerException;
import com.kurento.kms.api.MediaServerService;
import com.kurento.kms.api.MediaServerService.AsyncClient.createMixerEndPoint_call;

public abstract class Mixer extends MediaObject {

	private static final long serialVersionUID = 1L;

	static final String MIXER_TYPE_FIELD_NAME = "mixerType";

	Mixer(com.kurento.kms.api.MediaObject mixer) {
		super(mixer);
	}

	/* SYNC */

	public MixerEndPoint createMixerEndPoint() throws IOException {
		try {
			MediaServerServiceManager manager = MediaServerServiceManager
					.getInstance();
			MediaServerService.Client service = manager.getMediaServerService();
			com.kurento.kms.api.MediaObject mixerPort = service
					.createMixerEndPoint(mediaObject);
			manager.releaseMediaServerService(service);
			return new MixerEndPoint(mixerPort);
		} catch (MediaObjectNotFoundException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (MediaServerException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (TException e) {
			throw new IOException(e.getMessage(), e);
		}
	}

	/* ASYNC */

	public void createMixerEndPoint(final Continuation<MixerEndPoint> cont)
			throws IOException {
		try {
			MediaServerServiceManager manager = MediaServerServiceManager
					.getInstance();
			MediaServerService.AsyncClient service = manager
					.getMediaServerServiceAsync();
			service.createMixerEndPoint(
					mediaObject,
					new AsyncMethodCallback<MediaServerService.AsyncClient.createMixerEndPoint_call>() {
						@Override
						public void onComplete(createMixerEndPoint_call response) {
							try {
								com.kurento.kms.api.MediaObject mixerPort = response
										.getResult();
								cont.onSuccess(new MixerEndPoint(mixerPort));
							} catch (MediaObjectNotFoundException e) {
								cont.onError(new RuntimeException(e
										.getMessage(), e));
							} catch (MediaServerException e) {
								cont.onError(new RuntimeException(e
										.getMessage(), e));
							} catch (TException e) {
								cont.onError(new IOException(e.getMessage(), e));
							}
						}

						@Override
						public void onError(Exception exception) {
							cont.onError(exception);
						}
					});
			manager.releaseMediaServerServiceAsync(service);
		} catch (TException e) {
			throw new IOException(e.getMessage(), e);
		}
	}

}

@@ -142,16 +142,72 @@
 allback;
+%0Aimport org.apache.thrift.transport.TTransportException;
 %0A%0Aimport
@@ -806,33 +806,24 @@
 Exception %7B%0A
-%09%09try %7B%0A%09
 %09%09MediaServe
@@ -870,33 +870,32 @@
 viceManager%0A%09%09%09%09
-%09
 .getInstance();%0A
@@ -886,33 +886,32 @@
 .getInstance();%0A
-%09
 %09%09MediaServerSer
@@ -918,24 +918,44 @@
 vice.Client 
+service;%0A%09%09try %7B%0A%09%09%09
 service = ma
@@ -991,185 +991,168 @@
 ;%0A%09%09
-%09com.kurento.kms.api.MediaObject mixerPort = service%0A%09%09%09%09%09.createMixerEndPoint(mediaObject);%0A%09%09%09manager.releaseMediaServerService(service);%0A%09%09%09return new MixerEndPoint(mixerPort
+%7D catch (TTransportException e) %7B%0A%09%09%09throw new IOException(e.getMessage(), e);%0A%09%09%7D%0A%0A%09%09try %7B%0A%09%09%09return new MixerEndPoint(service.createMixerEndPoint(mediaObject)
 );%0A%09
@@ -1396,32 +1396,93 @@
 tMessage(), e);%0A
+%09%09%7D finally %7B%0A%09%09%09manager.releaseMediaServerService(service);%0A
 %09%09%7D%0A%09%7D%0A%0A%09/* ASYN
@@ -1584,25 +1584,16 @@
 ption %7B%0A
-%09%09try %7B%0A%09
 %09%09MediaS
@@ -1648,25 +1648,24 @@
 Manager%0A%09%09%09%09
-%09
 .getInstance
@@ -1668,17 +1668,16 @@
 ance();%0A
-%09
 %09%09MediaS
@@ -1697,24 +1697,44 @@
 AsyncClient 
+service;%0A%09%09try %7B%0A%09%09%09
 service = ma
@@ -1738,22 +1738,16 @@
  manager
-%0A%09%09%09%09%09
 .getMedi
@@ -1769,16 +1769,110 @@
 sync();%0A
+%09%09%7D catch (TTransportException e) %7B%0A%09%09%09throw new IOException(e.getMessage(), e);%0A%09%09%7D%0A%0A%09%09try %7B%0A
 %09%09%09servi
@@ -2112,133 +2112,70 @@
 %09%09co
-m.kurento.kms.api.MediaObject mixerPort = response%0A%09%09%09%09%09%09%09%09%09%09.getResult();%0A%09%09%09%09%09%09%09%09cont.onSuccess(new MixerEndPoint(mixerPort
+nt.onSuccess(new MixerEndPoint(response%0A%09%09%09%09%09%09%09%09%09%09.getResult()
 ));%0A
@@ -2640,60 +2640,8 @@
 %7D);%0A
-%09%09%09manager.releaseMediaServerServiceAsync(service);%0A
 %09%09%7D 
@@ -2708,18 +2708,83 @@
 (), e);%0A
+%09%09%7D finally %7B%0A%09%09%09manager.releaseMediaServerServiceAsync(service);%0A
 %09%09%7D%0A%09%7D%0A
-%0A
 %7D%0A

Implement Mixer